1use std::borrow::Cow;
5
6use reifydb_value::{byte_size::ByteSize, count::Count, value::duration::Duration};
7
8#[derive(Clone, Debug, PartialEq)]
9pub struct MetricsSample {
10 pub scope: Cow<'static, str>,
11 pub metric: &'static str,
12 pub reading: Reading,
13 pub kind: MetricKind,
14}
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum MetricKind {
18 Dimension,
19 Level,
20 Delta,
21 Counter,
22 Distribution,
23}
24
25impl MetricKind {
26 pub fn name(&self) -> &'static str {
27 match self {
28 MetricKind::Dimension => "dimension",
29 MetricKind::Level => "level",
30 MetricKind::Delta => "delta",
31 MetricKind::Counter => "counter",
32 MetricKind::Distribution => "distribution",
33 }
34 }
35}
36
37#[derive(Clone, Copy, Debug, PartialEq)]
38pub enum Reading {
39 Heap(ByteSize),
40 Bytes(ByteSize),
41 Count(Count),
42 Ratio(f64),
43 Version(u64),
44 Duration(Duration),
45}
46
47#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48pub enum ReadingKind {
49 Bytes,
50 Count,
51 Ratio,
52 Duration,
53}
54
55impl ReadingKind {
56 pub fn reading(self, value: f64) -> Reading {
57 match self {
58 ReadingKind::Bytes => Reading::Bytes(ByteSize::from_bytes(value as u64)),
59 ReadingKind::Count => Reading::Count(Count::new(value as u64)),
60 ReadingKind::Ratio => Reading::Ratio(value),
61 ReadingKind::Duration => Reading::Duration(
62 Duration::from_microseconds(value.min(9.0e15) as i64).unwrap_or_default(),
63 ),
64 }
65 }
66}
67
68impl MetricsSample {
69 pub fn heap(scope: impl Into<Cow<'static, str>>, metric: &'static str, bytes: ByteSize) -> Self {
70 Self {
71 scope: scope.into(),
72 metric,
73 reading: Reading::Heap(bytes),
74 kind: MetricKind::Level,
75 }
76 }
77
78 pub fn bytes(scope: impl Into<Cow<'static, str>>, metric: &'static str, bytes: ByteSize) -> Self {
79 Self {
80 scope: scope.into(),
81 metric,
82 reading: Reading::Bytes(bytes),
83 kind: MetricKind::Level,
84 }
85 }
86
87 pub fn count(scope: impl Into<Cow<'static, str>>, metric: &'static str, count: u64) -> Self {
88 Self {
89 scope: scope.into(),
90 metric,
91 reading: Reading::Count(Count::new(count)),
92 kind: MetricKind::Level,
93 }
94 }
95
96 pub fn counter(scope: impl Into<Cow<'static, str>>, metric: &'static str, count: u64) -> Self {
97 Self {
98 scope: scope.into(),
99 metric,
100 reading: Reading::Count(Count::new(count)),
101 kind: MetricKind::Counter,
102 }
103 }
104
105 pub fn ratio(scope: impl Into<Cow<'static, str>>, metric: &'static str, ratio: f64) -> Self {
106 Self {
107 scope: scope.into(),
108 metric,
109 reading: Reading::Ratio(ratio),
110 kind: MetricKind::Level,
111 }
112 }
113
114 pub fn version(scope: impl Into<Cow<'static, str>>, metric: &'static str, version: u64) -> Self {
115 Self {
116 scope: scope.into(),
117 metric,
118 reading: Reading::Version(version),
119 kind: MetricKind::Level,
120 }
121 }
122
123 pub fn duration(scope: impl Into<Cow<'static, str>>, metric: &'static str, duration: Duration) -> Self {
124 Self {
125 scope: scope.into(),
126 metric,
127 reading: Reading::Duration(duration),
128 kind: MetricKind::Level,
129 }
130 }
131
132 pub fn level(scope: impl Into<Cow<'static, str>>, metric: &'static str, reading: Reading) -> Self {
133 Self {
134 scope: scope.into(),
135 metric,
136 reading,
137 kind: MetricKind::Level,
138 }
139 }
140
141 pub fn cumulative(scope: impl Into<Cow<'static, str>>, metric: &'static str, reading: Reading) -> Self {
142 Self {
143 scope: scope.into(),
144 metric,
145 reading,
146 kind: MetricKind::Counter,
147 }
148 }
149
150 pub fn distribution(scope: impl Into<Cow<'static, str>>, metric: &'static str, reading: Reading) -> Self {
151 Self {
152 scope: scope.into(),
153 metric,
154 reading,
155 kind: MetricKind::Distribution,
156 }
157 }
158}
159
160impl Reading {
161 pub fn as_f64(&self) -> f64 {
162 match self {
163 Reading::Heap(bytes) | Reading::Bytes(bytes) => bytes.as_bytes() as f64,
164 Reading::Count(count) => count.as_u64() as f64,
165 Reading::Ratio(ratio) => *ratio,
166 Reading::Version(version) => *version as f64,
167 Reading::Duration(duration) => duration.to_std().as_micros() as f64,
168 }
169 }
170
171 pub fn unit(&self) -> &'static str {
172 match self {
173 Reading::Heap(_) | Reading::Bytes(_) => "bytes",
174 Reading::Count(_) => "count",
175 Reading::Ratio(_) => "ratio",
176 Reading::Version(_) => "versions",
177 Reading::Duration(_) => "us",
178 }
179 }
180
181 pub fn heap_bytes(&self) -> Option<u64> {
182 match self {
183 Reading::Heap(bytes) => Some(bytes.as_bytes()),
184 _ => None,
185 }
186 }
187}