Skip to content

Latest commit

 

History

History
74 lines (53 loc) · 5.55 KB

OBSERVABILITY.md

File metadata and controls

74 lines (53 loc) · 5.55 KB

Observability

Distributed Tracing (W3C Trace Context)

dtctl uses the OpenTelemetry SDK to create and export traces. dtctl exports spans directly via OTLP before the process exits. W3C Trace Context headers (traceparent / tracestate) are propagated on every HTTP request to Dynatrace APIs, so CLI invocations can participate in existing distributed traces.

How it works

Scenario Behaviour
TRACEPARENT is not set dtctl creates a new root span with a random trace-id
TRACEPARENT is set dtctl inherits the trace-id and starts a child span — this invocation becomes part of the caller's trace
TRACESTATE is set forwarded as-is alongside traceparent
TRACEPARENT is malformed silently ignored per W3C spec; a new root span is created
OTEL_EXPORTER_OTLP_ENDPOINT is set spans are exported to that OTLP endpoint via HTTP/protobuf
OTEL_EXPORTER_OTLP_ENDPOINT not set trace context is still generated and forwarded (no network export)

A span is named by the command that ran, such as dtctl get workflows or dtctl query. Arguments (a query, an id, a path) never appear in span names, since spans go wherever the traces are exported.

Export spans to Dynatrace via OTLP

Set the standard OpenTelemetry environment variables. The Dynatrace OTLP ingest endpoint accepts spans directly:

export OTEL_EXPORTER_OTLP_ENDPOINT="https://{your-env-id}.live.dynatrace.com/api/v2/otlp"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Api-Token dt0c01.YOUR_TOKEN"
export OTEL_SERVICE_NAME="my-pipeline"   # optional, defaults to "dtctl"
dtctl get workflows

Token scopes required: openTelemetryTrace.ingest

Propagate from a CI/CD pipeline

Set TRACEPARENT from the pipeline's own trace context so dtctl calls appear as child spans in your build trace:

export TRACEPARENT="00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
dtctl get workflows   # carries the same trace-id, new child span-id

GitHub Actions example:

- name: Query workflows
  env:
    TRACEPARENT: ${{ env.PIPELINE_TRACEPARENT }}
    OTEL_EXPORTER_OTLP_ENDPOINT: https://${{ vars.DT_ENV_ID }}.live.dynatrace.com/api/v2/otlp
    OTEL_EXPORTER_OTLP_HEADERS: Authorization=Api-Token ${{ secrets.DT_OTLP_TOKEN }}
  run: dtctl get workflows

Environment variables

Variable Description
TRACEPARENT W3C traceparent to inherit (version-traceId-parentId-flags)
TRACESTATE W3C tracestate to forward alongside traceparent
OTEL_EXPORTER_OTLP_ENDPOINT OTLP HTTP endpoint to export spans to (e.g. Dynatrace ingest URL)
OTEL_EXPORTER_OTLP_HEADERS HTTP headers for the OTLP exporter, comma-separated key=value (e.g. Authorization=Api-Token ...)
OTEL_SERVICE_NAME Service name in the trace (default: dtctl)

All other standard OTEL_* environment variables (e.g. OTEL_RESOURCE_ATTRIBUTES) are also passed through to the OTel SDK automatically.

Embedding dtctl (pkg/engine)

A program that runs dtctl in its own process with engine.WithConcurrentExecution owns that process's telemetry:

  • Each invocation's span is a child of the span on the context passed to Execute, and is recorded by the tracer provider the program installed (otel.SetTracerProvider). dtctl installs nothing and reads none of the variables above.
  • Every request to the Dynatrace API carries the program's trace through the program's propagator (otel.SetTextMapPropagator), and has the invocation's span and the program's baggage on its context, so a transport instrumented with OpenTelemetry (passed with engine.WithTransport) records each call as a child of the invocation's span.
  • A failed invocation's span records only that it failed, not the error message, which can carry what the user typed or the API returned.
  • A program that installs no tracer provider gets no spans from dtctl, and one that installs no propagator sends no traceparent header.

A serialized engine (without WithConcurrentExecution) traces like the CLI, from the variables above. When each run ends it puts back the tracer provider, propagator and error handler the program had installed. A program that had installed none stays on OpenTelemetry's defaults, which bind once per process to the first ones set, so from then on they forward to the run's: its spans are still dropped, but its propagator sends traceparent and its error handler is dtctl's. Telemetry the program installs later still takes effect; to avoid the forwarding, install it before the first run.