注入traces数据中未显示在loki-grafana上

发布于 2025-01-19 21:06:28 字数 2637 浏览 0 评论 0原文

设置: 我正在使用 python-logging-loki 库来手动检测我的代码的日志。我有一个正在运行的 Loki 实例(未使用 promtail,我直接推送到 loki)。 在 Grafana 上,我启动并运行了 Jaeger 和 Loki 作为数据源。对于 Traces,我使用 opentelemetry python 库(也手动检测代码)。

我正在尝试使用 Opentelemetry 日志记录仪器 库,用于在将跟踪数据发送到 Loki 之前自动将跟踪数据注入到我的日志中。 我的问题是,当我运行测试应用程序时,我可以看到在控制台上打印出注入的数据,但在 grafana 上注入的数据没有显示。

我为派生字段配置了 Loki,以使用正则表达式提取trace_id,并且内部链接指向 jaeger。

    # auto inject trace data into logs (Opentelemetry Logging Instrumentation library)
    LoggingInstrumentor().instrument(set_logging_format=True, log_level=logging.DEBUG)

    # manual instrumentation of traces 
    with tracer.start_as_current_span(name="random-name") as span:

        # using python-logging-loki library to construct the logs
        logger.info(msg="testing loki logger", extra={"tags": {"product_number": "ABCD123"}})

在 Grafana 上,日志已成功导出,但跟踪数据未出现,尽管在 Grafana 上的 Loki 中设置了派生字段值

在此处输入图像描述

在此处输入图像描述

在我的控制台上,但是当我运行测试应用程序时,我可以看到日志消息的打印输出,其中注入了跟踪数据,尽管

2022-04-08 15:17:15,476 DEBUG [urllib3.connectionpool] [connectionpool.py:228] [trace_id=e4fb8555337a28cc639955a36d994ed1 span_id=86425d0bc3cc088c resource.service.name=] - Starting new HTTP connection (1): localhost:3100
2022-04-08 15:17:15,498 DEBUG [urllib3.connectionpool] [connectionpool.py:456] [trace_id=e4fb8555337a28cc639955a36d994ed1 span_id=86425d0bc3cc088c resource.service.name=] - http://localhost:3100 "POST /loki/api/v1/push HTTP/1.1" 204 0
2022-04-08 15:17:15,469 INFO [otlp_handler] [main.py:39] [trace_id=e4fb8555337a28cc639955a36d994ed1 span_id=86425d0bc3cc088c resource.service.name=] - testing loki logger

我不确定我在这里做错了什么! 只有当我明确地将trace_id写入日志消息时,grafana才会派生这些值。因此,如果我这样写日志:

logger.info(msg="[ trace_id=d4ea420b4fa4e20e7fbb579c80fa4e88 span_id=856ca68d4b52bbd4 ] - testing loki logger", extra={"tags": {"product_number": "ABCD123"}})

上的跟踪

然后在 grafana loki 上,我可以看到记录中的派生字段,并且它正确链接到 jaeger 在此处输入图像描述

我做错了什么? .. 这是使用 otel 记录仪器的正确方法吗? ..

SETUP:
i'm using the python-logging-loki library to manually instrument my code for logs. I have a single running instance of Loki (promtail not used, i'm pushing directly to loki).
On Grafana i have Jaeger and Loki up and running as data sources. For Traces i'm using opentelemetry python library (also manually instrumented the code).

I'm trying to use the Opentelemetry Logging Instrumentation library to automatically inject trace data into my logs before they are sent to Loki.
My problem is, i can see the injected data printed out on my console when i run my test app but on grafana the injected data does not show up.

i configured Loki for derived fields to extract the trace_id with a regex and an internal link is pointing to jaeger.

    # auto inject trace data into logs (Opentelemetry Logging Instrumentation library)
    LoggingInstrumentor().instrument(set_logging_format=True, log_level=logging.DEBUG)

    # manual instrumentation of traces 
    with tracer.start_as_current_span(name="random-name") as span:

        # using python-logging-loki library to construct the logs
        logger.info(msg="testing loki logger", extra={"tags": {"product_number": "ABCD123"}})

