generate_latest() for MultiProcessing writes both the MultiProcess Metrics and per Process Metrics

1,823 views
Skip to first unread message

Mariam Sawires

unread,
Mar 17, 2017, 4:22:40 PM3/17/17
to Prometheus Users

Hi,


I guess the title says it all but using Gunicorn multiprocessing environment and creating a CollectorRegistry() that is passed into MultiProcessorCollector()... then doing generate_latest() on that registry, I get both keys for MultiProcess metrics and per Process Metrics. However, I would like to only see the MultiProcess Metrics and ignore the per Process Metrics. Is there a way to do that?


Here's my code for clarity:

```

import time


from flask import request, Response

from prometheus_client.multiprocess import MultiProcessCollector

from prometheus_client import generate_latest, CollectorRegistry, Counter, Histogram, CONTENT_TYPE_LATEST



REGISTRY = CollectorRegistry()


FLASK_REQUEST_LATENCY = Histogram(

    'flask_request_latency_seconds',

    'Flask Request Latency',

    ['method', 'endpoint'],

    registry=REGISTRY

)


FLASK_REQUEST_COUNT = Counter(

    'flask_request_count',

    'Flask Request Count',

    ['method', 'endpoint', 'http_status'],

    registry=REGISTRY

)


def configure_metrics(app):

    MultiProcessCollector(REGISTRY)


    @app.before_request

    def before_request():

        request.start_time = time.time()


    @app.after_request

    def after_request(response):

        request_latency = time.time() - request.start_time

        FLASK_REQUEST_LATENCY.labels(request.method, request.path).observe(request_latency)

        FLASK_REQUEST_COUNT.labels(request.method, request.path, response.status_code).inc()


        return response


    @app.route('/metrics')

    def metrics():

        output = generate_latest(REGISTRY)

        return Response(output, mimetype=CONTENT_TYPE_LATEST)

```

Thanks,
Mariam

Brian Brazil

unread,
Mar 17, 2017, 4:26:23 PM3/17/17
to Mariam Sawires, Prometheus Users
On 17 March 2017 at 20:22, Mariam Sawires <mar...@unata.com> wrote:

Hi,


I guess the title says it all but using Gunicorn multiprocessing environment and creating a CollectorRegistry() that is passed into MultiProcessorCollector()... then doing generate_latest() on that registry, I get both keys for MultiProcess metrics and per Process Metrics. However, I would like to only see the MultiProcess Metrics and ignore the per Process Metrics. Is there a way to do that?


Here's my code for clarity:

```

import time


from flask import request, Response

from prometheus_client.multiprocess import MultiProcessCollector

from prometheus_client import generate_latest, CollectorRegistry, Counter, Histogram, CONTENT_TYPE_LATEST



REGISTRY = CollectorRegistry()

You should follow the example at https://github.com/prometheus/client_python#multiprocess-mode-gunicorn exactly, and create the registry only per metrics request.

Brian
 

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-users+unsubscribe@googlegroups.com.
To post to this group, send email to prometheus-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/a17c564b-3c60-4048-985e-f66d42fba77e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages