vector/api/schema/events/
metric.rs1use 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,
26 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 async fn key(&self) -> &str {
48 self.key.as_ref()
49 }
50
51 async fn value(&self) -> &str {
53 self.value.as_ref()
54 }
55}
56
57#[Object]
58impl Metric {
60 async fn component_id(&self) -> &str {
62 self.output.output_id.component.id()
63 }
64
65 async fn component_type(&self) -> &str {
67 self.output.component_type.as_ref()
68 }
69
70 async fn component_kind(&self) -> &str {
72 self.output.component_kind
73 }
74
75 async fn timestamp(&self) -> Option<&DateTime<Utc>> {
77 self.event.data().timestamp()
78 }
79
80 async fn name(&self) -> &str {
82 self.event.name()
83 }
84
85 async fn namespace(&self) -> Option<&str> {
87 self.event.namespace()
88 }
89
90 async fn kind(&self) -> MetricKind {
92 self.event.kind().into()
93 }
94
95 async fn value_type(&self) -> &str {
97 self.event.value().as_name()
98 }
99
100 async fn value(&self) -> String {
102 self.event.value().to_string()
103 }
104
105 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 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}