vector_core/event/metric/
series.rs1use core::fmt;
2
3use vector_common::byte_size_of::ByteSizeOf;
4use vector_config::configurable_component;
5
6use super::{write_list, write_word, MetricTags, TagValue};
7
8#[configurable_component]
10#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
11pub struct MetricSeries {
12 #[serde(flatten)]
13 pub name: MetricName,
14
15 #[configurable(derived)]
16 #[serde(skip_serializing_if = "Option::is_none")]
17 pub tags: Option<MetricTags>,
18}
19
20impl MetricSeries {
21 pub fn name(&self) -> &MetricName {
23 &self.name
24 }
25
26 pub fn name_mut(&mut self) -> &mut MetricName {
28 &mut self.name
29 }
30
31 pub fn tags(&self) -> Option<&MetricTags> {
33 self.tags.as_ref()
34 }
35
36 pub fn tags_mut(&mut self) -> &mut Option<MetricTags> {
38 &mut self.tags
39 }
40
41 pub fn replace_tag(&mut self, key: String, value: impl Into<TagValue>) -> Option<String> {
45 (self.tags.get_or_insert_with(Default::default)).replace(key, value)
46 }
47
48 pub fn set_multi_value_tag(&mut self, key: String, values: impl IntoIterator<Item = TagValue>) {
49 (self.tags.get_or_insert_with(Default::default)).set_multi_value(key, values);
50 }
51
52 pub fn remove_tags(&mut self) {
54 self.tags = None;
55 }
56
57 pub fn remove_tag(&mut self, key: &str) -> Option<String> {
61 match &mut self.tags {
62 None => None,
63 Some(tags) => {
64 let result = tags.remove(key);
65 if tags.is_empty() {
66 self.tags = None;
67 }
68 result
69 }
70 }
71 }
72}
73
74impl ByteSizeOf for MetricSeries {
75 fn allocated_bytes(&self) -> usize {
76 self.name.allocated_bytes() + self.tags.allocated_bytes()
77 }
78}
79
80#[configurable_component]
82#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
83pub struct MetricName {
84 pub name: String,
90
91 #[serde(skip_serializing_if = "Option::is_none")]
99 pub namespace: Option<String>,
100}
101
102impl MetricName {
103 pub fn name(&self) -> &str {
105 self.name.as_str()
106 }
107
108 pub fn name_mut(&mut self) -> &mut String {
110 &mut self.name
111 }
112
113 pub fn namespace(&self) -> Option<&String> {
115 self.namespace.as_ref()
116 }
117
118 pub fn namespace_mut(&mut self) -> &mut Option<String> {
120 &mut self.namespace
121 }
122}
123
124impl fmt::Display for MetricSeries {
125 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
131 if let Some(namespace) = &self.name.namespace {
132 write_word(fmt, namespace)?;
133 write!(fmt, "_")?;
134 }
135 write_word(fmt, &self.name.name)?;
136 write!(fmt, "{{")?;
137 if let Some(tags) = &self.tags {
138 write_list(fmt, ",", tags.iter_all(), |fmt, (tag, value)| {
139 write_word(fmt, tag).and_then(|()| match value {
140 Some(value) => write!(fmt, "={value:?}"),
141 None => Ok(()),
142 })
143 })?;
144 }
145 write!(fmt, "}}")
146 }
147}
148
149impl ByteSizeOf for MetricName {
150 fn allocated_bytes(&self) -> usize {
151 self.name.allocated_bytes() + self.namespace.allocated_bytes()
152 }
153}