Elukey has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/392417 )
Change subject: Fix decoding of JSON data in python3 ...................................................................... Fix decoding of JSON data in python3 * Drop support for Python 2 to simplify the code maintenance. * Add the --encoding parameter (default to utf-8) to allow a user to instruct what encoding the druid exporter should expect to use when decoding POST data coming from Druid. The documentation seems to suggest that UTF-8 is the preferred one. Change-Id: Id1d4e31088117777bab972492724eba0faeb94f4 --- M README.md M druid_exporter/exporter.py M setup.py M tox.ini 4 files changed, 12 insertions(+), 8 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/operations/software/druid_exporter refs/changes/17/392417/1 diff --git a/README.md b/README.md index 93859be..9cf3d80 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ .venv/bin/python setup.py .venv/bin/druid_exporter -By default metrics are exposed on TCP port `8000`. +By default metrics are exposed on TCP port `8000`. Python 2 is not supported. ## Druid versions supported diff --git a/druid_exporter/exporter.py b/druid_exporter/exporter.py index 6f957be..462d29f 100644 --- a/druid_exporter/exporter.py +++ b/druid_exporter/exporter.py @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import absolute_import import argparse import json import logging @@ -30,21 +29,23 @@ class DruidWSGIApp(object): - def __init__(self, post_uri, druid_collector, prometheus_app): + def __init__(self, post_uri, druid_collector, prometheus_app, encoding): self.prometheus_app = prometheus_app self.druid_collector = druid_collector self.post_uri = post_uri + self.encoding = encoding def __call__(self, environ, start_response): if (environ['REQUEST_METHOD'] == 'GET' and environ['PATH_INFO'] == '/metrics'): return self.prometheus_app(environ, start_response) elif (environ['REQUEST_METHOD'] == 'POST' and - environ['PATH_INFO'] == self.post_uri): + environ['PATH_INFO'] == self.post_uri and + environ['CONTENT_TYPE'] == 'application/json'): try: request_body_size = int(environ.get('CONTENT_LENGTH', 0)) request_body = environ['wsgi.input'].read(request_body_size) - datapoints = json.loads(request_body) + datapoints = json.loads(request_body.decode(self.encoding)) # The HTTP metrics emitter can batch datapoints and send them to # a specific endpoint stated in the logs (this tool). for datapoint in datapoints: @@ -68,6 +69,8 @@ help='The URI to check for POSTs coming from Druid') parser.add_argument('-d', '--debug', action='store_true', help='Enable debug logging') + parser.add_argument('-e', '--encoding', default='utf-8', + help='Encoding of the Druid POST JSON data.') parser.add_argument('-c', '--collect-from', type=str, default='all', help="Comma separated list of daemons to collect " @@ -99,7 +102,8 @@ druid_collector = collector.DruidCollector(daemons) REGISTRY.register(druid_collector) prometheus_app = make_wsgi_app() - druid_wsgi_app = DruidWSGIApp(args.uri, druid_collector, prometheus_app) + druid_wsgi_app = DruidWSGIApp(args.uri, druid_collector, + prometheus_app, args.encoding) httpd = make_server(address, int(port), druid_wsgi_app) httpd.serve_forever() diff --git a/setup.py b/setup.py index ae7b22f..38efc51 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup setup(name='druid_exporter', - version='0.1', + version='0.3', description='Prometheus exporter for Druid', url='https://github.com/wikimedia/operations-software-druid_exporter', author='Luca Toscano', diff --git a/tox.ini b/tox.ini index d8c695f..8b8b169 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py3,py2 +envlist = py3 [testenv] deps=nose -- To view, visit https://gerrit.wikimedia.org/r/392417 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id1d4e31088117777bab972492724eba0faeb94f4 Gerrit-PatchSet: 1 Gerrit-Project: operations/software/druid_exporter Gerrit-Branch: debian Gerrit-Owner: Elukey <ltosc...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits