Skip to content

Commit e1c6139

Browse files
chore(metrics): introduce enums for metric names (#25342)
* Add MetricName into vector-common * Use EventName in lib/ * Use MetricName in src/internal_events * Format * Fix EventName imports * Use MetricName in k8s * Use MetricName in aws_sqs * Use EventName everywhere and make clippy pass * Separate MetricName -> CounterName/GaugeName/HistogramName * Merge imports * Finish MetricName separation * chore: update check-events script to support typed metric name enums * chore: fix stale MetricName doc references in macro wrappers * chore: remove stale doc paragraph from counter! macro * Fix clippy.toml * Use first metric names instead of made up names to remove clippy::disallowed_macros * chore: replace MetricName with CounterName in Windows internal events
1 parent 7923556 commit e1c6139

106 files changed

Lines changed: 1254 additions & 711 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ openssl-src = { version = "300", default-features = false, features = ["force-en
479479

480480
[dev-dependencies]
481481
approx = "0.5.1"
482+
strum.workspace = true
482483
assert_cmd = { version = "2.0.17", default-features = false }
483484
aws-smithy-runtime = { version = "1.8.3", default-features = false, features = ["tls-rustls"] }
484485
base64 = "0.22.1"

benches/metrics_snapshot.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use criterion::{BenchmarkId, Criterion, criterion_group};
2+
use strum::IntoEnumIterator;
3+
use vector_lib::counter;
4+
use vector_lib::internal_event::CounterName;
25

36
fn benchmark(c: &mut Criterion) {
47
let mut group = c.benchmark_group("metrics_snapshot");
@@ -22,8 +25,9 @@ fn prepare_metrics(cardinality: usize) -> &'static vector::metrics::Controller {
2225
let controller = vector::metrics::Controller::get().unwrap();
2326
controller.reset();
2427

28+
let name = CounterName::iter().next().unwrap();
2529
for idx in 0..cardinality {
26-
metrics::counter!("test", "idx" => idx.to_string()).increment(1);
30+
counter!(name, "idx" => idx.to_string()).increment(1);
2731
}
2832

2933
controller

clippy.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ disallowed-methods = [
88
{ path = "vrl::stdlib::all", reason = "Use `vector_vrl_functions::all()` instead for consistency across all Vector VRL functions." },
99
]
1010

11+
disallowed-macros = [
12+
{ path = "metrics::counter", reason = "Use the `counter!` macro from `vector_common` with a `CounterName` variant instead of a raw string." },
13+
{ path = "metrics::histogram", reason = "Use the `histogram!` macro from `vector_common` with a `HistogramName` variant instead of a raw string." },
14+
{ path = "metrics::gauge", reason = "Use the `gauge!` macro from `vector_common` with a `GaugeName` variant instead of a raw string." },
15+
]
16+
1117
disallowed-types = [
1218
{ path = "once_cell::sync::OnceCell", reason = "Use `std::sync::OnceLock` instead." },
1319
{ path = "once_cell::unsync::OnceCell", reason = "Use `std::cell::OnceCell` instead." },

lib/codecs/src/internal_events.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Internal events for codecs.
22
3-
use metrics::counter;
43
use tracing::error;
5-
use vector_common::internal_event::{
6-
ComponentEventsDropped, InternalEvent, UNINTENTIONAL, emit, error_stage, error_type,
4+
use vector_common::{
5+
counter,
6+
internal_event::{
7+
ComponentEventsDropped, CounterName, InternalEvent, UNINTENTIONAL, emit, error_stage,
8+
error_type,
9+
},
710
};
811
use vector_common_macros::NamedInternalEvent;
912

@@ -24,7 +27,7 @@ impl<E: std::fmt::Display> InternalEvent for DecoderFramingError<E> {
2427
stage = error_stage::PROCESSING,
2528
);
2629
counter!(
27-
"component_errors_total",
30+
CounterName::ComponentErrorsTotal,
2831
"error_code" => "decoder_frame",
2932
"error_type" => error_type::PARSER_FAILED,
3033
"stage" => error_stage::PROCESSING,
@@ -50,7 +53,7 @@ impl InternalEvent for DecoderDeserializeError<'_> {
5053
stage = error_stage::PROCESSING,
5154
);
5255
counter!(
53-
"component_errors_total",
56+
CounterName::ComponentErrorsTotal,
5457
"error_code" => "decoder_deserialize",
5558
"error_type" => error_type::PARSER_FAILED,
5659
"stage" => error_stage::PROCESSING,
@@ -77,7 +80,7 @@ impl InternalEvent for EncoderFramingError<'_> {
7780
stage = error_stage::SENDING,
7881
);
7982
counter!(
80-
"component_errors_total",
83+
CounterName::ComponentErrorsTotal,
8184
"error_code" => "encoder_frame",
8285
"error_type" => error_type::ENCODER_FAILED,
8386
"stage" => error_stage::SENDING,
@@ -105,7 +108,7 @@ impl InternalEvent for EncoderSerializeError<'_> {
105108
stage = error_stage::SENDING,
106109
);
107110
counter!(
108-
"component_errors_total",
111+
CounterName::ComponentErrorsTotal,
109112
"error_code" => "encoder_serialize",
110113
"error_type" => error_type::ENCODER_FAILED,
111114
"stage" => error_stage::SENDING,
@@ -137,7 +140,7 @@ impl<E: std::fmt::Display> InternalEvent for EncoderWriteError<'_, E> {
137140
stage = error_stage::SENDING,
138141
);
139142
counter!(
140-
"component_errors_total",
143+
CounterName::ComponentErrorsTotal,
141144
"error_type" => error_type::ENCODER_FAILED,
142145
"stage" => error_stage::SENDING,
143146
)
@@ -170,7 +173,7 @@ impl InternalEvent for EncoderNullConstraintError<'_> {
170173
stage = error_stage::SENDING,
171174
);
172175
counter!(
173-
"component_errors_total",
176+
CounterName::ComponentErrorsTotal,
174177
"error_code" => "encoding_null_constraint",
175178
"error_type" => error_type::ENCODER_FAILED,
176179
"stage" => error_stage::SENDING,
@@ -201,7 +204,7 @@ impl<E: std::fmt::Display> InternalEvent for EncoderRecordBatchError<'_, E> {
201204
stage = error_stage::SENDING,
202205
);
203206
counter!(
204-
"component_errors_total",
207+
CounterName::ComponentErrorsTotal,
205208
"error_code" => self.error_code,
206209
"error_type" => error_type::ENCODER_FAILED,
207210
"stage" => error_stage::SENDING,
@@ -228,7 +231,7 @@ impl InternalEvent for SchemaGenerationError<'_> {
228231
internal_log_rate_limit = false,
229232
);
230233
counter!(
231-
"component_errors_total",
234+
CounterName::ComponentErrorsTotal,
232235
"error_code" => "parquet_schema_generation_failed",
233236
"error_type" => error_type::ENCODER_FAILED,
234237
"stage" => error_stage::SENDING,
@@ -255,7 +258,7 @@ impl InternalEvent for ArrowWriterError<'_> {
255258
internal_log_rate_limit = false,
256259
);
257260
counter!(
258-
"component_errors_total",
261+
CounterName::ComponentErrorsTotal,
259262
"error_code" => "parquet_arrow_writer_failed",
260263
"error_type" => error_type::ENCODER_FAILED,
261264
"stage" => error_stage::SENDING,
@@ -281,7 +284,7 @@ impl InternalEvent for JsonSerializationError<'_> {
281284
internal_log_rate_limit = true,
282285
);
283286
counter!(
284-
"component_errors_total",
287+
CounterName::ComponentErrorsTotal,
285288
"error_type" => error_type::ENCODER_FAILED,
286289
"stage" => error_stage::SENDING,
287290
)

lib/vector-buffers/src/internal_events.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::time::Duration;
22

3-
use metrics::{Histogram, counter, gauge, histogram};
3+
use metrics::Histogram;
44
use vector_common::NamedInternalEvent;
55
use vector_common::{
6-
internal_event::{InternalEvent, error_type},
6+
counter, gauge, histogram,
7+
internal_event::{CounterName, GaugeName, HistogramName, InternalEvent, error_type},
78
registered_event,
89
};
910

@@ -21,29 +22,29 @@ impl InternalEvent for BufferCreated {
2122
let stage = self.idx.to_string();
2223
if self.max_size_events != 0 {
2324
gauge!(
24-
"buffer_max_size_events",
25+
GaugeName::BufferMaxSizeEvents,
2526
"buffer_id" => self.buffer_id.clone(),
2627
"stage" => stage.clone(),
2728
)
2829
.set(self.max_size_events as f64);
2930
// DEPRECATED: buffer-bytes-events-metrics
3031
gauge!(
31-
"buffer_max_event_size",
32+
GaugeName::BufferMaxEventSize,
3233
"buffer_id" => self.buffer_id.clone(),
3334
"stage" => stage.clone(),
3435
)
3536
.set(self.max_size_events as f64);
3637
}
3738
if self.max_size_bytes != 0 {
3839
gauge!(
39-
"buffer_max_size_bytes",
40+
GaugeName::BufferMaxSizeBytes,
4041
"buffer_id" => self.buffer_id.clone(),
4142
"stage" => stage.clone(),
4243
)
4344
.set(self.max_size_bytes as f64);
4445
// DEPRECATED: buffer-bytes-events-metrics
4546
gauge!(
46-
"buffer_max_byte_size",
47+
GaugeName::BufferMaxByteSize,
4748
"buffer_id" => self.buffer_id,
4849
"stage" => stage,
4950
)
@@ -66,40 +67,40 @@ impl InternalEvent for BufferEventsReceived {
6667
#[expect(clippy::cast_precision_loss)]
6768
fn emit(self) {
6869
counter!(
69-
"buffer_received_events_total",
70+
CounterName::BufferReceivedEventsTotal,
7071
"buffer_id" => self.buffer_id.clone(),
7172
"stage" => self.idx.to_string()
7273
)
7374
.increment(self.count);
7475

7576
counter!(
76-
"buffer_received_bytes_total",
77+
CounterName::BufferReceivedBytesTotal,
7778
"buffer_id" => self.buffer_id.clone(),
7879
"stage" => self.idx.to_string()
7980
)
8081
.increment(self.byte_size);
8182
// DEPRECATED: buffer-bytes-events-metrics
8283
gauge!(
83-
"buffer_events",
84+
GaugeName::BufferEvents,
8485
"buffer_id" => self.buffer_id.clone(),
8586
"stage" => self.idx.to_string()
8687
)
8788
.set(self.total_count as f64);
8889
gauge!(
89-
"buffer_size_events",
90+
GaugeName::BufferSizeEvents,
9091
"buffer_id" => self.buffer_id.clone(),
9192
"stage" => self.idx.to_string()
9293
)
9394
.set(self.total_count as f64);
9495
gauge!(
95-
"buffer_size_bytes",
96+
GaugeName::BufferSizeBytes,
9697
"buffer_id" => self.buffer_id.clone(),
9798
"stage" => self.idx.to_string()
9899
)
99100
.set(self.total_byte_size as f64);
100101
// DEPRECATED: buffer-bytes-events-metrics
101102
gauge!(
102-
"buffer_byte_size",
103+
GaugeName::BufferByteSize,
103104
"buffer_id" => self.buffer_id,
104105
"stage" => self.idx.to_string()
105106
)
@@ -121,39 +122,39 @@ impl InternalEvent for BufferEventsSent {
121122
#[expect(clippy::cast_precision_loss)]
122123
fn emit(self) {
123124
counter!(
124-
"buffer_sent_events_total",
125+
CounterName::BufferSentEventsTotal,
125126
"buffer_id" => self.buffer_id.clone(),
126127
"stage" => self.idx.to_string()
127128
)
128129
.increment(self.count);
129130
counter!(
130-
"buffer_sent_bytes_total",
131+
CounterName::BufferSentBytesTotal,
131132
"buffer_id" => self.buffer_id.clone(),
132133
"stage" => self.idx.to_string()
133134
)
134135
.increment(self.byte_size);
135136
// DEPRECATED: buffer-bytes-events-metrics
136137
gauge!(
137-
"buffer_events",
138+
GaugeName::BufferEvents,
138139
"buffer_id" => self.buffer_id.clone(),
139140
"stage" => self.idx.to_string()
140141
)
141142
.set(self.total_count as f64);
142143
gauge!(
143-
"buffer_size_events",
144+
GaugeName::BufferSizeEvents,
144145
"buffer_id" => self.buffer_id.clone(),
145146
"stage" => self.idx.to_string()
146147
)
147148
.set(self.total_count as f64);
148149
gauge!(
149-
"buffer_size_bytes",
150+
GaugeName::BufferSizeBytes,
150151
"buffer_id" => self.buffer_id.clone(),
151152
"stage" => self.idx.to_string()
152153
)
153154
.set(self.total_byte_size as f64);
154155
// DEPRECATED: buffer-bytes-events-metrics
155156
gauge!(
156-
"buffer_byte_size",
157+
GaugeName::BufferByteSize,
157158
"buffer_id" => self.buffer_id,
158159
"stage" => self.idx.to_string()
159160
)
@@ -200,41 +201,41 @@ impl InternalEvent for BufferEventsDropped {
200201
}
201202

202203
counter!(
203-
"buffer_discarded_events_total",
204+
CounterName::BufferDiscardedEventsTotal,
204205
"buffer_id" => self.buffer_id.clone(),
205206
"stage" => self.idx.to_string(),
206207
"intentional" => intentional_str,
207208
)
208209
.increment(self.count);
209210
counter!(
210-
"buffer_discarded_bytes_total",
211+
CounterName::BufferDiscardedBytesTotal,
211212
"buffer_id" => self.buffer_id.clone(),
212213
"stage" => self.idx.to_string(),
213214
"intentional" => intentional_str,
214215
)
215216
.increment(self.byte_size);
216217
// DEPRECATED: buffer-bytes-events-metrics
217218
gauge!(
218-
"buffer_events",
219+
GaugeName::BufferEvents,
219220
"buffer_id" => self.buffer_id.clone(),
220221
"stage" => self.idx.to_string()
221222
)
222223
.set(self.total_count as f64);
223224
gauge!(
224-
"buffer_size_events",
225+
GaugeName::BufferSizeEvents,
225226
"buffer_id" => self.buffer_id.clone(),
226227
"stage" => self.idx.to_string()
227228
)
228229
.set(self.total_count as f64);
229230
gauge!(
230-
"buffer_size_bytes",
231+
GaugeName::BufferSizeBytes,
231232
"buffer_id" => self.buffer_id.clone(),
232233
"stage" => self.idx.to_string()
233234
)
234235
.set(self.total_byte_size as f64);
235236
// DEPRECATED: buffer-bytes-events-metrics
236237
gauge!(
237-
"buffer_byte_size",
238+
GaugeName::BufferByteSize,
238239
"buffer_id" => self.buffer_id,
239240
"stage" => self.idx.to_string()
240241
)
@@ -258,7 +259,7 @@ impl InternalEvent for BufferReadError {
258259
stage = "processing",
259260
);
260261
counter!(
261-
"buffer_errors_total", "error_code" => self.error_code,
262+
CounterName::BufferErrorsTotal, "error_code" => self.error_code,
262263
"error_type" => "reader_failed",
263264
"stage" => "processing",
264265
)
@@ -270,7 +271,7 @@ registered_event! {
270271
BufferSendDuration {
271272
stage: usize,
272273
} => {
273-
send_duration: Histogram = histogram!("buffer_send_duration_seconds", "stage" => self.stage.to_string()),
274+
send_duration: Histogram = histogram!(HistogramName::BufferSendDurationSeconds, "stage" => self.stage.to_string()),
274275
}
275276

276277
fn emit(&self, duration: Duration) {

lib/vector-buffers/src/topology/channel/limited_queue.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ struct Metrics {
127127

128128
impl Metrics {
129129
#[expect(clippy::cast_precision_loss)] // We have to convert buffer sizes for a gauge, it's okay to lose precision here.
130+
#[allow(clippy::disallowed_macros)] // Metric names are constructed dynamically from runtime prefixes.
130131
fn new(
131132
limit: MemoryBufferSize,
132133
metadata: ChannelMetricMetadata,

lib/vector-common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pin-project.workspace = true
4848
serde.workspace = true
4949
serde_json.workspace = true
5050
smallvec = { version = "1", default-features = false }
51+
strum.workspace = true
5152
stream-cancel = { version = "0.8.2", default-features = false }
5253
tokio = { workspace = true, features = ["macros", "time"] }
5354
tracing.workspace = true

0 commit comments

Comments
 (0)