Repository navigation
Closed
Description
Question: can the generate_latest method detect and fix duplicates?
The code for my app is pretty simple. At the top of middleware.py I do:
# Metrics
# See https://github.com/amitsaha/python-prometheus-demo/blob/master/flask_app_prometheus_multiprocessing/src/helpers/middleware.py
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
REQUEST_COUNT = Counter(
"indexapi_request_count",
"help: App Request Count",
["method", "endpoint"],
registry=registry,
)
def setup_metrics(app):
# https://github.com/prometheus/client_python
@app.route("/metrics")
def metrics():
data = generate_latest(registry)
print(data)
return Response(data, mimetype=CONTENT_TYPE_LATEST)
somewhere in middleware.py during _call I do:
REQUEST_COUNT.labels(request.method, path).inc()
in dockerfile I have:
RUN mkdir /tmp/prom_data
ENV prometheus_multiproc_dir /tmp/prom_data
In my main app.py I have:
app = Flask(__name__)
app.wsgi_app = middleware.Middleware(app.wsgi_app)
CORS(app)
logger.debug("Starting metrics")
middleware.setup_metrics(app)
I run UWSGI with:
CMD uwsgi --http 0.0.0.0:8080 --wsgi-file indexapi/app.py --callable app --buffer-size 30000 --processes 4 --threads 4
It seems to work fine, as in no matter what thread processes the request, the metric gets updated once. However, sometimes the text contains duplicate output:
~/D/python-indexapi prom curl localhost:4444/metrics Mon May 3 12:36:28 2021
# HELP indexapi_request_count_total Multiprocess metric
# TYPE indexapi_request_count_total counter
indexapi_request_count_total{endpoint="/index",method="GET"} 1.0
# HELP indexapi_request_count_total help: App Request Count
# TYPE indexapi_request_count_total counter
~/D/python-indexapi prom curl localhost:4444/metrics Mon May 3 12:36:29 2021
# HELP indexapi_request_count_total Multiprocess metric
# TYPE indexapi_request_count_total counter
indexapi_request_count_total{endpoint="/index",method="GET"} 1.0
# HELP indexapi_request_count_total help: App Request Count
# TYPE indexapi_request_count_total counter
indexapi_request_count_total{endpoint="/index",method="GET"} 1.0
# HELP indexapi_request_count_created help: App Request Count
# TYPE indexapi_request_count_created gauge
indexapi_request_count_created{endpoint="/index",method="GET"} 1.6200591934205577e+09
~/D/python-indexapi prom curl localhost:4444/metrics Mon May 3 12:36:29 2021
# HELP indexapi_request_count_total Multiprocess metric
# TYPE indexapi_request_count_total counter
indexapi_request_count_total{endpoint="/index",method="GET"} 1.0
# HELP indexapi_request_count_total help: App Request Count
# TYPE indexapi_request_count_total counter
This is very perplexing to me. We can see
# TYPE indexapi_request_count_total counter
indexapi_request_count_total{endpoint="/index",method="GET"} 1.0
# HELP indexapi_request_count_total help: App Request Count
# TYPE indexapi_request_count_total counter
indexapi_request_count_total{endpoint="/index",method="GET"} 1.0
# HELP indexapi_request_count_created help: App Request Count
Also, when the duplicate is detected, this gauge appears out of nowwhere.
Metadata
Metadata
Assignees
Labels
No labels
Activity
csmarchbanks commentedon May 3, 2021
Hello,
For now you can workaround this issue by removing
registry=registrywhen you createREQUEST_COUNT. In fact, you may want to move the lines:to inside of your metrics route handler. When running in multi process mode all metrics are always registered so it is unnecessary to specify a registry when creating a metric, and can lead to the doubling of metrics that you see (one for multi-process and one for the local process).
At a minimum I think we need to document the registry behavior with multi process. We might also be able to error or warn in the case of duplicate metrics being registered for multi-process, but I would have to think some if that is breaking any legitimate use cases.
tommyjcarpenter commentedon May 3, 2021
@csmarchbanks please bare with me here I'll try to explain what happened when I did this:
So, removing those lines, and the registry from the counter, but leaving in the following in our Dockerfile:
(is that all that is needed to trigger "multiprocess mode" after I removed those lines???)
Leads to the scenario where some calls to
/metricsreturn the metric, and some do not at all. Moreover, only some calls that increment the counter Actually increment it, and some do not, leading to incorrect behavior.Please let me know if I missed a step. Basically the only thing I'm doing to trigger "multiprocess" is that Dockerfile addition and thats it, all the counters are registry agnostic and the registry code lines you mentioned are gone.
However, keeping all that, but also following "Option 1" here seems to be working for us:
https://echorand.me/posts/python-prometheus-monitoring-options/
What happens now is that, every 15 seconds, when metrics are pulled, depending on which worker is responding, it responds with the "right" metric for that worker". Like sometimes I will get:
and others I will get:
But this creates an explosion of metrics that is undesired that we have to sum over (like 4 workers etc). but it works for now.
I should note that we are using UWSGI and not gunicorn, but the docs on multiprocess mode either explictly assume gunicorn, or do not discuss anything other than that. Is this even possible with UWSGI?
tommyjcarpenter commentedon May 3, 2021
in fact, this leads to the same behavior as if I just remove the dockerfile stuff at all; so actually I dont have any idea how to "trigger" multiprocessing mode, after I remove the code you mentioned; can you please let me know how?
csmarchbanks commentedon May 4, 2021
You will still need the CollectorRegistry and MultiProcessCollector lines, they could just be moved into the scope of just the handler for /metrics. That way metrics are less likely to try to use a registry since it would be out of scope.
The minimal change to your example is removing registry=registry. If that is not working I will take a further look tomorrow. Sorry if my first message wasn’t clear!
tommyjcarpenter commentedon May 4, 2021
Your latest suggestion seems to have fixed the issue (after writing the redis reimplementation!). Thanks a lot. I defintiely think maybe some documentation could help:
csmarchbanks commentedon May 11, 2021
@tommyjcarpenter I created #651 to try to avoid duplicate issues in the future, would you be willing to take a look please?
tommyjcarpenter commentedon May 12, 2021
@csmarchbanks thank you, I left a few comments