Skip to content

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

README.md Outdated
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
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.

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