注入traces数据中未显示在loki-grafana上
设置: 我正在使用 python-logging-loki 库来手动检测我的代码的日志。我有一个正在运行的 Loki 实例(未使用 promtail,我直接推送到 loki)。 在 Grafana 上,我启动并运行了 Jaeger 和 Loki 作为数据源。对于 Traces,我使用 opentelemetry python 库(也手动检测代码)。
我为派生字段配置了 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
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
What am i doing wrong? .. is this the correct way to use this Logging Instrumentation by otel? ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要在
[
和trace_id
之间放置一个空间吗?解析时,字段被空间分开,因此在您的日志中,字段名称可能最终以[trace_id
。Could it be that you need to put a space between
[
andtrace_id
? When parsing, fields are separated by a space, so in your log the field name might end up as[trace_id
.如果我不得不猜测,我会说您在Grafana中看到的日志条目是创建的,而实际上没有积极的跨度。
不确定情况如何与Python一起使用,但是与JS一起使用。您可以在
loghook
回调功能中放置一个断点,以确保实际上有一个跨度运行。您也可以尝试手动获取示踪剂并包装
log.info()
在tracer.startactivespan
中调用。这是它在打字稿中的工作方式: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 atracer.startActiveSpan
. Here's how it works in Typescript: