We’ve recently started experimenting with the excellent scales library to collect in-process metrics (see Coda Hale’s CodeConf talk “Metrics everywhere” among many others for reasons why one definitely wants to do that).
Scales comes with a flask-based HTTP server that allows viewing the collected measurements and dumping them as JSON. But if you already are in a web application, there’s no real need to spin up yet another thread, open another port etc. to do this. In our case, we’re using Pyramid, so here’s a quick recipe to get the same view that greplin.scales.flaskhandler provides:
Update 2013-11-06: This code is now released as pyramid_scales.
# in your Pyramid setup
config.add_route('scales', '/scales/*prefix')
from StringIO import StringIO
from pyramid.view import view_config
import greplin.scales
import greplin.scales.formats
@view_config(route_name='scales', renderer='string')
def scales_stats(request):
parts = request.matchdict.get('prefix')
path = '/'.join(parts)
stats = greplin.scales.util.lookup(greplin.scales.getStats(), parts)
output = StringIO()
outputFormat = request.params.get('format', 'html')
query = request.params.get('query', None)
if outputFormat == 'json':
request.response.content_type = 'application/json'
greplin.scales.formats.jsonFormat(output, stats, query)
elif outputFormat == 'prettyjson':
request.response.content_type = 'application/json'
greplin.scales.formats.jsonFormat(output, stats, query, pretty=True)
else:
request.response.content_type = 'text/html'
# XXX Dear pyramid.renderers.string_renderer_factory,
# you can't be serious
request.response.default_content_type = 'not-text/html'
output.write('<html>')
greplin.scales.formats.htmlHeader(output, '/' + path, __name__, query)
greplin.scales.formats.htmlFormat(output, tuple(parts), stats, query)
output.write('</html>')
return output.getvalue()