tikv / rust-prometheus

Prometheus instrumentation library for Rust applications
Apache License 2.0
1.05k stars 182 forks source link

Const label and metric labels can cause duplicate labels without errors #444

Open mattiekat opened 2 years ago

mattiekat commented 2 years ago

Describe the bug Currently if you define const labels and then accidentally add a label with one of the names of the const labels it will get added twice without any errors.

To Reproduce Steps to reproduce the behavior:

let const_labels: HashMap<String, String> = labels! {
  "label_1" => "some_value",
  "label_2" => "other_value",
};
let const_labels_ref = const_labels
  .iter()
  .map(|(k, v)| (k.as_str(), v.as_str()))
  .collect::<HashMap<_, _>>();

let my_metric = register_int_counter_vec_with_registry!(
  opts!(
    "my_metric",
    "A basic metric",
    const_labels_ref
  ),
  &["label_2", "label_3"],
  registry
)?;

my_metric.with_label_values(&["other_value", "foobar"]).inc();

End result is that using my metric yields an entry such as

# HELP my_metric A basic metric
# TYPE my_metric counter
my_metric{label_1="some_value",label_2="other_value","label_2"="other_value",label_3="foobar"} 1

Expected behavior Creating the metric with a label that is already defined by the const labels should return an error instead of including it twice in the output.

Additional context I mean, this is certainly user error, but since the scraper seems to handle it by just ignoring the metrics with duplicate labels it took a while to debug and I definitely would have expected an error.