opentelemetry_spanprocessor_any/metrics/
mod.rs1use 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
39pub type Result<T> = result::Result<T, MetricsError>;
41
42#[derive(Error, Debug)]
44#[non_exhaustive]
45pub enum MetricsError {
46 #[error("Metrics error: {0}")]
48 Other(String),
49 #[error("The requested quantile is out of range")]
51 InvalidQuantile,
52 #[error("NaN value is an invalid input")]
54 NaNInput,
55 #[error("Negative value is out of range for this instrument")]
57 NegativeInput,
58 #[error("Inconsistent aggregator types: {0}")]
60 InconsistentAggregator(String),
61 #[error("No data collected by this aggregator")]
63 NoDataCollected,
64 #[error("A metric was already registered by this name with another kind or number type: {0}")]
66 MetricKindMismatch(String),
67 #[error("Inconsistent processor state")]
69 InconsistentState,
70 #[error("Aggregator does not subtract")]
72 NoSubtraction,
73 #[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#[derive(Clone, Default, Debug, PartialEq, Hash)]
92pub struct Unit(Cow<'static, str>);
93
94impl Unit {
95 pub fn new<S>(value: S) -> Self
97 where
98 S: Into<Cow<'static, str>>,
99 {
100 Unit(value.into())
101 }
102
103 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}