opentelemetry_spanprocessor_any/metrics/sdk_api.rs
1//! Metrics SDK API
2use crate::metrics::{AsyncRunner, Descriptor, Measurement, Number, Result};
3use crate::{Context, KeyValue};
4use std::any::Any;
5use std::fmt;
6use std::sync::Arc;
7
8/// The interface an SDK must implement to supply a Meter implementation.
9pub trait MeterCore: fmt::Debug {
10 /// Atomically record a batch of measurements.
11 fn record_batch_with_context(
12 &self,
13 cx: &Context,
14 attributes: &[KeyValue],
15 measurements: Vec<Measurement>,
16 );
17
18 /// Create a new synchronous instrument implementation.
19 fn new_sync_instrument(&self, descriptor: Descriptor) -> Result<Arc<dyn SyncInstrumentCore>>;
20
21 /// Create a new asynchronous instrument implementation.
22 ///
23 /// Runner is `None` if used in batch as the batch runner is registered separately.
24 fn new_async_instrument(
25 &self,
26 descriptor: Descriptor,
27 runner: Option<AsyncRunner>,
28 ) -> Result<Arc<dyn AsyncInstrumentCore>>;
29
30 /// Register a batch observer
31 fn new_batch_observer(&self, runner: AsyncRunner) -> Result<()>;
32}
33
34/// A common interface for synchronous and asynchronous instruments.
35pub trait InstrumentCore: fmt::Debug + Send + Sync {
36 /// Description of the instrument's descriptor
37 fn descriptor(&self) -> &Descriptor;
38}
39
40/// The implementation-level interface to a generic synchronous instrument
41/// (e.g., ValueRecorder and Counter instruments).
42pub trait SyncInstrumentCore: InstrumentCore {
43 /// Creates an implementation-level bound instrument, binding an attribute set
44 /// with this instrument implementation.
45 fn bind(&self, attributes: &'_ [KeyValue]) -> Arc<dyn SyncBoundInstrumentCore>;
46
47 /// Capture a single synchronous metric event.
48 fn record_one(&self, number: Number, attributes: &'_ [KeyValue]);
49
50 /// Returns self as any
51 fn as_any(&self) -> &dyn Any;
52}
53
54/// The implementation-level interface to a generic synchronous bound instrument
55pub trait SyncBoundInstrumentCore: fmt::Debug + Send + Sync {
56 /// Capture a single synchronous metric event.
57 fn record_one(&self, number: Number);
58}
59
60/// An implementation-level interface to an asynchronous instrument (e.g.,
61/// Observer instruments).
62pub trait AsyncInstrumentCore: InstrumentCore {
63 /// The underlying type as `Any` to support downcasting.
64 fn as_any(&self) -> &dyn Any;
65}