-
Notifications
You must be signed in to change notification settings - Fork 124
/
metrics_collection.rs
77 lines (64 loc) · 1.85 KB
/
metrics_collection.rs
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
use std::{
borrow::Cow,
collections::HashMap,
ops::{Deref, DerefMut},
};
use serde::Serialize;
use crate::metrics_visitors::CollectionKind;
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum CollectedMetricPrimitiveValue {
Counter(u64),
String(Cow<'static, str>),
}
#[derive(Default, Debug)]
pub struct CollectedMetricPrimitive {
pub value: Option<CollectedMetricPrimitiveValue>,
pub help: String,
pub value_type: CollectionKind,
}
impl Serialize for CollectedMetricPrimitive {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match &self.value {
Some(CollectedMetricPrimitiveValue::Counter(value)) => serializer.serialize_u64(*value),
Some(CollectedMetricPrimitiveValue::String(value)) => serializer.serialize_str(value),
None => serializer.serialize_none(),
}
}
}
pub type CollectedMetricChildren = HashMap<String, CollectedMetrics>;
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum CollectedMetrics {
Primitive(CollectedMetricPrimitive),
Component(Box<CollectedMetricChildren>),
}
impl CollectedMetrics {
pub fn new_component() -> Self {
Self::Component(Box::new(CollectedMetricChildren::default()))
}
}
#[derive(Default, Debug, Serialize)]
pub struct RootMetricCollectedMetrics {
#[serde(flatten)]
inner: CollectedMetricChildren,
}
impl RootMetricCollectedMetrics {
pub fn to_json5(&self) -> Result<std::string::String, serde_json::Error> {
serde_json::to_string_pretty(self)
}
}
impl Deref for RootMetricCollectedMetrics {
type Target = CollectedMetricChildren;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for RootMetricCollectedMetrics {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}