1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::{
metrics::{InstrumentBuilder, MetricsError},
Context, KeyValue,
};
use core::fmt;
use std::convert::TryFrom;
use std::sync::Arc;
pub trait SyncCounter<T> {
fn add(&self, cx: &Context, value: T, attributes: &[KeyValue]);
}
#[derive(Clone)]
pub struct Counter<T>(Arc<dyn SyncCounter<T> + Send + Sync>);
impl<T> fmt::Debug for Counter<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("Counter<{}>", std::any::type_name::<T>()))
}
}
impl<T> Counter<T> {
pub fn new(inner: Arc<dyn SyncCounter<T> + Send + Sync>) -> Self {
Counter(inner)
}
pub fn add(&self, cx: &Context, value: T, attributes: &[KeyValue]) {
self.0.add(cx, value, attributes)
}
}
impl TryFrom<InstrumentBuilder<'_, Counter<u64>>> for Counter<u64> {
type Error = MetricsError;
fn try_from(builder: InstrumentBuilder<'_, Counter<u64>>) -> Result<Self, Self::Error> {
builder.meter.instrument_provider.u64_counter(
builder.name,
builder.description,
builder.unit,
)
}
}
impl TryFrom<InstrumentBuilder<'_, Counter<f64>>> for Counter<f64> {
type Error = MetricsError;
fn try_from(builder: InstrumentBuilder<'_, Counter<f64>>) -> Result<Self, Self::Error> {
builder.meter.instrument_provider.f64_counter(
builder.name,
builder.description,
builder.unit,
)
}
}
pub trait AsyncCounter<T> {
fn observe(&self, cx: &Context, value: T, attributes: &[KeyValue]);
}
pub struct ObservableCounter<T>(Arc<dyn AsyncCounter<T> + Send + Sync>);
impl<T> ObservableCounter<T> {
pub fn new(inner: Arc<dyn AsyncCounter<T> + Send + Sync>) -> Self {
ObservableCounter(inner)
}
}
impl<T> fmt::Debug for ObservableCounter<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"ObservableCounter<{}>",
std::any::type_name::<T>()
))
}
}
impl<T> ObservableCounter<T> {
pub fn observe(&self, cx: &Context, value: T, attributes: &[KeyValue]) {
self.0.observe(cx, value, attributes)
}
}
impl TryFrom<InstrumentBuilder<'_, ObservableCounter<u64>>> for ObservableCounter<u64> {
type Error = MetricsError;
fn try_from(
builder: InstrumentBuilder<'_, ObservableCounter<u64>>,
) -> Result<Self, Self::Error> {
builder.meter.instrument_provider.u64_observable_counter(
builder.name,
builder.description,
builder.unit,
)
}
}
impl TryFrom<InstrumentBuilder<'_, ObservableCounter<f64>>> for ObservableCounter<f64> {
type Error = MetricsError;
fn try_from(
builder: InstrumentBuilder<'_, ObservableCounter<f64>>,
) -> Result<Self, Self::Error> {
builder.meter.instrument_provider.f64_observable_counter(
builder.name,
builder.description,
builder.unit,
)
}
}