opentelemetry/metrics/instruments/
mod.rs
1use crate::metrics::{Meter, MetricsError, Result};
2use crate::KeyValue;
3use core::fmt;
4use std::any::Any;
5use std::borrow::Cow;
6use std::marker;
7use std::sync::Arc;
8
9use super::InstrumentProvider;
10
11pub(super) mod counter;
12pub(super) mod gauge;
13pub(super) mod histogram;
14pub(super) mod up_down_counter;
15
16pub trait AsyncInstrument<T>: Send + Sync {
18 fn observe(&self, measurement: T, attributes: &[KeyValue]);
22
23 fn as_any(&self) -> Arc<dyn Any>;
25}
26
27pub struct InstrumentBuilder<'a, T> {
29 instrument_provider: &'a dyn InstrumentProvider,
30 name: Cow<'static, str>,
31 description: Option<Cow<'static, str>>,
32 unit: Option<Cow<'static, str>>,
33 _marker: marker::PhantomData<T>,
34}
35
36impl<'a, T> InstrumentBuilder<'a, T>
37where
38 T: TryFrom<Self, Error = MetricsError>,
39{
40 pub(crate) fn new(meter: &'a Meter, name: Cow<'static, str>) -> Self {
42 InstrumentBuilder {
43 instrument_provider: meter.instrument_provider.as_ref(),
44 name,
45 description: None,
46 unit: None,
47 _marker: marker::PhantomData,
48 }
49 }
50
51 pub fn with_description<S: Into<Cow<'static, str>>>(mut self, description: S) -> Self {
53 self.description = Some(description.into());
54 self
55 }
56
57 pub fn with_unit<S: Into<Cow<'static, str>>>(mut self, unit: S) -> Self {
65 self.unit = Some(unit.into());
66 self
67 }
68
69 pub fn try_init(self) -> Result<T> {
71 T::try_from(self)
72 }
73
74 pub fn init(self) -> T {
83 T::try_from(self).unwrap()
84 }
85}
86
87impl<T> fmt::Debug for InstrumentBuilder<'_, T> {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 f.debug_struct("InstrumentBuilder")
90 .field("name", &self.name)
91 .field("description", &self.description)
92 .field("unit", &self.unit)
93 .field("kind", &std::any::type_name::<T>())
94 .finish()
95 }
96}
97
98pub type Callback<T> = Box<dyn Fn(&dyn AsyncInstrument<T>) + Send + Sync>;
106
107pub struct AsyncInstrumentBuilder<'a, I, M>
109where
110 I: AsyncInstrument<M>,
111{
112 meter: &'a Meter,
113 name: Cow<'static, str>,
114 description: Option<Cow<'static, str>>,
115 unit: Option<Cow<'static, str>>,
116 _inst: marker::PhantomData<I>,
117 callbacks: Vec<Callback<M>>,
118}
119
120impl<'a, I, M> AsyncInstrumentBuilder<'a, I, M>
121where
122 I: TryFrom<Self, Error = MetricsError>,
123 I: AsyncInstrument<M>,
124{
125 pub(crate) fn new(meter: &'a Meter, name: Cow<'static, str>) -> Self {
127 AsyncInstrumentBuilder {
128 meter,
129 name,
130 description: None,
131 unit: None,
132 _inst: marker::PhantomData,
133 callbacks: Vec::new(),
134 }
135 }
136
137 pub fn with_description<S: Into<Cow<'static, str>>>(mut self, description: S) -> Self {
139 self.description = Some(description.into());
140 self
141 }
142
143 pub fn with_unit<S: Into<Cow<'static, str>>>(mut self, unit: S) -> Self {
151 self.unit = Some(unit.into());
152 self
153 }
154
155 pub fn with_callback<F>(mut self, callback: F) -> Self
157 where
158 F: Fn(&dyn AsyncInstrument<M>) + Send + Sync + 'static,
159 {
160 self.callbacks.push(Box::new(callback));
161 self
162 }
163
164 pub fn try_init(self) -> Result<I> {
166 I::try_from(self)
167 }
168
169 pub fn init(self) -> I {
178 I::try_from(self).unwrap()
179 }
180}
181
182impl<I, M> fmt::Debug for AsyncInstrumentBuilder<'_, I, M>
183where
184 I: AsyncInstrument<M>,
185{
186 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187 f.debug_struct("InstrumentBuilder")
188 .field("name", &self.name)
189 .field("description", &self.description)
190 .field("unit", &self.unit)
191 .field("kind", &std::any::type_name::<I>())
192 .field("callbacks_len", &self.callbacks.len())
193 .finish()
194 }
195}