Mercurial > wikked
changeset 329:0b7e6b2699ad
Add support for InfluxDB metrics.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 25 Oct 2014 22:27:17 -0700 |
parents | d19fbf3025e0 |
children | b4609aea0ec8 |
files | wikked/web.py |
diffstat | 1 files changed, 42 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/wikked/web.py Sat Oct 25 22:17:51 2014 -0700 +++ b/wikked/web.py Sat Oct 25 22:27:17 2014 -0700 @@ -28,6 +28,11 @@ app.config.setdefault('WIKI_NO_FLASK_LOGGER', False) app.config.setdefault('PROFILE', False) app.config.setdefault('PROFILE_DIR', None) +app.config.setdefault('INFLUXDB_HOST', None) +app.config.setdefault('INFLUXDB_PORT', 8086) +app.config.setdefault('INFLUXDB_USERNAME', 'root') +app.config.setdefault('INFLUXDB_PASSWORD', 'root') +app.config.setdefault('INFLUXDB_DATABASE', 'database') if app.config['WIKI_NO_FLASK_LOGGER']: @@ -149,3 +154,40 @@ update_wiki.delay(wiki.root) app.wiki_params.wiki_updater = async_updater + +# InfluxDB metrics. +if app.config['INFLUXDB_HOST']: + try: + import influxdb + except ImportError: + raise Exception("Please install the `influxdb` package if you need " + "analytics for your Wikked app.") + host = app.config['INFLUXDB_HOST'] + port = app.config['INFLUXDB_PORT'] + username = app.config['INFLUXDB_USERNAME'] + password = app.config['INFLUXDB_PASSWORD'] + database = app.config['INFLUXDB_DATABASE'] + metrics_db = influxdb.InfluxDBClient(host, port, username, password, database) + app.logger.info("Opening InfluxDB %s on %s:%s as %s." % ( + database, host, port, username)) + + import time + from flask import g, request, request_started, request_tearing_down + def on_request_started(sender, **extra): + g.metrics_start_time = time.clock() + def on_request_tearing_down(sender, **extra): + duration = time.clock() - g.metrics_start_time + data = [ + { + "name": "requests", + "columns": ["request_path", "compute_time"], + "points": [ + [str(request.path), duration] + ] + } + ] + metrics_db.write_points(data) + + request_started.connect(on_request_started, app) + request_tearing_down.connect(on_request_tearing_down, app) +