Skip to main content

reifydb_core/metrics/
sample.rs

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