pub struct PeriodicReader { /* private fields */ }
Expand description
A MetricReader
that periodically collects and exports metrics at a configurable interval.
By default, PeriodicReader
collects and exports metrics every 60 seconds.
The time taken for export is not included in the interval. Use PeriodicReaderBuilder
to customize the interval.
PeriodicReader
spawns a background thread to handle metric collection and export.
This thread remains active until shutdown()
is called.
§Collection Process
“Collection” refers to gathering aggregated metrics from the SDK’s internal storage. During this phase, callbacks from observable instruments are also triggered.
PeriodicReader
does not enforce a timeout for collection. If an
observable callback takes too long, it may delay the next collection cycle.
If a callback never returns, it will stall all metric collection (and exports)
indefinitely.
§Exporter Compatibility
When used with the OTLP Exporter
, the following
transport options are supported:
grpc-tonic
: RequiresMeterProvider
to be initialized within atokio
runtime.reqwest-blocking-client
: Works with both a standard (main
) function andtokio::main
.
PeriodicReader
does not enforce a timeout for exports either. Instead,
the configured exporter is responsible for enforcing timeouts. If an export operation
never returns, PeriodicReader
will stop exporting new metrics, stalling
metric collection.
§Manual Export & Shutdown
Users can manually trigger an export via force_flush()
. Calling shutdown()
exports any remaining metrics and should be done before application exit to ensure
all data is sent.
Warning: If using tokio’s current-thread runtime, calling shutdown()
from the main thread may cause a deadlock. To prevent this, call shutdown()
from a separate thread or use tokio’s spawn_blocking
.
§Example
use opentelemetry_sdk::metrics::PeriodicReader;
let exporter = get_exporter(); // set up a push exporter
let reader = PeriodicReader::builder(exporter).build();
Implementations§
Source§impl PeriodicReader
impl PeriodicReader
Sourcepub fn builder<E>(exporter: E) -> PeriodicReaderBuilder<E>where
E: PushMetricExporter,
pub fn builder<E>(exporter: E) -> PeriodicReaderBuilder<E>where
E: PushMetricExporter,
Configuration options for a periodic reader with own thread
Trait Implementations§
Source§impl Clone for PeriodicReader
impl Clone for PeriodicReader
Source§fn clone(&self) -> PeriodicReader
fn clone(&self) -> PeriodicReader
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for PeriodicReader
impl Debug for PeriodicReader
Source§impl MetricReader for PeriodicReader
impl MetricReader for PeriodicReader
Source§fn temporality(&self, kind: InstrumentKind) -> Temporality
fn temporality(&self, kind: InstrumentKind) -> Temporality
To construct a MetricReader when setting up an SDK, The output temporality (optional), a function of instrument kind. This function SHOULD be obtained from the exporter.
If not configured, the Cumulative temporality SHOULD be used.