prometheus / prometheus/client_python

Metrics without labels missing breaks some codepath

Open
#949 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.4k
Forks
876
Avg merge
8d 4h
Merged PRs (30d)
1

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the minimal example from the issue, then inspect the clear method at metrics.py line 213 and the initialization path for an unlabeled Gauge. Confirm that calling clear() on a metric without declared labels no longer raises the reported AttributeError and that the labeled case remains working.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.