Skip to content

Commit d4ced10

Browse files
authored
Fix duplicate metric registration for filters (#397)
Some filter currently call `register` (rather than `register_if_not_exists`) which throws an error if the metric has already been registered - this meant that we couldn't use multiple instances of those filter in the filter chain
1 parent ae0c424 commit d4ced10

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

src/filters/capture_bytes/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Metrics {
3232
"CaptureBytes",
3333
"Total number of packets dropped due capture size being larger than the received packet",
3434
))?
35-
.register(registry)?,
35+
.register_if_not_exists(registry)?,
3636
})
3737
}
3838
}

src/filters/compress/metrics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ impl Metrics {
3838
),
3939
&operation_labels,
4040
)?
41-
.register(registry)?;
41+
.register_if_not_exists(registry)?;
4242

4343
let decompressed_bytes_total = IntCounter::with_opts(filter_opts(
4444
"decompressed_bytes_total",
4545
"Compress",
4646
"Total number of decompressed bytes either received or sent.",
4747
))?
48-
.register(registry)?;
48+
.register_if_not_exists(registry)?;
4949

5050
let compressed_bytes_total = IntCounter::with_opts(filter_opts(
5151
"compressed_bytes_total",
5252
"Compress",
5353
"Total number of compressed bytes either received or sent.",
5454
))?
55-
.register(registry)?;
55+
.register_if_not_exists(registry)?;
5656

5757
Ok(Metrics {
5858
packets_dropped_compress: dropped_metric

src/filters/local_rate_limit/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Metrics {
3131
"LocalRateLimit",
3232
"Total number of packets dropped due to rate limiting",
3333
))?
34-
.register(registry)?,
34+
.register_if_not_exists(registry)?,
3535
})
3636
}
3737
}

src/filters/token_router/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Metrics {
3636
),
3737
&label_names,
3838
)?
39-
.register(registry)?;
39+
.register_if_not_exists(registry)?;
4040

4141
Ok(Metrics {
4242
packets_dropped_no_token_found: metric

src/metrics.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,22 @@ pub fn filter_opts(name: &str, filter_name: &str, description: &str) -> Opts {
4343
opts(name, &format!("filter_{}", filter_name), description)
4444
}
4545

46-
pub trait CollectorExt: Collector + Clone + Sized + 'static {
47-
/// Registers the current metric collector with the provided registry.
48-
/// Returns an error if a collector with the same name has already
49-
/// been registered.
50-
fn register(self, registry: &Registry) -> Result<Self> {
51-
registry.register(Box::new(self.clone()))?;
52-
Ok(self)
53-
}
46+
/// Registers the current metric collector with the provided registry.
47+
/// Returns an error if a collector with the same name has already
48+
/// been registered.
49+
fn register_metric<T: Collector + Sized + 'static>(
50+
registry: &Registry,
51+
collector: T,
52+
) -> Result<()> {
53+
registry.register(Box::new(collector))
54+
}
5455

56+
pub trait CollectorExt: Collector + Clone + Sized + 'static {
5557
/// Registers the current metric collector with the provided registry
5658
/// if not already registered.
5759
fn register_if_not_exists(self, registry: &Registry) -> Result<Self> {
58-
match self.clone().register(registry) {
60+
match register_metric(registry, self.clone()) {
5961
Ok(_) | Err(prometheus::Error::AlreadyReg) => Ok(self),
60-
Err(prometheus::Error::Msg(msg)) if msg.contains("already exists") => {
61-
// FIXME: We should be able to remove this branch entirely if `AlreadyReg` gets fixed.
62-
// https://github.com/tikv/rust-prometheus/issues/247
63-
Ok(self)
64-
}
6562
Err(err) => Err(err),
6663
}
6764
}

0 commit comments

Comments
 (0)