pub struct Telemetry { /* private fields */ }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
impl Telemetry
Sourcepub fn layer<S>(&self) -> impl Layer<S> + Send + Sync + 'static
Available on crate feature otel only.
pub fn layer<S>(&self) -> impl Layer<S> + Send + Sync + 'static
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.
Sourcepub fn install_subscriber(&self)
Available on crate feature otel only.
pub fn install_subscriber(&self)
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.
Sourcepub fn try_install_subscriber(&self) -> bool
Available on crate feature otel only.
pub fn try_install_subscriber(&self) -> bool
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
impl Telemetry
Sourcepub fn builder() -> TelemetryBuilder
Available on crate feature otel only.
pub fn builder() -> TelemetryBuilder
otel only.Configuration with every default; see TelemetryBuilder.
Sourcepub fn init() -> Result<Telemetry, TelemetryError>
Available on crate feature otel only.
pub fn init() -> Result<Telemetry, TelemetryError>
otel only.Every signal over OTLP, configured by the environment alone: the same
as Telemetry::builder followed by TelemetryBuilder::build.
§Errors
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.
Sourcepub fn init_where_configured() -> Result<Telemetry, TelemetryError>
Available on crate feature otel only.
pub fn init_where_configured() -> Result<Telemetry, TelemetryError>
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
Sourcepub fn otel_metrics(&self) -> OtelMetrics
Available on crate feature otel only.
pub fn otel_metrics(&self) -> OtelMetrics
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.
Sourcepub const fn tracer_provider(&self) -> Option<&SdkTracerProvider>
Available on crate feature otel only.
pub const fn tracer_provider(&self) -> Option<&SdkTracerProvider>
otel only.The tracer provider, when traces are exported.
Sourcepub const fn meter_provider(&self) -> Option<&SdkMeterProvider>
Available on crate feature otel only.
pub const fn meter_provider(&self) -> Option<&SdkMeterProvider>
otel only.The meter provider, when metrics are exported.
Sourcepub const fn logger_provider(&self) -> Option<&SdkLoggerProvider>
Available on crate feature otel only.
pub const fn logger_provider(&self) -> Option<&SdkLoggerProvider>
otel only.The logger provider, when logs are exported.
Sourcepub const fn prometheus_addr(&self) -> Option<SocketAddr>
Available on crate features otel-prometheus and otel only.
pub const fn prometheus_addr(&self) -> Option<SocketAddr>
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.
Sourcepub fn prometheus(&self) -> Option<PrometheusScrape>
Available on crate features otel-prometheus and otel only.
pub fn prometheus(&self) -> Option<PrometheusScrape>
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.
Sourcepub fn notices(&self) -> &[String]
Available on crate feature otel only.
pub fn notices(&self) -> &[String]
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.
Sourcepub fn describe(&self) -> String
Available on crate feature otel only.
pub fn describe(&self) -> String
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.
Sourcepub fn force_flush(&self) -> Result<(), TelemetryError>
Available on crate feature otel only.
pub fn force_flush(&self) -> Result<(), TelemetryError>
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.
Sourcepub fn shutdown(self) -> Result<(), TelemetryError>
Available on crate feature otel only.
pub fn shutdown(self) -> Result<(), TelemetryError>
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§
Auto Trait Implementations§
impl !RefUnwindSafe for Telemetry
impl !UnwindSafe for Telemetry
impl Forget for Telemetry
impl Freeze for Telemetry
impl Move for Telemetry
impl Send for Telemetry
impl Sync for Telemetry
impl Unpin for Telemetry
impl UnsafeUnpin for Telemetry
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].