Coverage Report

Created: 2024-11-22 20:17

/build/source/nativelink-metric-collector/src/metrics_collection.rs
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2024 The NativeLink Authors. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//    http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
use std::borrow::Cow;
16
use std::collections::HashMap;
17
use std::ops::{Deref, DerefMut};
18
19
use serde::Serialize;
20
21
use crate::metrics_visitors::CollectionKind;
22
23
/// The final-metric primitive value that was collected with type.
24
#[derive(Debug, Serialize)]
25
#[serde(untagged)]
26
pub enum CollectedMetricPrimitiveValue {
27
    Counter(u64),
28
    String(Cow<'static, str>),
29
}
30
31
/// The final-metric primitive field that was collected.
32
#[derive(Default, Debug)]
33
pub struct CollectedMetricPrimitive {
34
    pub value: Option<CollectedMetricPrimitiveValue>,
35
    pub help: String,
36
    pub value_type: CollectionKind,
37
}
38
39
impl Serialize for CollectedMetricPrimitive {
40
10
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
41
10
    where
42
10
        S: serde::Serializer,
43
10
    {
44
10
        match &self.value {
45
5
            Some(CollectedMetricPrimitiveValue::Counter(value)) => serializer.serialize_u64(*value),
46
5
            Some(CollectedMetricPrimitiveValue::String(value)) => serializer.serialize_str(value),
47
0
            None => serializer.serialize_none(),
48
        }
49
10
    }
50
}
51
52
/// Key-value represented output.
53
pub type CollectedMetricChildren = HashMap<String, CollectedMetrics>;
54
55
/// The type of the collected metric (eg: nested vs primitive).
56
#[derive(Debug, Serialize)]
57
#[serde(untagged)]
58
pub enum CollectedMetrics {
59
    Primitive(CollectedMetricPrimitive),
60
    Component(Box<CollectedMetricChildren>),
61
}
62
63
impl CollectedMetrics {
64
4
    pub fn new_component() -> Self {
65
4
        Self::Component(Box::default())
66
4
    }
67
}
68
69
/// The root metric component that was collected.
70
#[derive(Default, Debug, Serialize)]
71
pub struct RootMetricCollectedMetrics {
72
    #[serde(flatten)]
73
    inner: CollectedMetricChildren,
74
}
75
76
impl Deref for RootMetricCollectedMetrics {
77
    type Target = CollectedMetricChildren;
78
79
1
    fn deref(&self) -> &Self::Target {
80
1
        &self.inner
81
1
    }
82
}
83
84
impl DerefMut for RootMetricCollectedMetrics {
85
16
    fn deref_mut(&mut self) -> &mut Self::Target {
86
16
        &mut self.inner
87
16
    }
88
}