Skip to content

Instantly share code, notes, and snippets.

@arodland
Created November 5, 2014 19:50
Show Gist options
  • Save arodland/e7f6a2704948d43dc914 to your computer and use it in GitHub Desktop.
Save arodland/e7f6a2704948d43dc914 to your computer and use it in GitHub Desktop.
Flask per-route statsd magic
@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