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
use crate::export::metrics::aggregation::AggregationKind;
use crate::metrics::sdk_api::{Descriptor, InstrumentKind};
#[derive(Clone)]
struct ConstantTemporalitySelector(Temporality);
impl TemporalitySelector for ConstantTemporalitySelector {
fn temporality_for(&self, _descriptor: &Descriptor, _kind: &AggregationKind) -> Temporality {
self.0
}
}
pub fn constant_temporality_selector(temporality: Temporality) -> impl TemporalitySelector + Clone {
ConstantTemporalitySelector(temporality)
}
pub fn cumulative_temporality_selector() -> impl TemporalitySelector + Clone {
constant_temporality_selector(Temporality::Cumulative)
}
pub fn delta_temporality_selector() -> impl TemporalitySelector + Clone {
constant_temporality_selector(Temporality::Delta)
}
pub fn stateless_temporality_selector() -> impl TemporalitySelector + Clone {
constant_temporality_selector(Temporality::Cumulative)
}
#[derive(Clone)]
struct StatelessTemporalitySelector;
impl TemporalitySelector for StatelessTemporalitySelector {
fn temporality_for(&self, descriptor: &Descriptor, kind: &AggregationKind) -> Temporality {
if kind == &AggregationKind::SUM && descriptor.instrument_kind().precomputed_sum() {
Temporality::Cumulative
} else {
Temporality::Delta
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Temporality {
Cumulative = 1,
Delta = 2,
}
impl Temporality {
pub fn includes(&self, other: &Self) -> bool {
(*self as u32) & (*other as u32) != 0
}
pub fn memory_required(&self, kind: &InstrumentKind) -> bool {
match kind {
InstrumentKind::Histogram
| InstrumentKind::GaugeObserver
| InstrumentKind::Counter
| InstrumentKind::UpDownCounter => {
self.includes(&Temporality::Cumulative)
}
InstrumentKind::CounterObserver | InstrumentKind::UpDownCounterObserver => {
self.includes(&Temporality::Delta)
}
}
}
}
pub trait TemporalitySelector {
fn temporality_for(&self, descriptor: &Descriptor, kind: &AggregationKind) -> Temporality;
}