vector/api/schema/metrics/
received_events.rs

1use async_graphql::Object;
2use chrono::{DateTime, Utc};
3
4use crate::{
5    config::ComponentKey,
6    event::{Metric, MetricValue},
7};
8
9pub struct ReceivedEventsTotal(Metric);
10
11impl ReceivedEventsTotal {
12    pub const fn new(m: Metric) -> Self {
13        Self(m)
14    }
15
16    pub fn get_timestamp(&self) -> Option<DateTime<Utc>> {
17        self.0.timestamp()
18    }
19
20    pub fn get_received_events_total(&self) -> f64 {
21        match self.0.value() {
22            MetricValue::Counter { value } => *value,
23            _ => 0.00,
24        }
25    }
26}
27
28#[Object]
29impl ReceivedEventsTotal {
30    /// Metric timestamp
31    pub async fn timestamp(&self) -> Option<DateTime<Utc>> {
32        self.get_timestamp()
33    }
34
35    /// Total incoming events
36    pub async fn received_events_total(&self) -> f64 {
37        self.get_received_events_total()
38    }
39}
40
41impl From<Metric> for ReceivedEventsTotal {
42    fn from(m: Metric) -> Self {
43        Self(m)
44    }
45}
46
47pub struct ComponentReceivedEventsTotal {
48    component_key: ComponentKey,
49    metric: Metric,
50}
51
52impl ComponentReceivedEventsTotal {
53    /// Returns a new `ComponentReceivedEventsTotal` struct, which is a GraphQL type. The
54    /// component id is hoisted for clear field resolution in the resulting payload.
55    pub fn new(metric: Metric) -> Self {
56        let component_key = metric.tag_value("component_id").expect(
57            "Returned a metric without a `component_id`, which shouldn't happen. Please report.",
58        );
59        let component_key = ComponentKey::from(component_key);
60
61        Self {
62            component_key,
63            metric,
64        }
65    }
66}
67
68#[Object]
69impl ComponentReceivedEventsTotal {
70    /// Component id
71    async fn component_id(&self) -> &str {
72        self.component_key.id()
73    }
74
75    /// Total received events metric
76    async fn metric(&self) -> ReceivedEventsTotal {
77        ReceivedEventsTotal::new(self.metric.clone())
78    }
79}
80
81pub struct ComponentReceivedEventsThroughput {
82    component_key: ComponentKey,
83    throughput: i64,
84}
85
86impl ComponentReceivedEventsThroughput {
87    /// Returns a new `ComponentReceivedEventsThroughput`, set to the provided id/throughput values.
88    pub const fn new(component_key: ComponentKey, throughput: i64) -> Self {
89        Self {
90            component_key,
91            throughput,
92        }
93    }
94}
95
96#[Object]
97impl ComponentReceivedEventsThroughput {
98    /// Component id
99    async fn component_id(&self) -> &str {
100        self.component_key.id()
101    }
102
103    /// Received events throughput
104    async fn throughput(&self) -> i64 {
105        self.throughput
106    }
107}