-
Notifications
You must be signed in to change notification settings - Fork 799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to clear a particular metric's all labels and values #277
Comments
When trying to write an exporter you should not be using direct instrumentation, see https://github.com/prometheus/client_python#custom-collectors for how to handle this. |
Hi, |
Right now you can use |
Hi Chris, thanks for your swift response. My test example:
It throws the following error: Thanks, |
Ahh, yes, I see what you mean now. *MetricFamily does not have the remove method as they are usually created as part of each collect, not kept around across multiple scrapes. For an individual scrape, is there a concrete use case where you would need to remove a child? Some preprocessing should give you every label value to be exposed rather than adding and removing as you go. |
I'm writing an exporter to monitor a database. If a database is down, the metrics shouldn't be available. There's another scenario: you can have a metric related to an object which doesn't exist anymore (e.g. a tablespace gets dropped). What approach do you suggest? |
Now I understand your comment, thanks! |
How did you do it ? i have the same problem, when db is down i need to remove the gauge metric related to it. |
Recreating it every time. |
in my use case i have some metrics that are exported and then over a period of time, need to be removed (ie the sql instance is stopped or deleted). Recreating every time i dont see it as a solution in my cause , cause the unwanted metric still there and i want to be able to remove it. I see you tried that approch with the remove() and saw in the documentation that there is : ` def remove(self, *labelvalues: Any) -> None:
How can I use this ? |
Hi, At that time I tried with "remove" but it didn't work so I went for the other approach. |
Ok no worries, thanks |
You can remove a label simply by using the remove method as follows: x=Gauge(metric_name, metric_description, label_names) x.remove(label_values_list) Note:- you must provide all values of the label instance you want to delete and the order must match the label_names that were declared when the gauge metric was created. otherwise, you'll get an exception if you want to delete everything just use x.clear() |
Follow up on this, can I clear other metric types such as Counters and Histograms. I am dealing with a use-case where the cardinality is around 10K (number of unique labels), I dont want to store all the children in memory of my service instance, but when I clear a counter the current value will ultimately go to 0 and I think this may cause issues when I use increase method to visualize this metric. Please advice. |
According to the prometheus documentation in the below link,
https://prometheus.io/docs/instrumenting/writing_clientlibs/#labels
Metrics with labels SHOULD support a remove() method with the same signature as labels() that will remove a Child from the metric no longer exporting it, and a clear() method that removes all Children from the metric. These invalidate caching of Children.
This seems to be missing in this python library.
my_gauge_metric = Gauge(..)
my_gauage_metric._metrics.clear() would do the job, but I felt little discomfort in using underscore variables outside. Also, its not thread safe since i should have used self._lock
Would be great if this is fixed in the new updates of this library.
Clearing of a particular metric is needed in Guage since the python layer will constatntly give the old Guage values even though its not generated in the latest time.
The text was updated successfully, but these errors were encountered: