-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathsample.rs
More file actions
469 lines (364 loc) · 14.2 KB
/
sample.rs
File metadata and controls
469 lines (364 loc) · 14.2 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use chrono::{DateTime, Utc};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use super::counter::Counter;
use super::gauge::Gauge;
use super::label::LabelSet;
use super::prometheus::PrometheusSerializable;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Sample<T> {
#[serde(flatten)]
measurement: Measurement<T>,
#[serde(rename = "labels")]
label_set: LabelSet,
}
impl<T> Sample<T> {
#[must_use]
pub fn new(value: T, recorded_at: DurationSinceUnixEpoch, label_set: LabelSet) -> Self {
let data = Measurement { value, recorded_at };
Self {
measurement: data,
label_set,
}
}
#[must_use]
pub fn measurement(&self) -> &Measurement<T> {
&self.measurement
}
#[must_use]
pub fn value(&self) -> &T {
&self.measurement.value
}
#[must_use]
pub fn recorded_at(&self) -> DurationSinceUnixEpoch {
self.measurement.recorded_at
}
#[must_use]
pub fn labels(&self) -> &LabelSet {
&self.label_set
}
}
impl<T: PrometheusSerializable> PrometheusSerializable for Sample<T> {
fn to_prometheus(&self) -> String {
if self.label_set.is_empty() {
format!(" {}", self.measurement.to_prometheus())
} else {
format!("{} {}", self.label_set.to_prometheus(), self.measurement.to_prometheus())
}
}
}
impl Sample<Counter> {
pub fn increment(&mut self, time: DurationSinceUnixEpoch) {
self.measurement.increment(time);
}
}
impl Sample<Gauge> {
pub fn set(&mut self, value: f64, time: DurationSinceUnixEpoch) {
self.measurement.set(value, time);
}
pub fn increment(&mut self, time: DurationSinceUnixEpoch) {
self.measurement.increment(time);
}
pub fn decrement(&mut self, time: DurationSinceUnixEpoch) {
self.measurement.decrement(time);
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Measurement<T> {
/// The value of the sample.
value: T,
/// The time when the sample was last updated.
#[serde(serialize_with = "serialize_duration", deserialize_with = "deserialize_duration")]
recorded_at: DurationSinceUnixEpoch,
}
impl<T> Measurement<T> {
#[must_use]
pub fn new(value: T, recorded_at: DurationSinceUnixEpoch) -> Self {
Self { value, recorded_at }
}
#[must_use]
pub fn value(&self) -> &T {
&self.value
}
#[must_use]
pub fn recorded_at(&self) -> DurationSinceUnixEpoch {
self.recorded_at
}
fn set_recorded_at(&mut self, time: DurationSinceUnixEpoch) {
self.recorded_at = time;
}
}
impl<T> From<Sample<T>> for (LabelSet, Measurement<T>) {
fn from(sample: Sample<T>) -> Self {
(sample.label_set, sample.measurement)
}
}
impl<T: PrometheusSerializable> PrometheusSerializable for Measurement<T> {
fn to_prometheus(&self) -> String {
self.value.to_prometheus()
}
}
impl Measurement<Counter> {
pub fn increment(&mut self, time: DurationSinceUnixEpoch) {
self.value.increment(1);
self.set_recorded_at(time);
}
pub fn absolute(&mut self, value: u64, time: DurationSinceUnixEpoch) {
self.value.absolute(value);
self.set_recorded_at(time);
}
}
impl Measurement<Gauge> {
pub fn set(&mut self, value: f64, time: DurationSinceUnixEpoch) {
self.value.set(value);
self.set_recorded_at(time);
}
pub fn increment(&mut self, time: DurationSinceUnixEpoch) {
self.value.increment(1.0);
self.set_recorded_at(time);
}
pub fn decrement(&mut self, time: DurationSinceUnixEpoch) {
self.value.decrement(1.0);
self.set_recorded_at(time);
}
}
/// Serializes the `recorded_at` field as a string in ISO 8601 format (RFC 3339).
///
/// # Errors
///
/// Returns an error if:
/// - The conversion from `u64` to `i64` fails.
/// - The timestamp is invalid.
fn serialize_duration<S>(duration: &DurationSinceUnixEpoch, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let secs = i64::try_from(duration.as_secs()).map_err(|_| serde::ser::Error::custom("Timestamp too large"))?;
let nanos = duration.subsec_nanos();
let datetime = DateTime::from_timestamp(secs, nanos).ok_or_else(|| serde::ser::Error::custom("Invalid timestamp"))?;
serializer.serialize_str(&datetime.to_rfc3339()) // Serializes as ISO 8601 (RFC 3339)
}
fn deserialize_duration<'de, D>(deserializer: D) -> Result<DurationSinceUnixEpoch, D::Error>
where
D: Deserializer<'de>,
{
// Deserialize theISO 8601 (RFC 3339) formatted string
let datetime_str = String::deserialize(deserializer)?;
let datetime =
DateTime::parse_from_rfc3339(&datetime_str).map_err(|e| de::Error::custom(format!("Invalid datetime format: {e}")))?;
let datetime_utc = datetime.with_timezone(&Utc);
let secs = u64::try_from(datetime_utc.timestamp()).map_err(|_| de::Error::custom("Timestamp out of range"))?;
Ok(DurationSinceUnixEpoch::new(secs, datetime_utc.timestamp_subsec_nanos()))
}
#[cfg(test)]
mod tests {
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use super::*;
// Helper function to create a sample update time.
fn updated_at_time() -> DurationSinceUnixEpoch {
DurationSinceUnixEpoch::from_secs(1_743_552_000)
}
#[test]
fn it_should_have_a_value() {
let sample = Sample::new(
42,
DurationSinceUnixEpoch::from_secs(1_743_552_000),
LabelSet::from(vec![("test", "label")]),
);
assert_eq!(sample.value(), &42);
}
#[test]
fn it_should_record_the_latest_update_time() {
let sample = Sample::new(
42,
DurationSinceUnixEpoch::from_secs(1_743_552_000),
LabelSet::from(vec![("test", "label")]),
);
assert_eq!(sample.recorded_at(), updated_at_time());
}
#[test]
fn it_should_include_a_label_set() {
let sample = Sample::new(
42,
DurationSinceUnixEpoch::from_secs(1_743_552_000),
LabelSet::from(vec![("test", "label")]),
);
assert_eq!(sample.labels(), &LabelSet::from(vec![("test", "label")]));
}
mod for_counter_type_sample {
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use crate::label::LabelSet;
use crate::prometheus::PrometheusSerializable;
use crate::sample::tests::updated_at_time;
use crate::sample::{Counter, Sample};
#[test]
fn it_should_allow_a_counter_type_value() {
let sample = Sample::new(
Counter::new(42),
DurationSinceUnixEpoch::from_secs(1_743_552_000),
LabelSet::from(vec![("label_name", "label vale")]),
);
assert_eq!(sample.value(), &Counter::new(42));
}
#[test]
fn it_should_allow_incrementing_the_counter() {
let mut sample = Sample::new(Counter::default(), DurationSinceUnixEpoch::default(), LabelSet::default());
sample.increment(updated_at_time());
assert_eq!(sample.value(), &Counter::new(1));
}
#[test]
fn it_should_record_the_latest_update_time_when_the_counter_is_incremented() {
let mut sample = Sample::new(Counter::default(), DurationSinceUnixEpoch::default(), LabelSet::default());
let time = updated_at_time();
sample.increment(time);
assert_eq!(sample.recorded_at(), time);
}
#[test]
fn it_should_allow_exporting_to_prometheus_format() {
let counter = Counter::new(42);
let labels = LabelSet::from(vec![("label_name", "label_value"), ("method", "GET")]);
let sample = Sample::new(counter, DurationSinceUnixEpoch::default(), labels);
assert_eq!(sample.to_prometheus(), r#"{label_name="label_value",method="GET"} 42"#);
}
#[test]
fn it_should_allow_exporting_to_prometheus_format_with_empty_label_set() {
let counter = Counter::new(42);
let sample = Sample::new(counter, DurationSinceUnixEpoch::default(), LabelSet::default());
assert_eq!(sample.to_prometheus(), " 42");
}
}
mod for_gauge_type_sample {
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use crate::label::LabelSet;
use crate::prometheus::PrometheusSerializable;
use crate::sample::tests::updated_at_time;
use crate::sample::{Gauge, Sample};
#[test]
fn it_should_allow_a_counter_type_value() {
let sample = Sample::new(
Gauge::new(42.0),
DurationSinceUnixEpoch::from_secs(1_743_552_000),
LabelSet::from(vec![("label_name", "label vale")]),
);
assert_eq!(sample.value(), &Gauge::new(42.0));
}
#[test]
fn it_should_allow_setting_a_value() {
let mut sample = Sample::new(Gauge::default(), DurationSinceUnixEpoch::default(), LabelSet::default());
sample.set(1.0, updated_at_time());
assert_eq!(sample.value(), &Gauge::new(1.0));
}
#[test]
fn it_should_allow_incrementing_the_value() {
let mut sample = Sample::new(Gauge::new(0.0), DurationSinceUnixEpoch::default(), LabelSet::default());
sample.increment(updated_at_time());
assert_eq!(sample.value(), &Gauge::new(1.0));
}
#[test]
fn it_should_allow_decrementing_the_value() {
let mut sample = Sample::new(Gauge::new(1.0), DurationSinceUnixEpoch::default(), LabelSet::default());
sample.decrement(updated_at_time());
assert_eq!(sample.value(), &Gauge::new(0.0));
}
#[test]
fn it_should_record_the_latest_update_time_when_the_counter_is_incremented() {
let mut sample = Sample::new(Gauge::default(), DurationSinceUnixEpoch::default(), LabelSet::default());
let time = updated_at_time();
sample.set(1.0, time);
assert_eq!(sample.recorded_at(), time);
}
#[test]
fn it_should_allow_exporting_to_prometheus_format() {
let counter = Gauge::new(42.0);
let labels = LabelSet::from(vec![("label_name", "label_value"), ("method", "GET")]);
let sample = Sample::new(counter, DurationSinceUnixEpoch::default(), labels);
assert_eq!(sample.to_prometheus(), r#"{label_name="label_value",method="GET"} 42"#);
}
#[test]
fn it_should_allow_exporting_to_prometheus_format_with_empty_label_set() {
let gauge = Gauge::new(42.0);
let sample = Sample::new(gauge, DurationSinceUnixEpoch::default(), LabelSet::default());
assert_eq!(sample.to_prometheus(), " 42");
}
}
mod serialization_to_json {
use pretty_assertions::assert_eq;
use serde_json::json;
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use crate::label::LabelSet;
use crate::sample::tests::updated_at_time;
use crate::sample::Sample;
#[test]
fn test_serialization_round_trip() {
let original = Sample::new(42, updated_at_time(), LabelSet::from(vec![("test", "serialization")]));
let json = serde_json::to_string(&original).unwrap();
let deserialized: Sample<i32> = serde_json::from_str(&json).unwrap();
assert_eq!(original.measurement.value, deserialized.measurement.value);
assert_eq!(original.measurement.recorded_at, deserialized.measurement.recorded_at);
assert_eq!(original.label_set, deserialized.label_set);
}
#[test]
fn test_rfc3339_serialization_format_for_update_time() {
let sample = Sample::new(
42,
DurationSinceUnixEpoch::new(1_743_552_000, 100),
LabelSet::from(vec![("label_name", "label value")]),
);
let json = serde_json::to_string(&sample).unwrap();
let expected_json = r#"
{
"value": 42,
"recorded_at": "2025-04-02T00:00:00.000000100+00:00",
"labels": [
{
"name": "label_name",
"value": "label value"
}
]
}
"#;
assert_eq!(
serde_json::from_str::<serde_json::Value>(&json).unwrap(),
serde_json::from_str::<serde_json::Value>(expected_json).unwrap()
);
}
#[test]
fn test_invalid_update_timestamp_serialization() {
let timestamp_too_large = DurationSinceUnixEpoch::new(i64::MAX as u64 + 1, 0);
let sample = Sample::new(42, timestamp_too_large, LabelSet::from(vec![("label_name", "label value")]));
let result = serde_json::to_string(&sample);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Timestamp too large"));
}
#[test]
fn test_invalid_update_datetime_deserialization() {
let invalid_json = json!(
r#"
{
"value": 42,
"recorded_at": "1-1-2023T25:00:00Z",
"labels": [
{
"name": "label_name",
"value": "label value"
}
]
}
"#
);
let result: Result<DurationSinceUnixEpoch, _> = serde_json::from_value(invalid_json);
assert!(result.unwrap_err().to_string().contains("invalid type"));
}
#[test]
fn test_update_datetime_high_precision_nanoseconds() {
let sample = Sample::new(
42,
DurationSinceUnixEpoch::new(1_743_552_000, 100),
LabelSet::from(vec![("label_name", "label value")]),
);
let json = serde_json::to_string(&sample).unwrap();
let deserialized: Sample<i32> = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, sample);
}
}
}