prometheus / prometheus/client_python
How to measure time spent by collector.collect?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 876
- Avg merge
- 8d 4h
- Merged PRs (30d)
- 1
Description
Having a 3rd-party application which can not expose any metrics by itself I wrote my own exporter which takes data from the app and exposes metrics. The next idea was to measure and expose to prometheus an exact time it takes for all of the collectors to get actual data from the app. But two different approaches - with collector.collect() decorator and measurements taken directly inside of collector.collect() yields different results, and I don't understand what the decorator is actually measures.
Could you please explain this difference or maybe I'm missing something?
import time
from typing import Iterable
from prometheus_client import make_wsgi_app, Gauge
from prometheus_client.core import REGISTRY, GaugeMetricFamily, Metric
from prometheus_client.registry import Collector
from wsgiref.simple_server import make_server
COLLECTOR_TIME = Gauge('collector_spent_secods', 'Time spent by collector to get data', ['collector'])
class MetricsCollector(Collector):
@COLLECTOR_TIME.labels('decorator').time()
def collect(self) -> Iterable[Metric]:
st = time.perf_counter()
gauge = GaugeMetricFamily(
name='some_metric_seconds',
documentation='Some seconds metric',
labels=['somename']
)
time.sleep(1)
gauge.add_metric(['foo'], 3.0)
COLLECTOR_TIME.labels('inside').set(time.perf_counter() - st)
yield gauge
if __name__ == "__main__":
REGISTRY.register(MetricsCollector())
app = make_wsgi_app(REGISTRY)
host = '0.0.0.0'
port = 6543
httpd = make_server(host, port, app)
httpd.serve_forever()
$ curl localhost:6543 2>/dev/null | egrep -v '^#' | grep 'collector_spent_secods'
collector_spent_secods{collector="decorator"} 7.499998901039362e-06
collector_spent_secods{collector="inside"} 1.0012022000155412
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the shown Collector.collect generator and the Gauge.time() decorator, then compare the decorator timing with the code inside collect(). Explain why the two measurements differ and document the behavior so users can measure collector collection time correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- prometheus, python
- Domain
- observability
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100