prometheus / prometheus/client_python

labels method lead to memory leak

Open
#937 2 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I find metrics in my celery task will lead to memory leak. And I reproduced this problem issue locally.

import tracemalloc
from prometheus_client import Counter

counter_a = Counter('request_count_a', 'request count to a', ['name'])
counter_b = Counter('request_count_b', 'request count to b')

tracemalloc.start()
for _ in range(10):
    counter_a.labels('a').inc()
    curr_mem, peak_mem = tracemalloc.get_traced_memory()
    print(f"curr mem: {curr_mem / 10**3} Kb, peak mem: {peak_mem / 10**3} Kb")

print("*****************************************")

for _ in range(10):
    counter_b.inc()
    curr_mem, peak_mem = tracemalloc.get_traced_memory()
    print(f"curr mem: {curr_mem / 10 ** 3} Kb, peak mem: {peak_mem / 10 ** 3} Kb")

It seems to be caused by the labels method. PS: prometheus-client==0.17.1
mem

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

Start with the provided tracemalloc reproduction and inspect the labels method used by counter_a, comparing it with counter_b.inc(). Confirm whether repeated labels calls cause the reported memory growth, then verify that the reproduced leak is resolved without changing normal counter behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.