client_python
client_python copied to clipboard
Remove child metric
Hello. Tried to remove some childs, but got KeyError.
My custom metric (gauge) have some lables and worked fine. But when i needed to delete some childs, i'm stuck:
global statusOK
statusOK = "OK"
global statusNOK
statusNOK = "Fail"
service_login_status_prom = prom.Gauge('service_login_status', 'Check access token availability', ['status', 'dc_name'])
result = str('0')
service_login_status_prom.labels(statusOK, dc_name).set(result)
service_login_status_prom.remove(statusNOK, dc_name)
https://github.com/prometheus/client_python/blob/09fb45953bac018a90e89f0b1e7bcd1d5d81c01b/prometheus_client/metrics.py#L187
Error is -
File "/usr/local/lib/python3.8/site-packages/prometheus_client/metrics.py", line 187, in remove
del self._metrics[labelvalues]
KeyError: ('Fail', '11u25')
When label is only one, error quite different -
File "/usr/local/lib/python3.8/site-packages/prometheus_client/metrics.py", line 187, in remove
del self._metrics[labelvalues]
KeyError: ('Fail', )
Comma at the end of labelvalues confused me
Hello, it looks like you are trying to remove a child that does not exist, thus KeyError is an appropriate response.
In the second example are you only passing one argument? The error could be improved some for that case.
Hello @csmarchbanks ,
I also have this KeyError issue while trying to remove a child metric that is still there, as prometheus can see it. Can you provide a short example on how to call the remove method?
Thanks!
Hello @cmarasescu
In my case, I rewrite it with clear()
-
service_login_status_prom.clear()
Hello @hAh0L13 ,
I've finally fixed my issue, it seems that I've just put the labels in a wrong order. Most likely your initial call was failing simply because there was no child metric matching your labels.