Search code examples
python-3.xmonitoringprometheussanicprometheus-node-exporter

Prometheus not capturing data for counter


Prometheus is not able to capture data for the counter if calling increment from ipython console or celery tasks. Counter is worked for API request but outside of API request not worked like if i tried it from Ipython console, from celery tasks to increment the counter.

Prometheus is also not thrown any exceptions.

# Using Sanic python framework (async) and python 3.6.
Here are the library Details:
sanic==0.7.0
ipython
prometheus-client==0.5.0
python 3.6.5

Here is the config I am using in Sanic Python app to configure Prometheus:

class MetricsHandler(HTTPMethodView):
    """prometheus metrics exporter"""

    async def get(self, request, *args, **kwargs):
        registry = None
        if 'prometheus_multiproc_dir' in os.environ:
            registry = core.REGISTRY
        else:
            registry = CollectorRegistry()
            multiprocess.MultiProcessCollector(registry)
        data = generate_latest(registry)
        return raw(data, content_type=CONTENT_TYPE_LATEST)

# ipython console example:
In [1]: Prom_Count = Counter('Prom_Counter', 'prom counter', ['func', 'count'])
In [2]: Prom_Count.labels("test", "count").inc()

prometheus_multiproc_dir enviroment varibale is also set.

Previously we were using sanic-prometheus library:

prometheus-client==0.2.0
sanic-prometheus==0.1.4
wrapt==1.10.11

Thanks in advance.


Solution

  • Consider doing this:

    def get_registry():
        if 'prometheus_multiproc_dir' in os.environ:
            registry = CollectorRegistry()
            multiprocess.MultiProcessCollector(registry)
        else:
            registry = REGISTRY
        return registry
    

    And then pass registry to Counter constructor:

    Prom_Count = Counter('Prom_Counter', 'prom counter', ['func', 'count'], registry=get_registry())
    

    Once I have faced such problem and it worked for me