50onred / flask-datadog Goto Github PK
View Code? Open in Web Editor NEWThis project forked from cyberdelia/flask-statsd
Access dogstatsd from your Flask app
This project forked from cyberdelia/flask-statsd
Access dogstatsd from your Flask app
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:
import time
from flask import Flask, g, request
from flask.ext.datadog import StatsD
app = Flask(__name__)
statsd = StatsD(app=app)
def before_request():
g.request_start_time = time.time()
def after_request(response):
# Return early if we don't have the start time
if hasattr(g, 'request_start_time'):
return response
# Get elapsed time in milliseconds
elapsed = time.time() - g.request_start_time
elapsed = int(round(1000 * elapsed))
# Collect request/response tags
tags = [
'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 metric
statsd.timing('flask.response.time', elapsed, tags=g.request_tags + tags)
# Return the original unmodified response
return response
app.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?
Hi,
This is a nice extension. Unfortunately it took me a while to get it working because my dogstatsd server was listening on localhost and this line (app.config['STATSD_HOST'] = 'statsd.local'
) in the example caused my data to be sent into the void. Statsd's protocol lacks acknowledgements, so I didn't get any error messages from the host setting being incorrect, and it took me a while to realize what was wrong.
So, why is there a line in the example usage that seems to prevent the extension from working with a standard Datadog Agent setup? I noticed that line is also in https://github.com/cyberdelia/flask-statsd. Can it be removed or does it still serve a purpose?
Also the Pypy package is out of date.
Thanks for your work on this.
The pypi packages out there are unusable due to the (api breaking?) change in the datadog package, fixed in #9 but not yet deployed.
The idea is to allow people to batch all metrics during a single request and flush at the end of the request.
Something similar to the following:
def before_request():
statsd.open_buffer(statsd.max_buffer_size)
def after_request():
statsd.close_buffer()
Similar to #4 we could add this by default, and add a config flag to disable (or enable), and/or add a setup_batching(app)
type method.
Since we are adding in the middleware for response time, maybe there are some other default metrics we can provide for people?
An initial thought I had was, flask.response.size
which is a histogram of response.headers['Content-Type']
, which we could also tag with response.mimetype
so you can track response body size.
Since they also contain their API client in the datadog
package, it might be nice to also have an API
class that sets up an API client as well.
from flask.ext.datadog import API
app = Flask(__name__)
app.config['DATADOG_API_KEY'] = ''
app.config['DATADOG_APP_KEY'] = ''
dogapi = API(app=app)
dogapi.Event.create(...)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.