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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::collections::BTreeMap;
use std::num::NonZeroU32;
use std::time::Duration;

use chrono::Utc;
use futures::StreamExt;
use serde_with::serde_as;
use tokio::time;
use tokio_stream::wrappers::IntervalStream;
use vector_lib::configurable::configurable_component;
use vector_lib::internal_event::{
    ByteSize, BytesReceived, CountByteSize, InternalEventHandle as _, Protocol,
};
use vector_lib::{config::LogNamespace, ByteSizeOf, EstimatedJsonEncodedSizeOf};

use crate::{
    config::{SourceConfig, SourceContext, SourceOutput},
    event::{
        metric::{MetricData, MetricName, MetricSeries, MetricTime, MetricValue},
        EventMetadata, Metric, MetricKind,
    },
    internal_events::{EventsReceived, StreamClosedError},
    shutdown::ShutdownSignal,
    SourceSender,
};

/// Configuration for the `static_metrics` source.
#[serde_as]
#[configurable_component(source(
    "static_metrics",
    "Produce static metrics defined in configuration."
))]
#[derive(Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct StaticMetricsConfig {
    /// The interval between metric emitting, in seconds.
    #[serde_as(as = "serde_with::DurationSecondsWithFrac<f64>")]
    #[serde(default = "default_interval")]
    #[configurable(metadata(docs::human_name = "Emitting interval"))]
    pub interval_secs: Duration,

    /// Overrides the default namespace for the metrics emitted by the source.
    #[serde(default = "default_namespace")]
    pub namespace: String,

    #[configurable(derived)]
    #[serde(default)]
    pub metrics: Vec<StaticMetricConfig>,
}

impl Default for StaticMetricsConfig {
    fn default() -> Self {
        Self {
            interval_secs: default_interval(),
            metrics: Vec::default(),
            namespace: default_namespace(),
        }
    }
}

/// Tag configuration for the `internal_metrics` source.
#[configurable_component]
#[derive(Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct StaticMetricConfig {
    /// Name of the static metric
    pub name: String,

    /// "Observed" value of the static metric
    pub value: MetricValue,

    /// Kind of the static metric - either absolute or incremental
    pub kind: MetricKind,

    /// Key-value pairs representing tags and their values to add to the metric.
    #[configurable(metadata(
        docs::additional_props_description = "An individual tag - value pair."
    ))]
    pub tags: BTreeMap<String, String>,
}

fn default_interval() -> Duration {
    Duration::from_secs_f64(1.0)
}

fn default_namespace() -> String {
    "static".to_owned()
}

impl_generate_config_from_default!(StaticMetricsConfig);

#[async_trait::async_trait]
#[typetag::serde(name = "static_metrics")]
impl SourceConfig for StaticMetricsConfig {
    async fn build(&self, cx: SourceContext) -> crate::Result<super::Source> {
        if self.interval_secs.is_zero() {
            warn!(
                "Interval set to 0 secs, this could result in high CPU utilization. It is suggested to use interval >= 1 secs.",
            );
        }
        let interval = self.interval_secs;

        let namespace = self.namespace.clone();

        let metrics = self.metrics.clone();

        Ok(Box::pin(
            StaticMetrics {
                namespace,
                metrics,
                interval,
                out: cx.out,
                shutdown: cx.shutdown,
            }
            .run(),
        ))
    }

    fn outputs(&self, _global_log_namespace: LogNamespace) -> Vec<SourceOutput> {
        vec![SourceOutput::new_metrics()]
    }

    fn can_acknowledge(&self) -> bool {
        false
    }
}

struct StaticMetrics {
    namespace: String,
    metrics: Vec<StaticMetricConfig>,
    interval: time::Duration,
    out: SourceSender,
    shutdown: ShutdownSignal,
}

