8000 Example on how to expose metrics in a flask app by banjoh · Pull Request #297 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 1 commit into from
Aug 21, 2018

Conversation

banjoh
Copy link
Contributor
@banjoh banjoh commented Aug 17, 2018

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

10000
README.md Outdated

@app.route("/metrics")
def metrics():
return Response(generate_latest(), content_type=CONTENT_TYPE_LATEST)
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor Author
@banjoh banjoh Aug 17, 2018

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)
    ...

Copy link
Contributor

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.

10000 Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@banjoh banjoh force-pushed the doc_update branch 3 times, most recently from db967e5 to 27d2caf Compare August 20, 2018 12:15
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.
Copy link
Contributor

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
Copy link
Contributor

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'])
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@banjoh banjoh force-pushed the doc_update branch 2 times, most recently from 9bd8902 to 42e3c76 Compare August 21, 2018 06:36
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flask Prometheus WSGI

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@banjoh banjoh force-pushed the doc_update branch 2 times, most recently from 28a7b36 to b97f7ce Compare August 21, 2018 12:26
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Promethues//

Copy link
Contributor Author

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>
@brian-brazil brian-brazil merged commit b476bff into prometheus:master Aug 21, 2018
@brian-brazil
Copy link
Contributor

Thanks!

@banjoh banjoh deleted the doc_update branch August 23, 2018 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0