axum-tracing-opentelemetry
Middlewares and tools to integrate axum + tracing + opentelemetry.
- Read OpenTelemetry header from incoming request
- Start a new trace if no trace found in the incoming request
- Trace is attached into tracing'span
For examples, you can look at:
- the examples folder
- davidB/sandbox_axum_observability: Sandbox to experiment axum and observability. This example shows also propagation of the trace between tracing span and service (via reqwest).
//...
use opentelemetry_tracing_layer;
async
async
To retrieve the current trace_id (eg to add it into error message (as header or attributes))
let trace_id = find_current_trace_id;
json!
To also inject the trace id into the response (could be useful for debugging) uses the layer response_with_trace_layer
// build our application with a route
new
...
// include trace context as header into the response
.layer
Configuration based on environment variable
To ease setup and compliancy with Opentelemetry SDK configuration, the configuration can be done with the following environment variables (see sample init_tracing() above):
OTEL_EXPORTER_OTLP_TRACES_ENDPOINTfallback toOTEL_EXPORTER_OTLP_ENDPOINTfor the url of the exporter / collectorOTEL_EXPORTER_OTLP_TRACES_PROTOCOLfallback toOTEL_EXPORTER_OTLP_PROTOCOL, fallback to autodetection based on ENDPOINT portOTEL_SERVICE_NAMEfor the name of the serviceOTEL_PROPAGATORSfor the configuration of propagator
In the context of kubernetes, the above environment variable can be injected by the Opentelemetry operator (via inject-sdk):
apiVersion: apps/v1
kind: Deployment
spec:
template:
metadata:
annotations:
# to inject environment variables only by opentelemetry-operator
instrumentation.opentelemetry.io/inject-sdk: "opentelemetry-operator/instrumentation"
instrumentation.opentelemetry.io/container-names: "app"
containers:
- name: app
Or if you don't setup inject-sdk, you can manually set the environment variable eg
apiVersion: apps/v1
kind: Deployment
spec:
template:
metadata:
containers:
- name: app
env:
- name: OTEL_SERVICE_NAME
value: "app"
- name: OTEL_EXPORTER_OTLP_PROTOCOL
value: "grpc"
# for otel collector in `deployment` mode, use the name of the service
# - name: OTEL_EXPORTER_OTLP_ENDPOINT
# value: "http://opentelemetry-collector.opentelemetry-collector:4317"
# for otel collector in sidecar mode (imply to deploy a sidecar CR per namespace)
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: "http://localhost:4317"
# for `daemonset` mode: need to use the local daemonset (value interpolated by k8s: `$(...)`)
# - name: OTEL_EXPORTER_OTLP_ENDPOINT
# value: "http://$(HOST_IP):4317"
# - name: HOST_IP
# valueFrom:
# fieldRef:
# fieldPath: status.hostIP
examples/otlp
In a terminal, run
)
)
)
)
Into an other terminal, call the / (endpoint with opentelemetry_tracing_layer and response_with_trace_layer)
}
call the /health (endpoint with NO layer)
}
Compatibility
| axum | axum-tracing-opentelemetry |
|---|---|
| 0.6 | latest - 0.6 |
| 0.5 | 0.1 - 0.5 |
History
0.8
-
add
init_propagatorto configure the global propagator based on content of the env variable OTEL_PROPAGATORS -
add a layer
response_with_trace_layerto havetraceparentinjected into response -
improve discovery of otlp configuration based on
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,OTEL_EXPORTER_OTLP_ENDPOINT,OTEL_EXPORTER_OTLP_TRACES_PROTOCOL,OTEL_EXPORTER_OTLP_PROTOCOL
0.6
- upgrade to axum 0.6
0.5
- upgrade to opentelemetry 0.18
- breaking change: upgrade opentelemetry-jaeger to 0.17 (switch from PipelineBuiler to AgentPipeline)
0.4
- allow customization of tracer
- add tracer to export on stdout or stderr
- add tracer to export to nowhere (like
/dev/null) to allow to have trace_id and the opentelemetry span & metadata on log and http response (without collector)
0.3
- Allow customization of exporter pipeline
- Fix name of the root span (#6)
0.2
- First public release as a crate
0.1
- Code originally created at part of axum-extra Add OpenTelemetry middleware by davidpdrsn · Pull Request #769 · tokio-rs/axum
- Code copied and modified as part of davidB/sandbox_axum_observability: Sandbox to experiment axum and observability
- Published as a standalone crate with OK from original author