use crate::{
metrics::{
sdk_api::{
AsyncInstrumentCore, InstrumentCore, MeterCore, SyncBoundInstrumentCore,
SyncInstrumentCore,
},
AsyncRunner, Descriptor, InstrumentKind, Measurement, Meter, MeterProvider, Number,
NumberKind, Result,
},
Context, KeyValue,
};
use std::any::Any;
use std::sync::Arc;
lazy_static::lazy_static! {
static ref NOOP_DESCRIPTOR: Descriptor = Descriptor::new(String::new(), "noop", None, InstrumentKind::Counter, NumberKind::U64);
}
#[derive(Debug, Default)]
pub struct NoopMeterProvider {
_private: (),
}
impl NoopMeterProvider {
pub fn new() -> Self {
NoopMeterProvider { _private: () }
}
}
impl MeterProvider for NoopMeterProvider {
fn meter(&self, name: &'static str, version: Option<&'static str>) -> Meter {
Meter::new(name, version, Arc::new(NoopMeterCore::new()))
}
}
#[derive(Debug, Default)]
pub struct NoopMeterCore {
_private: (),
}
impl NoopMeterCore {
pub fn new() -> Self {
NoopMeterCore { _private: () }
}
}
impl MeterCore for NoopMeterCore {
fn new_sync_instrument(&self, _descriptor: Descriptor) -> Result<Arc<dyn SyncInstrumentCore>> {
Ok(Arc::new(NoopSyncInstrument::new()))
}
fn new_async_instrument(
&self,
_descriptor: Descriptor,
_runner: Option<AsyncRunner>,
) -> Result<Arc<dyn AsyncInstrumentCore>> {
Ok(Arc::new(NoopAsyncInstrument::new()))
}
fn record_batch_with_context(
&self,
_cx: &Context,
_attributes: &[KeyValue],
_measurements: Vec<Measurement>,
) {
}
fn new_batch_observer(&self, _runner: AsyncRunner) -> Result<()> {
Ok(())
}
}
#[derive(Debug, Default)]
pub struct NoopSyncInstrument {
_private: (),
}
impl NoopSyncInstrument {
pub fn new() -> Self {
NoopSyncInstrument { _private: () }
}
}
impl InstrumentCore for NoopSyncInstrument {
fn descriptor(&self) -> &Descriptor {
&NOOP_DESCRIPTOR
}
}
impl SyncInstrumentCore for NoopSyncInstrument {
fn bind(&self, _attributes: &'_ [KeyValue]) -> Arc<dyn SyncBoundInstrumentCore> {
Arc::new(NoopBoundSyncInstrument::new())
}
fn record_one(&self, _number: Number, _attributes: &'_ [KeyValue]) {
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[derive(Debug, Default)]
pub struct NoopBoundSyncInstrument {
_private: (),
}
impl NoopBoundSyncInstrument {
pub fn new() -> Self {
NoopBoundSyncInstrument { _private: () }
}
}
impl SyncBoundInstrumentCore for NoopBoundSyncInstrument {
fn record_one(&self, _number: Number) {
}
}
#[derive(Debug, Default)]
pub struct NoopAsyncInstrument {
_private: (),
}
impl NoopAsyncInstrument {
pub fn new() -> Self {
NoopAsyncInstrument { _private: () }
}
}
impl InstrumentCore for NoopAsyncInstrument {
fn descriptor(&self) -> &Descriptor {
&NOOP_DESCRIPTOR
}
}
impl AsyncInstrumentCore for NoopAsyncInstrument {
fn as_any(&self) -> &dyn Any {
self
}
}