Created
November 5, 2014 19:50
-
-
Save arodland/e7f6a2704948d43dc914 to your computer and use it in GitHub Desktop.
Flask per-route statsd magic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.before_request | |
def statsd_start(): | |
startTime = time.time() | |
endpoint = request.endpoint | |
if endpoint: | |
@flask.after_this_request | |
def statsd_finish(response): | |
endTime = time.time() | |
duration = endTime - startTime | |
status = response.status_code | |
method = request.method | |
routepreifx = "routes." + method + "." + endpoint | |
app.statsd.incr(routeprefix + ".request") | |
app.statsd.incr(routeprefix + ".status." + str(status)) | |
app.statsd.timing(routeprefix + ".timing", duration * 1000) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment