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
mod allocated_bytes;
mod errors;
pub mod filter;
mod output;
mod received_bytes;
mod received_events;
mod sent_bytes;
mod sent_events;
mod sink;
pub mod source;
mod transform;
mod uptime;

#[cfg(feature = "sources-host_metrics")]
mod host;

pub use allocated_bytes::{AllocatedBytes, ComponentAllocatedBytes};
use async_graphql::{Interface, Subscription};
use chrono::{DateTime, Utc};
pub use errors::{ComponentErrorsTotal, ErrorsTotal};
pub use filter::*;
pub use output::*;
pub use received_bytes::{
    ComponentReceivedBytesThroughput, ComponentReceivedBytesTotal, ReceivedBytesTotal,
};
pub use received_events::{
    ComponentReceivedEventsThroughput, ComponentReceivedEventsTotal, ReceivedEventsTotal,
};
pub use sent_bytes::{ComponentSentBytesThroughput, ComponentSentBytesTotal, SentBytesTotal};
pub use sent_events::{ComponentSentEventsThroughput, ComponentSentEventsTotal, SentEventsTotal};
pub use sink::{IntoSinkMetrics, SinkMetrics};
pub use source::{IntoSourceMetrics, SourceMetrics};
use tokio_stream::{Stream, StreamExt};
pub use transform::{IntoTransformMetrics, TransformMetrics};
pub use uptime::Uptime;

use crate::config::ComponentKey;

#[derive(Interface)]
#[graphql(field(name = "timestamp", ty = "Option<DateTime<Utc>>"))]
pub enum MetricType {
    Uptime(Uptime),
}

#[derive(Default)]
pub struct MetricsQuery;

#[cfg(feature = "sources-host_metrics")]
#[async_graphql::Object]
impl MetricsQuery {
    /// Vector host metrics
    async fn host_metrics(&self) -> host::HostMetrics {
        host::HostMetrics::new()
    }
}

#[derive(Default)]
pub struct MetricsSubscription;