impl StaticMetrics {
    async fn run(mut self) -> Result<(), ()> {
        let events_received = register!(EventsReceived);
        let bytes_received = register!(BytesReceived::from(Protocol::STATIC));
        let mut interval =
            IntervalStream::new(time::interval(self.interval)).take_until(self.shutdown);

        // Prepare metrics, since they are static and won't change
        let metrics: Vec<Metric> = self
            .metrics
            .into_iter()
            .map(
                |StaticMetricConfig {
                     name,
                     value,
                     kind,
                     tags,
                 }| {
                    Metric::from_parts(
                        MetricSeries {
                            name: MetricName {
                                name,
                                namespace: Some(self.namespace.clone()),
                            },
                            tags: Some(tags.into()),
                        },
                        MetricData {
                            time: MetricTime {
                                timestamp: None,
                                interval_ms: NonZeroU32::new(self.interval.as_millis() as u32),
                            },
                            kind,
                            value: value.clone(),
                        },
                        EventMetadata::default(),
                    )
                },
            )
            .collect();

        while interval.next().await.is_some() {
            let count = metrics.len();
            let byte_size = metrics.size_of();
            let json_size = metrics.estimated_json_encoded_size_of();

            bytes_received.emit(ByteSize(byte_size));
            events_received.emit(CountByteSize(count, json_size));

            let batch = metrics
                .clone()
                .into_iter()
                .map(|metric| metric.with_timestamp(Some(Utc::now())));

            if (self.out.send_batch(batch).await).is_err() {
                emit!(StreamClosedError { count });
                return Err(());
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        event::Event,
        test_util::{
            self,
            components::{run_and_assert_source_compliance, SOURCE_TAGS},
        },
    };

    #[test]
    fn generate_config() {
        test_util::test_generate_config::<StaticMetricsConfig>();
    }

    async fn events_from_config(config: StaticMetricsConfig) -> Vec<Event> {
        run_and_assert_source_compliance(config, time::Duration::from_millis(100), &SOURCE_TAGS)
            .await
    }

    fn default_metric() -> StaticMetricConfig {
        StaticMetricConfig {
            name: "".to_string(),
            value: MetricValue::Gauge { value: 0.0 },
            kind: MetricKind::Absolute,
            tags: BTreeMap::default(),
        }
    }

    #[tokio::test]
    async fn default_empty() {
        let events = events_from_config(StaticMetricsConfig::default()).await;

        assert!(events.is_empty());
    }

    #[tokio::test]
    async fn default_namespace() {
        let mut events = events_from_config(StaticMetricsConfig {
            metrics: vec![default_metric()],
            ..Default::default()
        })
        .await;

        assert!(!events.is_empty());
        let event = events.remove(0);
        assert_eq!(event.as_metric().namespace(), Some("static"));
    }

    #[tokio::test]
    async fn default_namespace_multiple_events() {
        let mut events = events_from_config(StaticMetricsConfig {
            metrics: vec![default_metric(), default_metric()],
            ..Default::default()
        })
        .await;

        assert!(!events.is_empty());
        let event = events.remove(0);
        assert_eq!(event.as_metric().namespace(), Some("static"));
        let event = events.remove(0);
        assert_eq!(event.as_metric().namespace(), Some("static"));
    }

    #[tokio::test]
    async fn namespace() {
        let namespace = "totally_custom";

        let config = StaticMetricsConfig {
            namespace: namespace.to_owned(),
            metrics: vec![default_metric()],
            ..StaticMetricsConfig::default()
        };

        let mut events = events_from_config(config).await;
        assert!(!events.is_empty());
        let event = events.remove(0);

        assert_eq!(event.as_metric().namespace(), Some(namespace));
    }

    #[tokio::test]
    async fn sets_custom_tags() {
        let mut events = events_from_config(StaticMetricsConfig {
            metrics: vec![StaticMetricConfig {
                name: "test".to_string(),
                value: MetricValue::Gauge { value: 2.3 },
                kind: MetricKind::Absolute,
                tags: BTreeMap::from([("custom_tag".to_string(), "custom_tag_value".to_string())]),
            }],
            ..Default::default()
        })
        .await;

        assert!(!events.is_empty());
        let event = events.remove(0);
        let metric = event.as_metric();

        assert_eq!(metric.name(), "test");
        assert!(matches!(metric.value(), MetricValue::Gauge { value: 2.3 }));
        assert_eq!(
            metric.tag_value("custom_tag"),
            Some("custom_tag_value".to_string())
        );
    }
}