Skip to content
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

Add support for mounting the prometheus metrics directly on the Flask app #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,20 @@ flask_request_latency_seconds_sum{endpoint="/",method="GET"} 0.00100278854370117
```

Beware, this seems to not work when the app is run in debug mode.

## WSGI Wrapper
You may also use the WSGI wrapper to expose the metrics, like so:

```
from flask import Flask
from flask_prometheus import monitor_app

app = Flask(__name__)

@app.route('/')
def hello():
return 'Hello'

app = monitor_app(app)
app.run()

Choose a reason for hiding this comment

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

AttributeError: 'DispatcherMiddleware' object has no attribute 'run'

Copy link

Choose a reason for hiding this comment

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

I have a working version over here: https://github.com/noqcks/flask-prom

```
18 changes: 15 additions & 3 deletions flask_prometheus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

import time

from prometheus_client import Counter, Histogram
from prometheus_client import start_http_server
from flask import request
from prometheus_client import Counter, Histogram
from prometheus_client import start_http_server, make_wsgi_app
from werkzeug.wsgi import DispatcherMiddleware

FLASK_REQUEST_LATENCY = Histogram('flask_request_latency_seconds', 'Flask Request Latency',
['method', 'endpoint'])
Expand All @@ -22,6 +22,18 @@ def after_request(response):

return response


def monitor_app(app, path="/metrics"):
app.before_request(before_request)
app.after_request(after_request)

prometheus_app = make_wsgi_app()

return DispatcherMiddleware(app, {
path: prometheus_app
})


def monitor(app, port=8000, addr=''):
app.before_request(before_request)
app.after_request(after_request)
Expand Down