Skip to content
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

Metrics without labels missing breaks some codepath #949

Open
romuald opened this issue Aug 2, 2023 · 2 comments
Open

Metrics without labels missing breaks some codepath #949

romuald opened this issue Aug 2, 2023 · 2 comments

Comments

@romuald
Copy link
Contributor

romuald commented Aug 2, 2023

When initialized without labels, it seems that the resulting metrics objects will lack some instance variables.

Minimal example:

from prometheus_client import Gauge, generate_latest

cc = Gauge("dummy_count", "POC")

if __name__ == '__main__':
    cc.set(42)
    print(generate_latest().decode())  # OK

    cc.clear()    # raises

"""
Traceback (most recent call last):
  File "prom_example.py", line 10, in <module>
    cc.clear()    # raises
    ^^^^^^^^^^
  File …/site-packages/prometheus_client/metrics.py", line 213, in clear
    with self._lock:
         ^^^^^^^^^^
AttributeError: 'Gauge' object has no attribute '_lock'
"""

The same code with declared labels will properly clear the metrics data

I came up upon this when adding a new metric and forgetting to add the labels.

(I'm using .clear() to reset the metrics during tests)

@romuald
Copy link
Contributor Author

romuald commented Aug 2, 2023

I've tried to add a _lock: contextlib.contextmanager = contextlib.nullcontext() in MetricWrapperBase

This fixes the problem, but the .clear() does not do anything anyway (it simply creates a _metrics = {} on the object, the values are kept)

Not sure how to properly fix this, or if this is even desirable

@roidelapluie
Copy link
Member

clear() is indeed not intended for metrics without labels.

You could use e.g. .set(-1) if your tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants