You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The first thing I did in my app with this client was to add before_request and after_request handlers to get response time metrics into datadog. Seems like it might be a decent/common functionality people would want?
The gist of it looks like this:
importtimefromflaskimportFlask, g, requestfromflask.ext.datadogimportStatsDapp=Flask(__name__)
statsd=StatsD(app=app)
defbefore_request():
g.request_start_time=time.time()
defafter_request(response):
# Return early if we don't have the start timeifhasattr(g, 'request_start_time'):
returnresponse# Get elapsed time in millisecondselapsed=time.time() -g.request_start_timeelapsed=int(round(1000*elapsed))
# Collect request/response tagstags= [
'endpoint:{endpoint}'.format(endpoint=request.endpoint),
'request_method:{method}'.format(method=request.method.lower()),
'status_code:{status_code}'.format(status_code=response.status_code),
]
# Record our response time metricstatsd.timing('flask.response.time', elapsed, tags=g.request_tags+tags)
# Return the original unmodified responsereturnresponseapp.before_request(before_request)
app.after_request(after_request)
This might be something we can do by default when running init_app, or maybe just a method setup_middleware(app) for handling it.
Does this seem like a reasonable thing to try and tackle, or should we leave this middleware stuff as an exercise for the implementer?
The text was updated successfully, but these errors were encountered:
Its definitely common functionality as its pretty much the first thing we did as well. If its done by default it should be able to be disabled. I kind of like the setup_middleware approach. I can see different projects wanting different tags or wanting/needing to use different names for their tags so as long as its customizable it would be a good thing to have.
The first thing I did in my app with this client was to add
before_request
andafter_request
handlers to get response time metrics into datadog. Seems like it might be a decent/common functionality people would want?The gist of it looks like this:
This might be something we can do by default when running
init_app
, or maybe just a methodsetup_middleware(app)
for handling it.Does this seem like a reasonable thing to try and tackle, or should we leave this middleware stuff as an exercise for the implementer?
The text was updated successfully, but these errors were encountered: