Package: wsgi-statsd
wsgi-statsd documentationwsgi_statsd is a WSGI middleware that provides an easy way to time all requests.
Integration is as easy as just wrapping your existing wsgi application.Contentswsgi-statsd documentationUsageWhat it doesCustomizing for your needsContactLicenseChangelog0.3.10.2.60.2.50.2.40.2.30.2.10.2.00.1.0UsageIn your wsgi.py file wrap your WSGI application as follows:importstatsdfromwsgi_statsdimportStatsdTimingMiddlewaredefapplication(environ,start_response):response_body='The request method was%s'%environ['REQUEST_METHOD']status='200 OK'response_headers=[('Content-Type','text/plain'),('Content-Length',str(len(response_body)))]start_response(status,response_headers)return[response_body]client=statsd.StatsClient(prefix='your_prefix',host='your_host',port=8125)application=StatsdTimingMiddleware(application,client)NoteIf an unhandled exception happens, it will not be timed by default.
This is a design decision to separate error reporting and actual statistical measurements.
To enable exception timing, passtime_exception=Trueto the middleware constructor:application=StatsdTimingMiddleware(application,client,time_exceptions=True)What it doeswsgi-statsd uses the statsd timer function, generates a key and sets the amount of time the request took as the value.
The key is constructed as follows:<statsd-client-prefix>.<separator-joined-path>.<request-method>.<response-code>.<exception-name>Using thefooprefix, in your statsd client, and calling thewww.spam.com/bar/test/page will result infoo .bar_test.GET.200having a value equal to the time it took to handle the request.If you passedtime_exceptions=Trueand any exception occurs during the response, then the key name will be postfixed
with the exception class name:foo.bar_test.GET.500.ValueError.Customizing for your needsIt’s possible to customize the waywsgi_statsdgenerates the key and/or time.StatsdTimingMiddlewarehassend_statsandget_key_namewhich you can override:classCustomStatsdMiddleware(StatsdTimingMiddleware):defget_key_name(self,environ,response_interception):returnsuper(self,CustomStatsdMiddleware).get_key_name(environ,response_interception)+'.'+environ['Transfer-Encoding']defsend_stats(self,start,environ,response_interception):super(self,CustomStatsdMiddleware).send_stats(start+10,environ,response_interception)wsgi-statsd uses underscores as a separator for the path part of the key that is sent to statsd as that makes it
easy to retrieve the data from graphite. You can override this default by passing aseparatorvalue to the
middleware constructor:StatsdTimingMiddleware(application,client,separator='.')ContactIf you have questions, bug reports, suggestions, etc. please create an issue on
theGitHub project page.LicenseThis software is licensed under theMIT license.Please refer to thelicense file.© 2015 Wouter Lansu, Paylogic International and others.Changelog0.3.1Added clean target to Makefile.Added the changes for the 0.2.6 version.0.2.6Updated documentation.0.2.5Fixed issue with the separator parameter. It now functions as expected and documented.0.2.4Add separator parameter to StatsdTimingMiddleware.__init__().0.2.3Add exception class name to the key when exception happens (bubenkoff)0.2.1ensure close() is properly called from the response (bubenkoff, GrahamDumpleton)optionally time exceptions (bubenkoff)code readability improvements (bubenkoff)0.2.0Fixed version numbersAdded support for WSGI applications returning generatorsAdded a test to verify that the response body remains the same after passing through the middleware0.1.0Initial public release