vector/api/schema/events/
metric.rs

1use async_graphql::{Enum, Object};
2use chrono::{DateTime, Utc};
3use serde_json::Value;
4use vector_lib::encode_logfmt;
5use vector_lib::event;
6use vector_lib::tap::topology::TapOutput;
7
8use super::EventEncodingType;
9
10#[derive(Debug, Clone)]
11pub struct Metric {
12    output: TapOutput,
13    event: event::Metric,
14}
15
16impl Metric {
17    pub const fn new(output: TapOutput, event: event::Metric) -> Self {
18        Self { output, event }
19    }
20}
21
22#[derive(Copy, Clone, Debug, PartialEq, Eq, Enum)]
23enum MetricKind {
24    /// Incremental metrics update previous values
25    Incremental,
26    /// Absolute metrics set the reference value for future updates
27    Absolute,
28}
29
30impl From<event::MetricKind> for MetricKind {
31    fn from(kind: event::MetricKind) -> Self {
32        match kind {
33            event::MetricKind::Incremental => Self::Incremental,
34            event::MetricKind::Absolute => Self::Absolute,
35        }
36    }
37}
38
39struct MetricTag {
40    key: String,
41    value: String,
42}
43
44#[Object]
45impl MetricTag {
46    /// Metric tag key
47    async fn key(&self) -> &str {
48        self.key.as_ref()
49    }
50
51    /// Metric tag value
52    async fn value(&self) -> &str {
53        self.value.as_ref()
54    }
55}
56
57#[Object]
58/// Metric event with fields for querying metric data
59impl Metric {
60    /// Id of the component associated with the metric event
61    async fn component_id(&self) -> &str {
62        self.output.output_id.component.id()
63    }
64
65    /// Type of component associated with the metric event
66    async fn component_type(&self) -> &str {
67        self.output.component_type.as_ref()
68    }
69
70    /// Kind of component associated with the metric event
71    async fn component_kind(&self) -> &str {
72        self.output.component_kind
73    }
74
75    /// Metric timestamp
76    async fn timestamp(&self) -> Option<&DateTime<Utc>> {
77        self.event.data().timestamp()
78    }
79
80    /// Metric name
81    async fn name(&self) -> &str {
82        self.event.name()
83    }
84
85    /// Metric namespace
86    async fn namespace(&self) -> Option<&str> {
87        self.event.namespace()
88    }
89
90    /// Metric kind
91    async fn kind(&self) -> MetricKind {
92        self.event.kind().into()
93    }
94
95    /// Metric type
96    async fn value_type(&self) -> &str {
97        self.event.value().as_name()
98    }
99
100    /// Metric value in human readable form
101    async fn value(&self) -> String {
102        self.event.value().to_string()
103    }
104
105    /// Metric tags
106    async fn tags(&self) -> Option<Vec<MetricTag>> {
107        self.event.tags().map(|tags| {
108            tags.iter_single()
109                .map(|(key, value)| MetricTag {
110                    key: key.to_owned(),
111                    value: value.to_owned(),
112                })
113                .collect()
114        })
115    }
116
117    /// Metric event as an encoded string format
118    async fn string(&self, encoding: EventEncodingType) -> String {
119        match encoding {
120            EventEncodingType::Json => serde_json::to_string(&self.event)
121                .expect("JSON serialization of metric event failed. Please report."),
122            EventEncodingType::Yaml => serde_yaml::to_string(&self.event)
123                .expect("YAML serialization of metric event failed. Please report."),
124            EventEncodingType::Logfmt => {
125                let json = serde_json::to_value(&self.event)
126                    .expect("logfmt serialization of metric event failed: conversion to serde Value failed. Please report.");
127                match json {
128                    Value::Object(map) => encode_logfmt::encode_map(
129                        &map.into_iter().map(|(k,v)| (event::KeyString::from(k), v)).collect(),
130                    )
131                    .expect("logfmt serialization of metric event failed. Please report."),
132                    _ => panic!("logfmt serialization of metric event failed: metric converted to unexpected serde Value. Please report."),
133                }
134            }
135        }
136    }
137}