On Grafana, the logs are exported successfully but the trace data does not appear, despite setting the derived fields values in Loki on Grafana

enter image description here

enter image description here

on my console however when i run my test app, i can see a printout of the log msg with the trace data injected into though

2022-04-08 15:17:15,476 DEBUG [urllib3.connectionpool] [connectionpool.py:228] [trace_id=e4fb8555337a28cc639955a36d994ed1 span_id=86425d0bc3cc088c resource.service.name=] - Starting new HTTP connection (1): localhost:3100
2022-04-08 15:17:15,498 DEBUG [urllib3.connectionpool] [connectionpool.py:456] [trace_id=e4fb8555337a28cc639955a36d994ed1 span_id=86425d0bc3cc088c resource.service.name=] - http://localhost:3100 "POST /loki/api/v1/push HTTP/1.1" 204 0
2022-04-08 15:17:15,469 INFO [otlp_handler] [main.py:39] [trace_id=e4fb8555337a28cc639955a36d994ed1 span_id=86425d0bc3cc088c resource.service.name=] - testing loki logger

i'm not sure what i'm doing wrong here !
Only when i explicitly write down the trace_id into the log message, grafana is deriving those values. So if i write the log like this:

logger.info(msg="[ trace_id=d4ea420b4fa4e20e7fbb579c80fa4e88 span_id=856ca68d4b52bbd4 ] - testing loki logger", extra={"tags": {"product_number": "ABCD123"}})

then on grafana loki, i can see the derived fields in the records and it links correctly to the trace on jaeger

enter image description here

What am i doing wrong? .. is this the correct way to use this Logging Instrumentation by otel? ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伏妖词 2025-01-26 21:06:28

您可能需要在[trace_id之间放置一个空间吗?解析时,字段被空间分开,因此在您的日志中,字段名称可能最终以[trace_id

Could it be that you need to put a space between [ and trace_id? When parsing, fields are separated by a space, so in your log the field name might end up as [trace_id.

无法回应 2025-01-26 21:06:28

如果我不得不猜测,我会说您在Grafana中看到的日志条目是创建的,而实际上没有积极的跨度。

不确定情况如何与Python一起使用,但是与JS一起使用。您可以在loghook回调功能中放置一个断点,以确保实际上有一个跨度运行。

您也可以尝试手动获取示踪剂并包装log.info()tracer.startactivespan中调用。这是它在打字稿中的工作方式:

import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston';
import { Span } from '@opentelemetry/api';

// initTracer does all the otel tracer setup stuff and takes an optional param InstrumentationOption[]
initTracer([
  new WinstonInstrumentation({
    logHook: (span: Span) => {
      console.log(span) // <---- Put breakpoint here.
    },
  }),
]);

// manually start a span
trace
  .getTracer('my-service')
  .startActiveSpan('seedRootUser', async (span: Span) => {
    logger.info('This will have the Span\'s span_id and trace_id attached to the log data');
    span.end(); // make sure to call or I won't work!
  });

If I had to guess I'd say the log entry you're seeing in Grafana was created while there wasn't actually an active Span.

Not sure how things work with Python but with JS you'd use instrumentation-winston. You can put a breakpoint in the logHook callback function to make sure there is actually a Span running.

You could also try manually getting the tracer and wrapping the log.info() call in a tracer.startActiveSpan. Here's how it works in Typescript:

import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston';
import { Span } from '@opentelemetry/api';

// initTracer does all the otel tracer setup stuff and takes an optional param InstrumentationOption[]
initTracer([
  new WinstonInstrumentation({
    logHook: (span: Span) => {
      console.log(span) // <---- Put breakpoint here.
    },
  }),
]);

// manually start a span
trace
  .getTracer('my-service')
  .startActiveSpan('seedRootUser', async (span: Span) => {
    logger.info('This will have the Span\'s span_id and trace_id attached to the log data');
    span.end(); // make sure to call or I won't work!
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文