prometheus / prometheus/client_python

Gauge and Counter allocate alot of memory

Open
#922 3 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

Hey, I've noticed that a prometheus Gauge or Counter object needs alot of memory. Is this a normal/expected behaviour?

import prometheus_client
import objsize
labels={}
labels['test']='test'

print("Memory Registry at Start: " +str(objsize.get_deep_size(prometheus_client.REGISTRY)))
print("Memory Labels as dict: "+ str(objsize.get_deep_size(labels)))
gauge = prometheus_client.Gauge("test", "", list(sorted(labels.keys())))
gauge.labels(**labels).set(10)
print("Memory Gauge object: "+ str(objsize.get_deep_size(gauge)))
print("Memory Registry after one Gauge: "+str(objsize.get_deep_size(prometheus_client.REGISTRY)))
gauge2 = prometheus_client.Gauge("test2", "")
gauge2.set(10)
print("Memory Gauge object: "+ str(objsize.get_deep_size(gauge2)))
print("Memory Registry after second Gauge: "+str(objsize.get_deep_size(prometheus_client.REGISTRY)))
counter = prometheus_client.Counter("test3", "", list(sorted(labels.keys())))
counter.labels(**labels).inc(10)
print("Memory counter object: "+str(objsize.get_deep_size(counter)))
print("Memory Registry after Counter: "+str(objsize.get_deep_size(prometheus_client.REGISTRY)))
labels={}
labels['test']='test2'
gauge.labels(**labels).set(10)
print("Memory Gauge second label: "+str(objsize.get_deep_size(gauge)))
print("Memory Registry end: "+str(objsize.get_deep_size(prometheus_client.REGISTRY)))

results in this memory usage:

Memory Registry at Start: 3988
Memory Labels as dict: 285
Memory Gauge object: 2418
Memory Registry after one Gauge: 6725
Memory Gauge object: 1051
Memory Registry after second Gauge: 7723
Memory counter object: 2108
Memory Registry after Counter: 10027
Memory Gauge second label: 3376
Memory Registry end: 10931

In this Blog Post https://www.robustperception.io/memory-usage-of-prometheus-client-libraries/ ( I know it's old) a counter needed way less memory than in my usecase. I'm using python 3.9, on windows(but i've got the same problem in a docker container) and prometheus-client 0.17.0

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 by running the supplied Python reproducer with prometheus-client 0.17.0 and Python 3.9, measuring both individual metric objects and prometheus_client.REGISTRY. Done means establishing whether the reported allocations are expected or identifying a concrete memory regression, with a focused test or documentation target if the repository provides one.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.