Right way to implement prometheus_client.multiprocess.MultiProcessCollector (Django + Celery)

Viewed 2216

I have:

  1. django app run with python manage.py runserver

  2. celery worker run with celery -A project worker -l info

  3. celery beat run with celery -A project beat -l info

They will be deployed in their own pods in kubernetes.

/metrics endpoint is exposed in Django app but metrics are being gathered in Celery tasks. And because these two apps are run in two different process (pods) they have two different registries.

If there is any way to handle this?

It seems that prometheus_client.multiprocess.MultiProcessCollector can help. I tried this but it didn't work:

settings.py

registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry, path='/home/aleksandrovalbert/Work/blackbox_exporter/exporter/multiproc-tmp')

views.py

from project.settings import registry

def export(request):
    metrics_page = prometheus_client.generate_latest(registry)
    return HttpResponse(metrics_page, content_type=prometheus_client.CONTENT_TYPE_LATEST)

tasks.py

registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry, path='/home/aleksandrovalbert/Work/blackbox_exporter/exporter/multiproc-tmp')


class TestBeatPeriodicTask(PeriodicTask):
    run_every = timedelta(minutes=1)

    metric = Gauge(
       name='test_beat',
       documentation='Task Documentation.',
       registry=registry
    )

    def run(self):
        metric.set(888)

Or this can be handled with pushgateway only?

0 Answers
Related