#[Subscription]
impl MetricsSubscription {
    /// Metrics for how long the Vector instance has been running
    async fn uptime(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Uptime> {
        get_metrics(interval).filter_map(|m| match m.name() {
            "uptime_seconds" => Some(Uptime::new(m)),
            _ => None,
        })
    }

    /// Total received events metrics
    #[graphql(deprecation = "Use component_received_events_totals instead")]
    async fn received_events_total(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = ReceivedEventsTotal> {
        get_metrics(interval).filter_map(|m| match m.name() {
            "component_received_events_total" => Some(ReceivedEventsTotal::new(m)),
            _ => None,
        })
    }

    /// Total received events throughput sampled over the provided millisecond `interval`
    #[graphql(deprecation = "Use component_received_events_throughputs instead")]
    async fn received_events_throughput(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = i64> {
        counter_throughput(interval, &|m| m.name() == "component_received_events_total")
            .map(|(_, throughput)| throughput as i64)
    }

    /// Total incoming component events throughput metrics over `interval`
    async fn component_received_events_throughputs(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentReceivedEventsThroughput>> {
        component_counter_throughputs(interval, &|m| m.name() == "component_received_events_total")
            .map(|m| {
                m.into_iter()
                    .map(|(m, throughput)| {
                        ComponentReceivedEventsThroughput::new(
                            ComponentKey::from(m.tag_value("component_id").unwrap()),
                            throughput as i64,
                        )
                    })
                    .collect()
            })
    }

    /// Total received component event metrics over `interval`
    async fn component_received_events_totals(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentReceivedEventsTotal>> {
        component_counter_metrics(interval, &|m| m.name() == "component_received_events_total").map(
            |m| {
                m.into_iter()
                    .map(ComponentReceivedEventsTotal::new)
                    .collect()
            },
        )
    }

    /// Total sent events metrics
    #[graphql(deprecation = "Use component_sent_events_totals instead")]
    async fn sent_events_total(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = SentEventsTotal> {
        get_metrics(interval).filter_map(|m| match m.name() {
            "component_sent_events_total" => Some(SentEventsTotal::new(m)),
            _ => None,
        })
    }

    /// Total outgoing events throughput sampled over the provided millisecond `interval`
    #[graphql(deprecation = "Use component_sent_events_throughputs instead")]
    async fn sent_events_throughput(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = i64> {
        counter_throughput(interval, &|m| m.name() == "component_sent_events_total")
            .map(|(_, throughput)| throughput as i64)
    }

    /// Total outgoing component event throughput metrics over `interval`
    async fn component_sent_events_throughputs(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentSentEventsThroughput>> {
        component_sent_events_total_throughputs_with_outputs(interval).map(|m| {
            m.into_iter()
                .map(|(key, total_throughput, outputs)| {
                    ComponentSentEventsThroughput::new(key, total_throughput, outputs)
                })
                .collect()
        })
    }

    /// Total outgoing component event metrics over `interval`
    async fn component_sent_events_totals(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentSentEventsTotal>> {
        component_sent_events_totals_metrics_with_outputs(interval).map(|ms| {
            ms.into_iter()
                .map(|(m, m_by_outputs)| ComponentSentEventsTotal::new(m, m_by_outputs))
                .collect()
        })
    }

    /// Component bytes received metrics over `interval`.
    async fn component_received_bytes_totals(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentReceivedBytesTotal>> {
        component_counter_metrics(interval, &|m| m.name() == "component_received_bytes_total").map(
            |m| {
                m.into_iter()
                    .map(ComponentReceivedBytesTotal::new)
                    .collect()
            },
        )
    }

    /// Component bytes received throughput over `interval`
    async fn component_received_bytes_throughputs(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentReceivedBytesThroughput>> {
        component_counter_throughputs(interval, &|m| m.name() == "component_received_bytes_total")
            .map(|m| {
                m.into_iter()
                    .map(|(m, throughput)| {
                        ComponentReceivedBytesThroughput::new(
                            ComponentKey::from(m.tag_value("component_id").unwrap()),
                            throughput as i64,
                        )
                    })
                    .collect()
            })
    }

    /// Component bytes sent metrics over `interval`.
    async fn component_sent_bytes_totals(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentSentBytesTotal>> {
        component_counter_metrics(interval, &|m| m.name() == "component_sent_bytes_total")
            .map(|m| m.into_iter().map(ComponentSentBytesTotal::new).collect())
    }

    /// Component bytes sent throughput over `interval`
    async fn component_sent_bytes_throughputs(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentSentBytesThroughput>> {
        component_counter_throughputs(interval, &|m| m.name() == "component_sent_bytes_total").map(
            |m| {
                m.into_iter()
                    .map(|(m, throughput)| {
                        ComponentSentBytesThroughput::new(
                            ComponentKey::from(m.tag_value("component_id").unwrap()),
                            throughput as i64,
                        )
                    })
                    .collect()
            },
        )
    }

    /// Total error metrics.
    async fn errors_total(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = ErrorsTotal> {
        get_metrics(interval)
            .filter(|m| m.name().ends_with("_errors_total"))
            .map(ErrorsTotal::new)
    }

    /// Allocated bytes metrics.
    async fn allocated_bytes(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = AllocatedBytes> {
        get_metrics(interval)
            .filter(|m| m.name() == "component_allocated_bytes")
            .map(AllocatedBytes::new)
    }

    /// Component allocation metrics
    async fn component_allocated_bytes(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentAllocatedBytes>> {
        component_gauge_metrics(interval, &|m| m.name() == "component_allocated_bytes")
            .map(|m| m.into_iter().map(ComponentAllocatedBytes::new).collect())
    }

    /// Component error metrics over `interval`.
    async fn component_errors_totals(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = Vec<ComponentErrorsTotal>> {
        component_counter_metrics(interval, &|m| m.name().ends_with("_errors_total"))
            .map(|m| m.into_iter().map(ComponentErrorsTotal::new).collect())
    }

    /// All metrics.
    async fn metrics(
        &self,
        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
    ) -> impl Stream<Item = MetricType> {
        get_metrics(interval).filter_map(|m| match m.name() {
            "uptime_seconds" => Some(MetricType::Uptime(m.into())),
            _ => None,
        })
    }
}