Skip to content

Reset ObservableGauge for all cached attributes sets to 0 #1221

@Matthias247

Description

@Matthias247

I'm using an ObservableGauge to track errors of certain entities that the application is monitoring might have encountered.
The application scans the state of those entities, in periodic intervals, aggregates errors, and then updates gauges according to the latest state. This uses code along:

let mut attrs: Vec<KeyValue> = attributes.to_vec();
attrs.push(KeyValue::new("error", "".to_string()));
let mut total_errs = 0;

for (error, &count) in m.errors_encountered.iter() {
    total_errs += count;
    attrs.last_mut().unwrap().value = error.to_string().into();
    self.errors_gauge
        .observe(otel_cx, count as u64, &attrs);
}

attrs.last_mut().unwrap().value = "any".to_string().into();
self.errors_gauge
    .observe(otel_cx, total_errs as u64, &attrs);

That works fine for all errors that had been recently encountered.
However I noticed that once one scan doesn't report a certain error anymore, the opentelemetry-rust/promtheus stack still reports the old error. It would need to be explicetly set to 0.

Is there any mechanism in opentelemetry-rust that allows to reset a gauge (with all variations of attributes) to 0 before updating them again?

If there would be a well-defined set of errors, I could obviously manually update the values that are not part of errors_encountered to 0. But since those errors are dynamic strings that are received from another application that isn't easily possible.

Maybe the right answer here is also "you are doing it wrong and shouldn't use gauges for it", which is certainly debatable :) But for this particular problem where the exact amount of entities in a certain state should be determined independently of the report frequency and without metric math, they seem much easier to use.

Metadata

Metadata

Assignees

Labels

A-metricsArea: issues related to metricsquestionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions