Skip to main content

Telemetry

Struct Telemetry 

Source
pub struct Telemetry { /* private fields */ }
Available on crate feature otlp only.
Expand description

Exporting providers for traces, metrics and logs, and the guard that flushes them. Built by Telemetry::init or Telemetry::builder.

Dropping it flushes and shuts every provider down, best-effort; shutdown does the same and reports what it can see failed. Either is safe on any thread, inside a Tokio runtime of either flavour or outside one, and may block while a collector is slow: each of the three providers is given the SDK’s 5 s shutdown timeout (opentelemetry_sdk 0.33.1), one after the other. Measured on hello-agent after SIGTERM (debug build, one run each): 0.1 s to a collector that answers, 1.8 s to a port that refuses, 15.0 s to one that accepts the connection and never answers, 4.1 s to that one with OTEL_EXPORTER_OTLP_TIMEOUT=2000.

#[must_use]: Telemetry::init()?; alone on a line drops the guard at once, which shuts every exporter down before anything is recorded (audit G-14). Keep it for as long as the process should export.

Implementations§

Source§

impl Telemetry

Source

pub fn layer<S>(&self) -> impl Layer<S> + Send + Sync + 'static
where S: Subscriber + for<'a> LookupSpan<'a> + Send + Sync,

Available on crate feature otel only.

The tracing layer: spans become OpenTelemetry spans, and events at or above the log level become log records. Add it to the application’s subscriber, next to any other layers. A signal that is off contributes nothing.

Source

pub fn install_subscriber(&self)

Available on crate feature otel only.

Installs layer as the process’s global tracing subscriber, with a layer that prints warnings and errors to standard error and logs describe once — what a binary with no subscriber of its own needs. Without the printing layer a process whose export is failing, or that was never given a collector, shows nothing at all.

RUST_LOG sets what is printed, in target=level form (default: warn everywhere, info for this module’s own startup line and for the application’s own crate, named after the default service name with - as _: with_default_service_name("deploy-agent") prints deploy_agent’s INFO lines). Until 2026-10-29 an application’s INFO lines (where it listens, which database it opened) were hidden unless RUST_LOG named its crate.

Under OTEL_SDK_DISABLED=true nothing is exported (layer() adds nothing), but the printing layer is still installed: the variable turns off the OpenTelemetry SDK, and says nothing about a process’s own warnings and errors. Until 2026-10-31 it installed nothing, so a process told to export nothing also lost every warning, its own and the SDK’s (audit C-07). It never replaces a global subscriber that was already set; try_install_subscriber says whether it installed one. A process with its own subscriber adds layer() to it instead.

Source

pub fn try_install_subscriber(&self) -> bool

Available on crate feature otel only.

install_subscriber, saying whether it installed the subscriber: false when a global subscriber was already set. Until 0.16.0 install_subscriber returned this, which every caller but the tests ignored.

Source§

impl Telemetry

Source

pub fn builder() -> TelemetryBuilder

Available on crate feature otel only.

Configuration with every default; see TelemetryBuilder.

Source

pub fn init() -> Result<Telemetry, TelemetryError>

Available on crate feature otel only.

Every signal over OTLP, configured by the environment alone: the same as Telemetry::builder followed by TelemetryBuilder::build.

§Errors

As TelemetryBuilder::build.

Without an endpoint in the environment, every signal is exported to localhost (4318, or 4317 over gRPC), as the OpenTelemetry environment specification has it — what a deployment with a collector beside it (a sidecar) and no OTEL_* variable relies on. A binary that must run as well without a collector wants init_where_configured.

Source

pub fn init_where_configured() -> Result<Telemetry, TelemetryError>

Available on crate feature otel only.

Like init, but a signal is built only where an endpoint is configured for it: the same as Telemetry::builder followed by TelemetryBuilder::with_export_where_configured and TelemetryBuilder::build. With no OTEL_EXPORTER_OTLP_*ENDPOINT set, nothing is exported and no export thread is started.

§Errors

As TelemetryBuilder::build.

Source

pub fn otel_metrics(&self) -> OtelMetrics

Available on crate feature otel only.

An OtelMetrics recording through this telemetry’s meter provider, for RequestHandlerBuilder::with_metrics. With metrics off it records into a no-op meter.

Source

pub const fn tracer_provider(&self) -> Option<&SdkTracerProvider>

Available on crate feature otel only.

The tracer provider, when traces are exported.

Source

pub const fn meter_provider(&self) -> Option<&SdkMeterProvider>

Available on crate feature otel only.

The meter provider, when metrics are exported.

Source

pub const fn logger_provider(&self) -> Option<&SdkLoggerProvider>

Available on crate feature otel only.

The logger provider, when logs are exported.

Source

pub const fn prometheus_addr(&self) -> Option<SocketAddr>

Available on crate features otel-prometheus and otel only.

The address /metrics is served on under OTEL_METRICS_EXPORTER=prometheus — the port the operating system chose when OTEL_EXPORTER_PROMETHEUS_PORT is 0.

Source

pub fn prometheus(&self) -> Option<PrometheusScrape>

Available on crate features otel-prometheus and otel only.

Under OTEL_METRICS_EXPORTER=prometheus, a handle that renders the metrics in the Prometheus text format, for the application to serve at /metrics on its own port, behind its own authentication; with TelemetryBuilder::without_prometheus_listener it is the only way out (audit C-16, ADR 0038 §3). None when that exporter is not chosen.

Source

pub fn notices(&self) -> &[String]

Available on crate feature otel only.

What the environment or the code asked for that this SDK does not do, and went on without: an exporter name, protocol or compression it does not have, a timeout or port it could not read, a propagator it does not have, an OTEL_SDK_DISABLED that is not true or false. Each line names the variable and its value and says what was used instead. Each is also logged at warn by install_subscriber; an application with its own subscriber reads them here. Until 2026-11-01 most of these stopped build.

Source

pub fn describe(&self) -> String

Available on crate feature otel only.

Where each signal goes, in one line: the transport, the endpoint (with any login removed), gzip, the proxy it is sent through, or off; or why nothing is exported at all. install_subscriber logs it at startup; a binary with its own subscriber can log it too.

Source

pub fn force_flush(&self) -> Result<(), TelemetryError>

Available on crate feature otel only.

Exports everything recorded so far, without shutting down.

§Errors

TelemetryError::Export naming each signal whose flush failed or timed out, or whose exports were lost since the last report — see shutdown.

Source

pub fn shutdown(self) -> Result<(), TelemetryError>

Available on crate feature otel only.

Flushes and shuts down every provider, then the export runtime.

§Errors

TelemetryError::Export naming each signal whose shutdown failed or timed out, or whose exports were lost since the last report, on either transport: an export the exporter gave up on after its retries (opentelemetry-otlp makes four attempts by default), whose batch the SDK then dropped. An attempt refused and then retried successfully is not a loss. Termination should not be blocked on it.

The SDK’s batch processors log a lost export and their shutdown returns Ok regardless (opentelemetry_sdk 0.33.1, trace/span_processor.rs); the count is this crate’s.

Trait Implementations§

Source§

impl Debug for Telemetry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Drop for Telemetry

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more