Skip to main content

init_otlp_pipeline

Function init_otlp_pipeline 

Source
pub fn init_otlp_pipeline(
    service_name: Option<&str>,
) -> Result<SdkMeterProvider, Box<dyn Error>>
👎Deprecated since 0.16.0:

use Telemetry::builder().with_traces(false).with_logs(false).with_otlp_protocol(OtlpProtocol::Grpc).build(): it adds service.instance.id, OTEL_SDK_DISABLED, TLS, proxies and lost-export counts; the book’s Telemetry page has the migration

Available on crate feature otel only.
Expand description

Initialise an OTLP metrics export pipeline and install it as the global MeterProvider.

The endpoint defaults to http://localhost:4317 and can be overridden via the OTEL_EXPORTER_OTLP_ENDPOINT environment variable.

Returns the [SdkMeterProvider] so the caller can hold onto it and call [SdkMeterProvider::shutdown] during graceful termination.

§Arguments

  • service_name — the service.name resource attribute when the environment names none. OTEL_SERVICE_NAME, or service.name in OTEL_RESOURCE_ATTRIBUTES, wins over it, as the OpenTelemetry environment-variable specification prescribes; None leaves the name to the environment, and to the SDK’s unknown_service default without one. Until 0.15 the argument was a &str that overwrote the environment.

§This is gRPC only

A behaviour that surprises anyone arriving with an OpenTelemetry deployment already configured; a current limitation, not a decision the specification supports:

  • The transport is gRPC (OTLP/gRPC, port 4317) and cannot be changed. MetricExporter::builder().with_tonic() is hard-coded here, though the crate also compiles opentelemetry-otlp’s HTTP/protobuf exporter, which Telemetry uses. OTEL_EXPORTER_OTLP_PROTOCOL has no effect. There is no TLS either: the channel is tonic’s own, built with no TLS feature, so an https:// collector cannot be reached. Setting it to http/protobuf and pointing OTEL_EXPORTER_OTLP_ENDPOINT at a collector’s :4318 produces gRPC spoken at an HTTP port, and silence.

Only the endpoint, headers and timeout variables reach the exporter.

§Only metrics — there is no span export

This pipeline exports metrics and nothing else: it installs no TracerProvider and exports no spans. Telemetry (feature otlp) exports the spans the SDK records, with metrics and logs, and is what new code should use.

Since 0.13 the SDK also propagates W3C trace context: the server parses an inbound traceparent into RequestContext::trace_context, and a2a-protocol-client’s TracePropagationInterceptor writes one on the way out, so a delegation chain shares one trace id across agents.

§Errors

Returns an error if the OTLP exporter or meter provider cannot be created.

§Panics

Must be called from within a Tokio runtime. The tonic OTLP channel is constructed here, and tonic spawns onto the ambient runtime while building it; called outside one, this panics with there is no reactor running, must be called from the context of a Tokio 1.x runtime rather than returning Err. In practice that means calling it inside #[tokio::main] / #[tokio::test], or within a Runtime::enter guard — not from a plain fn main before the runtime is started.

The panic originates inside tonic/hyper-util, so no lint in this workspace can catch the misuse at compile time; it was found by adding the first test to ever call this function (tests/otel_pipeline_tests.rs).

§Global state

This installs a process-global MeterProvider, and is last-write-wins: calling it twice replaces the first provider, silently orphaning it.

§Shutdown behaviour

[SdkMeterProvider::shutdown] performs a final export flush. If metrics have been recorded and the collector is unreachable, that flush fails — observed as InternalFailure("[Timeout(5s)]"). Graceful-termination code should therefore treat a shutdown error as “metrics may have been lost”, not as a fatal condition, and should not block termination on it.