vector/api/schema/metrics/
sent_events.rs

1use async_graphql::Object;
2use chrono::{DateTime, Utc};
3
4use super::{Output, OutputThroughput};
5use crate::{
6    config::ComponentKey,
7    event::{Metric, MetricValue},
8};
9
10pub struct SentEventsTotal(Metric);
11
12impl SentEventsTotal {
13    pub const fn new(m: Metric) -> Self {
14        Self(m)
15    }
16
17    pub fn get_timestamp(&self) -> Option<DateTime<Utc>> {
18        self.0.timestamp()
19    }
20
21    pub fn get_sent_events_total(&self) -> f64 {
22        match self.0.value() {
23            MetricValue::Counter { value } => *value,
24            _ => 0.00,
25        }
26    }
27}
28
29#[Object]
30impl SentEventsTotal {
31    /// Metric timestamp
32    pub async fn timestamp(&self) -> Option<DateTime<Utc>> {
33        self.get_timestamp()
34    }
35
36    /// Total sent events
37    pub async fn sent_events_total(&self) -> f64 {
38        self.get_sent_events_total()
39    }
40}
41
42impl From<Metric> for SentEventsTotal {
43    fn from(m: Metric) -> Self {
44        Self(m)
45    }
46}
47
48pub struct ComponentSentEventsTotal {
49    component_key: ComponentKey,
50    outputs: Vec<Output>,
51    metric: Metric,
52}
53
54impl ComponentSentEventsTotal {
55    /// Returns a new `ComponentSentEventsTotal` struct, which is a GraphQL type. The
56    /// component id is hoisted for clear field resolution in the resulting payload.
57    pub fn new(metric: Metric, metric_by_outputs: Vec<Metric>) -> Self {
58        let component_key = metric.tag_value("component_id").expect(
59            "Returned a metric without a `component_id`, which shouldn't happen. Please report.",
60        );
61        let component_key = ComponentKey::from(component_key);
62        let outputs = metric_by_outputs
63            .iter()
64            .filter_map(|m| {
65                m.tag_value("output")
66                    .map(|output_name| Output::new(output_name, Some(m.clone())))
67            })
68            .collect::<Vec<_>>();
69
70        Self {
71            component_key,
72            outputs,
73            metric,
74        }
75    }
76}
77
78#[Object]
79impl ComponentSentEventsTotal {
80    /// Component id
81    async fn component_id(&self) -> &str {
82        self.component_key.id()
83    }
84
85    /// Total outgoing events metric
86    async fn metric(&self) -> SentEventsTotal {
87        SentEventsTotal::new(self.metric.clone())
88    }
89
90    /// Output streams with outgoing events metrics
91    async fn outputs(&self) -> &Vec<Output> {
92        &self.outputs
93    }
94}
95
96pub struct ComponentSentEventsThroughput {
97    component_key: ComponentKey,
98    throughput: i64,
99    outputs: Vec<OutputThroughput>,
100}
101
102impl ComponentSentEventsThroughput {
103    /// Returns a new `ComponentSentEventsThroughput`, set to the provided id/throughput values
104    pub const fn new(
105        component_key: ComponentKey,
106        throughput: i64,
107        outputs: Vec<OutputThroughput>,
108    ) -> Self {
109        Self {
110            component_key,
111            throughput,
112            outputs,
113        }
114    }
115}
116
117#[Object]
118impl ComponentSentEventsThroughput {
119    /// Component id
120    async fn component_id(&self) -> &str {
121        self.component_key.id()
122    }
123
124    /// Total events processed throughput
125    async fn throughput(&self) -> i64 {
126        self.throughput
127    }
128
129    /// Output streams with throughputs
130    async fn outputs(&self) -> &Vec<OutputThroughput> {
131        &self.outputs
132    }
133}