-
Notifications
You must be signed in to change notification settings - Fork 815
Example on how to expose metrics in a flask app #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
README.md
Outdated
|
||
@app.route("/metrics") | ||
def metrics(): | ||
return Response(generate_latest(), content_type=CONTENT_TYPE_LATEST) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't pull in all the other logic and endpoint should have. Can you reuse one of the ones that handles this for you?
README.md
Outdated
|
||
@app.route("/metrics") | ||
def metrics(): | ||
r = core.REGISTRY |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want this logic duplicated here, I want you to use one of the existing functions for it. This code will change over time, and copy&pasted code in user applications won't get updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean using like so?
from prometheus_client import REGISTRY
def metrics():
r = REGISTRY
If not, I'm probably missing the point. In my initial implementation I was hoping/expecting to find a clean function that returns a response having parsed out the parameters and done its magic. A new public api could be added that does something like this
def generate_latest(registry=core.REGISTRY, query_string=None)
"""Generate the latest metrics taking the query string into account"""
metrics_subset = parse_qs(query_string).get('name[]') if query_string is not None else None
if metrics_subset:
r = r.restricted_registry(metrics_subset)
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I mean reusing something like MetricsHandler that does all the http stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since flask's response is a uwsgi application, I'll try to see how I can reuse make_wsgi_app
as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brian-brazil I think the example should be good to go now.
db967e5
to
27d2caf
Compare
README.md
Outdated
#### WSGI | ||
#### Flask | ||
|
||
To use prometheus with [flask](http://flask.pocoo.org/) we need to serve metrics through a promethues wsgi application. This can be achieved using [flask's application dispatching|http://flask.pocoo.org/docs/latest/patterns/appdispatch/]. Below is a working example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flask
README.md
Outdated
@@ -254,7 +254,57 @@ reactor.listenTCP(8000, factory) | |||
reactor.run() | |||
``` | |||
|
|||
#### WSGI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wsgi should stay first
README.md
Outdated
return "Hello world !!" | ||
|
||
# Expose some basic metrics of the application | ||
REQUEST_LATENCY = Histogram('request_latency_seconds', 'API Request Latency', ['method', 'endpoint']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep this minimal, just enough to expose metrics at all like in the other examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
9bd8902
to
42e3c76
Compare
README.md
Outdated
@@ -280,6 +280,34 @@ from prometheus_client import start_wsgi_server | |||
start_wsgi_server(8000) | |||
``` | |||
|
|||
#### Flask | |||
|
|||
To use prometheus with [flask](http://flask.pocoo.org/) we need to serve metrics through a promethues wsgi application. This can be achieved using [flask's application dispatching](http://flask.pocoo.org/docs/latest/patterns/appdispatch/). Below is a working example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flask Prometheus WSGI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand this comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proper nouns should be capitalised.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
README.md
Outdated
}) | ||
``` | ||
|
||
To test this example, save the above snippet in a `myapp.py` file and run the command below |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We mention filenames before the relevant example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
28a7b36
to
b97f7ce
Compare
README.md
Outdated
@@ -280,6 +280,36 @@ from prometheus_client import start_wsgi_server | |||
start_wsgi_server(8000) | |||
``` | |||
|
|||
#### Flask | |||
|
|||
To use Prometheus with [Flask](http://flask.pocoo.org/) we need to serve metrics through a Promethues WSGI application. This can be achieved using [Flask's application dispatching](http://flask.pocoo.org/docs/latest/patterns/appdispatch/). Below is a working example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Promethues//
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops :) Typo fixed
Signed-off-by: Evans Mungai <mbuevans@gmail.com>
Thanks! |
I'm working on a flask application that needs to expose metrics to prometheus. It was not very clear how that could be done without reading the code. This PR updates the README with a simple example of doing so. Hopefully it saves someone some time of trying to figure that out.
@brian-brazil