-
Notifications
You must be signed in to change notification settings - Fork 637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Span/Trace ID is always zero in log files when running with Django #821
Comments
I've made some progress by moving the initialisation code out of This gets me the traceID's into the logs and they are showing up in Loki, however it only seems to be for errors and warnings, not for successful responses:
|
How are you setting up your tracing pipeline? I can't seem to find it in your sample. |
This could well be where I'm going wrong then! 🤣 I'm using the OTLP HTTP exporter sending directly to Grafana's Tempo over a TLS Connection for the traces, and using the Django Loki Logging module to send the logs directly to Grafana's Loki. I'm then trying to stitch it all together in Grafana, but because the traceID in the log is As an example, I have this in the logs for a 404: but that trace doesn't exist in Tempo even though it exists in the Context. I've got be honest, whilst I'm pretty aufait with Python, I'm not a pro by any means when it comes to Django, so I may well be holding this upside down and back to front! |
FWIW, I've gone back to flask and stripped everything back to a single file just to understand where I might be going wrong. The app, env vars, and requirements.txt are all available in this Gist, and the result is exactly the same - the Trace and Span ID are |
OK, I've stripped this down even further, based it on the Getting Started Guide and I'm still not getting trace ID's in the logs: # flask_example.py
import flask
import requests
from opentelemetry import trace
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
)
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
trace.set_tracer_provider(TracerProvider())
span_processor = BatchSpanProcessor(OTLPSpanExporter(endpoint="https://otlphttp.service.wallace.network/v1/traces"))
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(ConsoleSpanExporter())
)
trace.get_tracer_provider().add_span_processor(
span_processor
)
app = flask.Flask(__name__)
LoggingInstrumentor().instrument(set_logging_format=True)
FlaskInstrumentor().instrument_app(app)
RequestsInstrumentor().instrument()
tracer = trace.get_tracer(__name__)
@app.route("/")
def hello():
with tracer.start_as_current_span("example-request"):
requests.get("http://www.example.com")
return "hello"
app.run(port=5000) The trace ID's are echoed to the console but are not in the application logs: (otel-test) [mmw@rincewind otel-test]$ python newapp.py
* Serving Flask app 'newapp' (lazy loading)
* Environment: dev
* Debug mode: off
2021-12-07 17:36:03,874 INFO [werkzeug] [_internal.py:225] [trace_id=0 span_id=0 resource.service.name=unknown_service] - * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
2021-12-07 17:36:13,514 INFO [werkzeug] [_internal.py:225] [trace_id=0 span_id=0 resource.service.name=unknown_service] - 127.0.0.1 - - [07/Dec/2021 17:36:13] "GET / HTTP/1.1" 200 -
{
"name": "HTTP GET",
"context": {
"trace_id": "0xfc221152991c56a4c9c8cd08bc6526c3",
"span_id": "0x13b69c2d3be73e43",
"trace_state": "[]"
},
"kind": "SpanKind.CLIENT",
"parent_id": "0x7cde6ec7ec31f68f",
"start_time": "2021-12-07T17:36:13.338285Z",
"end_time": "2021-12-07T17:36:13.513855Z",
"status": {
"status_code": "UNSET"
},
"attributes": {
"http.method": "GET",
"http.url": "http://www.example.com",
"http.status_code": 200
},
"events": [],
"links": [],
"resource": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.7.1",
"service.name": "unknown_service"
}
}
{
"name": "example-request",
"context": {
"trace_id": "0xfc221152991c56a4c9c8cd08bc6526c3",
"span_id": "0x7cde6ec7ec31f68f",
"trace_state": "[]"
},
"kind": "SpanKind.INTERNAL",
"parent_id": "0x44df0ee87aedd45e",
"start_time": "2021-12-07T17:36:13.338130Z",
"end_time": "2021-12-07T17:36:13.514149Z",
"status": {
"status_code": "UNSET"
},
"attributes": {},
"events": [],
"links": [],
"resource": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.7.1",
"service.name": "unknown_service"
}
}
{
"name": "/",
"context": {
"trace_id": "0xfc221152991c56a4c9c8cd08bc6526c3",
"span_id": "0x44df0ee87aedd45e",
"trace_state": "[]"
},
"kind": "SpanKind.SERVER",
"parent_id": null,
"start_time": "2021-12-07T17:36:13.337428Z",
"end_time": "2021-12-07T17:36:13.514632Z",
"status": {
"status_code": "UNSET"
},
"attributes": {
"http.method": "GET",
"http.server_name": "127.0.0.1",
"http.scheme": "http",
"net.host.port": 5000,
"http.host": "localhost:5000",
"http.target": "/",
"net.peer.ip": "127.0.0.1",
"http.user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
"net.peer.port": 50356,
"http.flavor": "1.1",
"http.route": "/",
"http.status_code": 200
},
"events": [],
"links": [],
"resource": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.7.1",
"service.name": "unknown_service"
}
} and yet the traces are being sent correctly to Tempo: |
Taking a look at your most recent sample code, it looks like the log messages produced are originating from the The logging you actually want is to be done in actual spans in your code. So if you modify your code like this: @app.route("/")
def hello():
with tracer.start_as_current_span("example-request"):
requests.get("http://www.example.com")
logging.warning("Log message in span")
return "hello" You should be able to get a log message with trace_id/span_id corresponding to the current span context (the one generated from |
Yup, this works perfectly, thanks @lzchen! |
Describe your environment
Running github.com/makemonmouth/mventory either via
manage.py
oruwsgi
does not produce correct trace/span ID's.Steps to reproduce
pip install -r requirements.txt
./manage.py runserver
What is the expected behavior?
Trace and Span ID's showing up in the logs
What is the actual behavior?
Additional context
I'm trying to get OpenTelemetry traces to be sent to Tempo, with the trace/span ID's being logged in Loki so I can lookup and compare the values.
If I add the following code to
manage.py
, then the Span/Trace ID's are printed to STDOUT but do not appear in the logs:Setup code is at https://github.com/MakeMonmouth/mventory/blob/feature/opentelemetry/manage.py#L6-L41 but may be in the wrong place, I wasn't sure from the docs
Logging settings are at https://github.com/MakeMonmouth/mventory/blob/feature/opentelemetry/mventory/settings.py#L161-L198
The text was updated successfully, but these errors were encountered: