A set of helpers to initialize (and more) tracing + opentelemetry (compose your own or use opinionated preset)
```txt //... use axumtracingopentelemetry::opentelemetrytracinglayer;
async fn main() -> Result<(), axum::BoxError> { // very opinionated init of tracing, look as is source to compose your own inittracingopentelemetry::tracingsubscriberext::init_subscribers()?;
...;
Ok(())
} ```
AND Call opentelemetry_api::global::shutdown_tracer_provider();
on shutdown of the app to be sure to send the pending trace,...
To configure opentelemetry tracer & tracing, you can use the functions from init_tracing_opentelemetry::tracing_subscriber_ext
, but they are very opinionated (and WIP to make them more customizable and friendly), so we recommend making your composition, but look at the code (to avoid some issue) and share your feedback.
``txt
pub fn build_loglevel_filter_layer() -> tracing_subscriber::filter::EnvFilter {
// filter what is output on log (fmt)
// std::env::set_var("RUST_LOG", "warn,axum_tracing_opentelemetry=info,otel=debug");
std::env::set_var(
"RUST_LOG",
format!(
//
otel::tracingshould be a level trace to emit opentelemetry trace & span
//
otel::setup` set to debug to log detected resources, configuration read and infered
"{},otel::tracing=trace,otel=debug",
std::env::var("RUSTLOG")
.orelse(|| std::env::var("OTELLOGLEVEL"))
.unwraporelse(|| "info".tostring())
),
);
EnvFilter::fromdefault_env()
}
pub fn buildotellayer() -> Resultaxum_tracing_opentelemetry::stdio::WriteNoWhere::default()
// or to std::io::stdout()
to print
//
// let oteltracer =
// stdio::inittracer(otelrsrc, stdio::identity, stdio::WriteNoWhere::default())?;
initpropagator()?;
Ok(tracingopentelemetry::layer().withtracer(oteltracer))
}
```
To retrieve the current trace_id
(eg to add it into error message (as header or attributes))
```rust # use tracingopentelemetryinstrumentation_sdk;
let traceid = tracingopentelemetryinstrumentationsdk::findcurrenttraceid(); //json!({ "error" : "xxxxxx", "traceid": trace_id}) ```
To ease setup and compliance with OpenTelemetry SDK configuration, the configuration can be done with the following environment variables (see sample init_tracing()
above):
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
fallback to OTEL_EXPORTER_OTLP_ENDPOINT
for the url of the exporter / collectorOTEL_EXPORTER_OTLP_TRACES_PROTOCOL
fallback to OTEL_EXPORTER_OTLP_PROTOCOL
, fallback to auto-detection based on ENDPOINT portOTEL_SERVICE_NAME
for the name of the serviceOTEL_PROPAGATORS
for the configuration of the propagatorsOTEL_TRACES_SAMPLER
& OTEL_TRACES_SAMPLER_ARG
for configuration of the samplerIn the context of kubernetes, the above environment variable can be injected by the Opentelemetry operator (via inject-sdk
):
yaml
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
yaml
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