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

Streaming responses and latency #143

Open
bheilbrun opened this issue Sep 30, 2022 · 1 comment
Open

Streaming responses and latency #143

bheilbrun opened this issue Sep 30, 2022 · 1 comment

Comments

@bheilbrun
Copy link

What's the best way to measure duration for streaming endpoints?

If I'm not mistaken, the current latency measurements don't work for streaming responses. prometheus_flask_exporter measures the time to return the response generator rather than the time to actually generate the stream response.

Flask's stream documentation gives an example of a streaming endpoint,

@app.route('/large.csv')
def generate_large_csv():
    def generate():
        for row in iter_all_rows():
            yield f"{','.join(row)}\n"
    return app.response_class(generate(), mimetype='text/csv')

In this example, prometheus_flask_exporter would start a duration timer via Flask.before_request and then record the duration via Flask.after_request. When after_request is invoked, the actual response bytes haven't been generated or sent.

I wonder if measuring via Flask.teardown_request along with stream_with_context() would work, but not sure.

Thoughts appreciated!

@rycus86
Copy link
Owner

rycus86 commented Oct 15, 2022

That's an interesting one! I haven't checked stream_with_context() yet, but my gut-feel is that you could add a custom metric on the generate() function inside the request handler function, and that should be timed OK.
We could also look at adding some streaming-friendly wrappers to work with those handlers directly, I haven't looked at that yet.

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

No branches or pull requests

2 participants