opentelemetry_spanprocessor_any/metrics/
mod.rs

1//! # OpenTelemetry Metrics API
2
3use std::borrow::Cow;
4use std::result;
5use std::sync::PoisonError;
6use thiserror::Error;
7
8mod async_instrument;
9mod config;
10mod counter;
11mod descriptor;
12mod kind;
13mod meter;
14pub mod noop;
15mod number;
16mod observer;
17pub mod registry;
18pub mod sdk_api;
19mod sync_instrument;
20mod up_down_counter;
21mod value_recorder;
22
23use crate::sdk::export::ExportError;
24pub use async_instrument::{AsyncRunner, BatchObserverResult, Observation, ObserverResult};
25pub use config::InstrumentConfig;
26pub use counter::{BoundCounter, Counter, CounterBuilder};
27pub use descriptor::Descriptor;
28pub use kind::InstrumentKind;
29pub use meter::{Meter, MeterProvider};
30pub use number::{AtomicNumber, Number, NumberKind};
31pub use observer::{
32    BatchObserver, SumObserver, SumObserverBuilder, UpDownSumObserver, UpDownSumObserverBuilder,
33    ValueObserver, ValueObserverBuilder,
34};
35pub use sync_instrument::Measurement;
36pub use up_down_counter::{BoundUpDownCounter, UpDownCounter, UpDownCounterBuilder};
37pub use value_recorder::{BoundValueRecorder, ValueRecorder, ValueRecorderBuilder};
38
39/// A specialized `Result` type for metric operations.
40pub type Result<T> = result::Result<T, MetricsError>;
41
42/// Errors returned by the metrics API.
43#[derive(Error, Debug)]
44#[non_exhaustive]
45pub enum MetricsError {
46    /// Other errors not covered by specific cases.
47    #[error("Metrics error: {0}")]
48    Other(String),
49    /// Errors when requesting quantiles out of the 0-1 range.
50    #[error("The requested quantile is out of range")]
51    InvalidQuantile,
52    /// Errors when recording nan values.
53    #[error("NaN value is an invalid input")]
54    NaNInput,
55    /// Errors when recording negative values in monotonic sums.
56    #[error("Negative value is out of range for this instrument")]
57    NegativeInput,
58    /// Errors when merging aggregators of incompatible types.
59    #[error("Inconsistent aggregator types: {0}")]
60    InconsistentAggregator(String),
61    /// Errors when requesting data when no data has been collected
62    #[error("No data collected by this aggregator")]
63    NoDataCollected,
64    /// Errors when registering to instruments with the same name and kind
65    #[error("A metric was already registered by this name with another kind or number type: {0}")]
66    MetricKindMismatch(String),
67    /// Errors when processor logic is incorrect
68    #[error("Inconsistent processor state")]
69    InconsistentState,
70    /// Errors when aggregator cannot subtract
71    #[error("Aggregator does not subtract")]
72    NoSubtraction,
73    /// Fail to export metrics
74    #[error("Metrics exporter {} failed with {0}", .0.exporter_name())]
75    ExportErr(Box<dyn ExportError>),
76}
77
78impl<T: ExportError> From<T> for MetricsError {
79    fn from(err: T) -> Self {
80        MetricsError::ExportErr(Box::new(err))
81    }
82}
83
84impl<T> From<PoisonError<T>> for MetricsError {
85    fn from(err: PoisonError<T>) -> Self {
86        MetricsError::Other(err.to_string())
87    }
88}
89
90/// Units denote underlying data units tracked by `Meter`s.
91#[derive(Clone, Default, Debug, PartialEq, Hash)]
92pub struct Unit(Cow<'static, str>);
93
94impl Unit {
95    /// Create a new `Unit` from an `Into<String>`
96    pub fn new<S>(value: S) -> Self
97    where
98        S: Into<Cow<'static, str>>,
99    {
100        Unit(value.into())
101    }
102
103    /// View unit as &str
104    pub fn as_str(&self) -> &str {
105        self.0.as_ref()
106    }
107}
108
109impl AsRef<str> for Unit {
110    #[inline]
111    fn as_ref(&self) -> &str {
112        self.0.as_ref()
113    }
114}