An alert fires: a service’s error rate is climbing. You find the slow trace, then hunt for the matching logs by hand, by timestamp, across three different pods. With OpenTelemetry logs, Dynatrace removes that step thanks to the trace_id: from a trace, you open the exact log lines it produced.
Goal of this tutorial: make every log carry the trace context (trace_id, span_id) through two paths — the OpenTelemetry SDK exporting logs over OTLP, and the Collector reading existing log files — then check the correlation. The content targets Dynatrace SaaS (logs on Grail); for Dynatrace Managed, OTLP log ingest also exists, but check the minimum version and the available screens in your cluster’s documentation.
This topic complements the OpenTelemetry Collector for Dynatrace and its deployment on Kubernetes.
Prerequisites
- An API token with the
logs.ingestscope (andopenTelemetryTrace.ingestfor traces). - An application already traced with OpenTelemetry. The example uses Python 3.10+; the principle is the same in Java, Go or .NET.
- For the file path: a contrib Collector (
filelogreceiver, renamedfile_login recent versions; the old name remains accepted as a deprecated alias). - OTLP endpoint:
https://<env-id>.live.dynatrace.com/api/v2/otlp(SaaS) orhttps://<your-domain>/e/<env-id>/api/v2/otlp(Managed).
OpenTelemetry logs: Dynatrace and trace_id correlation
An OpenTelemetry log is a structured record. The data model has two dedicated fields, TraceId and SpanId. When the log is emitted inside an active span, the SDK fills them automatically. Dynatrace then stores these identifiers with the log, so you can move from trace to log and back. Without these fields, correlation relies on time, which does not hold under load.
Remember: a log is not a span. Do not emit span events to replace logs, or the other way round.
Path 1: export logs from the SDK
Step 1: install the packages
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-http Step 2: plug in the log handler
import logging from opentelemetry import trace from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter from opentelemetry._logs import set_logger_provider from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler from opentelemetry.sdk._logs.export import BatchLogRecordProcessor from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter resource = Resource.create({"service.name": "checkout"}) tp = TracerProvider(resource=resource) tp.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) trace.set_tracer_provider(tp) lp = LoggerProvider(resource=resource) lp.add_log_record_processor(BatchLogRecordProcessor(OTLPLogExporter())) set_logger_provider(lp) logging.getLogger().addHandler(LoggingHandler(level=logging.INFO, logger_provider=lp)) logging.getLogger().setLevel(logging.INFO) tracer = trace.get_tracer("checkout") log = logging.getLogger("checkout") with tracer.start_as_current_span("pay-order"): log.warning("payment declined", extra={"order.id": "A-1042"}) The critical lines:
- The same
Resourcefor traces and logs: Dynatrace attaches both to the same service. -
LoggingHandlerconverts eachloggingrecord into an OpenTelemetry log, with the active span’strace_id. - The
_logsmodules have a leading underscore: the Python SDK logs API is still marked experimental. Since opentelemetry-sdk 1.40,LoggingHandleris also deprecated in favour of theopentelemetry-instrumentation-loggingpackage (LoggingInstrumentor); the code above still works, but pin your versions and plan the migration. -
extrabecomes log attributes: keep keys stable and few.
Step 3: configure export with environment variables
export OTEL_EXPORTER_OTLP_ENDPOINT="https://<env-id>.live.dynatrace.com/api/v2/otlp" export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Api-Token%20dt0c01.XXXX" export OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf" The space after Api-Token is encoded as %20. The exporters append /v1/traces and /v1/logs to the endpoint themselves. In production, point to a local Collector instead.
Path 2: read log files with the Collector
Many applications already write JSON logs to standard output. If they include the trace_id (for example through the logging instrumentation, which adds otelTraceID to the format), the Collector can promote it to a trace field.
receivers: filelog: include: [/var/log/pods/*/*/*.log] include_file_path: true operators: - type: container # strips the runtime envelope - type: json_parser parse_from: body parse_to: attributes - type: trace_parser trace_id: parse_from: attributes.trace_id span_id: parse_from: attributes.span_id processors: batch: {} exporters: otlphttp/dynatrace: endpoint: ${env:DT_ENDPOINT} headers: Authorization: "Api-Token ${env:DT_API_TOKEN}" service: pipelines: logs: receivers: [filelog] processors: [batch] exporters: [otlphttp/dynatrace] -
container: the operator recognises CRI and Docker formats. It does not exist in older Collector versions; use the parsing operators described in the receiver docs instead. -
trace_parser: copies the attribute into the OTel model’sTraceId/SpanIdfields. This is what makes the correlation native. - Adapt
attributes.trace_idto the real field name in your logs (traceId,otelTraceID…).
On Kubernetes, add k8sattributes to get pod and namespace back. If you prefer to transform logs on the Dynatrace side, self-service log ingest and OpenPipeline offer an alternative.
How to check that it works
-
On the Collector side, look for export errors:
kubectl -n observability logs ds/otel-node-opentelemetry-collector | grep -iE "logs|error|401|413"A
413means the batch is too large: reducesend_batch_size. -
On SaaS, in a Notebook:
fetch logs, from: now() - 30m | filter isNotNull(trace_id) | summarize logs = count(), by: {service.name, loglevel}The exact name of the trace fields on logs can vary by source; inspect one record with
fetch logs | limit 1before filtering. -
Take a
trace_idyou found, then:
fetch spans, from: now() - 30m | filter trace.id == "<trace_id>" | fields start_time, span.name, durationIf the trace and the logs come back with the same identifier, the correlation is in place. In the UI, the trace view should also offer the related logs.
On Managed, open the log viewer, filter on the service and check that the log shows a link to the trace. The exact location depends on the version: refer to docs.dynatrace.com.
Best practices
- One path per log: if the application exports over OTLP, do not also read its file, or every line arrives twice.
- INFO level minimum in production: log volume is often the largest cost item.
- No personal data in the body or attributes; mask it in the Collector or in OpenPipeline.
- Structured logs: stable JSON is parsed once and for all and reduces toil.
- Consistent sampling: if you sample traces, some logs will point to a missing trace. That is expected; keep all error logs.
Frequently asked questions
Does OneAgent already do this correlation?
For the technologies it instruments, OneAgent can enrich logs with trace context. The OpenTelemetry path is for when OneAgent is not present or when you standardise on OTel.
Why do my logs arrive without a trace_id?
The log was emitted outside an active span (startup, background task), or parsing did not find the field. Check the field name and the execution context.
Should logs be sent over gRPC or HTTP?
The Dynatrace OTLP API accepts HTTP with protobuf. Use otlphttp towards Dynatrace; gRPC remains possible between your applications and the Collector.
Conclusion
OpenTelemetry logs that carry their trace_id save precious minutes in every incident: the trace shows where, the log explains why. Start with one critical service, check the join in DQL, then extend to your other workloads. Next, tie these signals to your objectives with OpenTelemetry SLOs in DQL.
span.observer is an independent community blog, not affiliated with Dynatrace LLC. Dynatrace is a trademark of Dynatrace LLC. Fields and screens change: check them in the official documentation.
Originally published on span.observer. French version: https://span.observer/fr/logs-opentelemetry-dynatrace-trace-id/.