Elukey has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/389475 )

Change subject: [WIP] First commit
......................................................................

[WIP] First commit

Change-Id: I90d3d84304c5f9f783e1b52afa203f04eb066a05
---
A LICENSE
A README.md
A debian/changelog
A debian/compact
A debian/control
A debian/copyright
A debian/init
A debian/rules
A debian/service
A debian/source/format
A druid_exporter/__init__.py
A druid_exporter/collector.py
A druid_exporter/exporter.py
A setup.py
A test/test_collector.py
A tox.ini
16 files changed, 736 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/software/druid_exporter 
refs/changes/75/389475/1

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..261eeb9
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,201 @@
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..709abac
--- /dev/null
+++ b/README.md
@@ -0,0 +1,16 @@
+# Prometheus exporter for Druid (http://druid.io/)
+
+Collects HTTP POST JSON data coming from Druid daemons and expose them 
formatted
+following the [Prometheus](https://prometheus.io) standards.
+
+## Running
+
+The easiest way to run `druid_exporter` is via a virtualenv:
+
+  virtualenv .venv
+  .venv/bin/python setup.py develop
+  .venv/bin/druid_exporter
+
+By default metrics are exposed on TCP port `8000`:
+
+TODO
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..c4a7d75
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+prometheus-hhvm-exporter (0.1-1) unstable; urgency=low
+
+  * Initial packaging
+
+ -- Luca Toscano <[email protected]>  Fri, 06 Oct 2017 13:53:36 +0000
diff --git a/debian/compact b/debian/compact
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/debian/compact
@@ -0,0 +1 @@
+9
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..b659314
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,19 @@
+Source: prometheus-druid-exporter
+Maintainer: Luca Toscano <[email protected]>
+Section: python
+Priority: optional
+Build-Depends: debhelper (>= 9),
+  dh-python,
+  dh-systemd,
+  python3-setuptools (>= 0.6b3),
+  python3-all,
+  python3-nose,
+  python3-prometheus-client,
+  python3-requests
+Standards-Version: 3.9.8
+
+Package: prometheus-druid-exporter
+Architecture: all
+Depends: ${misc:Depends}, ${python3:Depends}
+Description: Prometheus exporter for Druid (http://druid.io/)
+
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..0dcd8d2
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,24 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: hhvm_exporter
+Source: https://gerrit.wikimedia.org/r/operations/software/hhvm_exporter
+
+Files: *
+Copyright: 2017 Luca Toscano <[email protected]>
+           2017 Wikimedia Foundation
+License: Apache-2.0
+
+License: Apache-2.0
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ .
+ http://www.apache.org/licenses/LICENSE-2.0
+ .
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ .
+ On Debian systems, the full text of the Apache License version 2 can be found
+ in the file `/usr/share/common-licenses/Apache-2.0'.
diff --git a/debian/init b/debian/init
new file mode 100755
index 0000000..90bf685
--- /dev/null
+++ b/debian/init
@@ -0,0 +1,64 @@
+#!/bin/sh
+# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
+if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
+    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
+fi
+### BEGIN INIT INFO
+# Provides:          prometheus-druid-exporter
+# Required-Start:    $remote_fs $syslog
+# Required-Stop:     $remote_fs $syslog
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Prometheus exporter for druid
+### END INIT INFO
+
+# Author: Filippo Giunchedi <[email protected]>
+
+DESC="Prometheus exporter for druid"
+DAEMON=/usr/bin/prometheus-druid-exporter
+NAME=prometheus-druid-exporter
+USER=prometheus
+PIDFILE=/var/run/prometheus/prometheus-druid-exporter.pid
+LOGFILE=/var/log/prometheus/prometheus-druid-exporter.log
+
+ARGS=""
+[ -r /etc/default/$NAME ] && . /etc/default/$NAME
+
+HELPER=/usr/bin/daemon
+HELPER_ARGS="--name=$NAME --output=$LOGFILE --pidfile=$PIDFILE --user=$USER"
+
+do_start_prepare()
+{
+    mkdir -p `dirname $PIDFILE` || true
+    chown -R $USER: `dirname $LOGFILE`
+    chown -R $USER: `dirname $PIDFILE`
+}
+
+do_start_cmd()
+{
+    # Return
+    #   0 if daemon has been started
+    #   1 if daemon was already running
+    #   2 if daemon could not be started
+    $HELPER $HELPER_ARGS --running && return 1
+    $HELPER $HELPER_ARGS -- $DAEMON $ARGS || return 2
+    return 0
+}
+
+do_stop_cmd()
+{
+    # Return
+    #   0 if daemon has been stopped
+    #   1 if daemon was already stopped
+    #   2 if daemon could not be stopped
+    #   other if a failure occurred
+    $HELPER $HELPER_ARGS --running || return 1
+    $HELPER $HELPER_ARGS --stop || return 2
+    # wait for the process to really terminate
+    for n in 1 2 3 4 5; do
+        sleep 1
+        $HELPER $HELPER_ARGS --running || break
+    done
+    $HELPER $HELPER_ARGS --running || return 0
+    return 2
+}
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..7ac6d64
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,15 @@
+#!/usr/bin/make -f
+
+export DH_VERBOSE=1
+export PYBUILD_NAME=druid_exporter
+
+DEBPKGNAME ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Source: //p')
+
+%:
+       dh $@ --with python3,systemd --buildsystem=pybuild
+
+override_dh_auto_install:
+       dh_auto_install
+       # Rename the binary to match the debian package.
+       mv -v debian/$(DEBPKGNAME)/usr/bin/druid_exporter \
+            debian/$(DEBPKGNAME)/usr/bin/$(DEBPKGNAME)
diff --git a/debian/service b/debian/service
new file mode 100644
index 0000000..1bb889f
--- /dev/null
+++ b/debian/service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Prometheus exporter for Druid (http://druid.io/)
+Documentation=https://prometheus.io/docs/introduction/overview/
+
+[Service]
+Restart=always
+User=prometheus
+EnvironmentFile=-/etc/default/prometheus-druid-exporter
+ExecStart=/usr/bin/prometheus-druid-exporter $ARGS
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/source/format b/debian/source/format
new file mode 100644
index 0000000..163aaf8
--- /dev/null
+++ b/debian/source/format
@@ -0,0 +1 @@
+3.0 (quilt)
diff --git a/druid_exporter/__init__.py b/druid_exporter/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/druid_exporter/__init__.py
diff --git a/druid_exporter/collector.py b/druid_exporter/collector.py
new file mode 100644
index 0000000..9f421a6
--- /dev/null
+++ b/druid_exporter/collector.py
@@ -0,0 +1,229 @@
+# Copyright 2017 Luca Toscano
+#                Wikimedia Foundation
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from prometheus_client.core import (CounterMetricFamily, GaugeMetricFamily,
+                                    HistogramMetricFamily, Summary)
+import logging
+
+log = logging.getLogger(__name__)
+
+
+class DruidCollector(object):
+    scrape_duration = Summary(
+            'druid_scrape_duration_seconds', 'Druid scrape duration')
+
+    def __init__(self, allowed_daemons):
+        self.supported_metric_names = [
+            'query/time',
+            'query/bytes',
+            'query/node/time',
+            'query/node/bytes',
+            'query/cache/total/numEntries',
+            'query/cache/total/sizeBytes',
+            'query/cache/total/hits',
+            'query/cache/total/misses',
+            'query/cache/total/evictions',
+            'query/cache/total/timeouts',
+            'query/cache/total/errors',
+            'jvm/pool/committed',
+            'jvm/pool/init',
+            'jvm/pool/max',
+            'jvm/pool/used',
+            'jvm/bufferpool/count',
+            'jvm/bufferpool/used',
+            'jvm/bufferpool/capacity',
+            'jvm/mem/init',
+            'jvm/mem/max',
+            'jvm/mem/used',
+            'jvm/mem/committed',
+            'jvm/gc/count',
+            'jvm/gc/time',
+            'segment/max',
+            'segment/count',
+            'segment/pendingDelete',
+            'segment/used',
+        ]
+        self.metric_buckets = {
+            'query/time': ['10', '100', '500', '10000', 'inf'],
+            'query/bytes': ['10', '100', '500', '10000', 'inf'],
+            'query/node/time': ['10', '100', '500', '10000', 'inf'],
+            'query/node/bytes': ['10', '100', '500', '10000', 'inf'],
+        }
+
+        # Format: {metric_name: {bucket2: value, bucket2: value, ...}
+        self.histograms = {
+            'query/time': None,
+            'query/bytes': None,
+            'query/node/time': None,
+            'query/node/bytes': None,
+        }
+
+        self.counters = {
+            'query/cache/total/numentries': None,
+            'query/cache/total/sizebytes': None,
+            'query/cache/total/hits': None,
+            'query/cache/total/misses': None,
+            'query/cache/total/evictions': None,
+            'query/cache/total/timeouts': None,
+            'query/cache/total/errors': None,
+            'segment/max': None,
+            'segment/count': None,
+            'segment/pendingDelete': None,
+            'segment/used': None,
+        }
+
+        self.allowed_daemons = allowed_daemons
+
+    def _get_query_histograms(self, daemon):
+        return {
+               'query/time': HistogramMetricFamily(
+                       'druid_' + daemon + '_query_time',
+                       'Milliseconds taken to complete a query.',
+                       labels=['datasource', 'le']),
+               'query/bytes': HistogramMetricFamily(
+                       'druid_' + daemon + '_query_bytes',
+                       'Number of bytes returned in query response.',
+                       labels=['datasource', 'le']),
+               'query/node/time': HistogramMetricFamily(
+                       'druid_' + daemon + '_query_node_time',
+                       'Milliseconds taken to query individual '
+                       'historical/realtime nodes.',
+                       labels=['datasource', 'server', 'le']),
+               'query/node/bytes': HistogramMetricFamily(
+                       'druid_' + daemon + '_query_node_bytes',
+                       'number of bytes returned from querying individual '
+                       'historical/realtime nodes.',
+                       labels=['datasource', 'server', 'le']),
+                }
+
+    def _get_cache_counters(self, daemon):
+        return {
+            'query/cache/total/numentries': GaugeMetricFamily(
+                   'druid_' + daemon + '_query_cache_total_numentries',
+                   'Number of cache entries.'),
+            'query/cache/total/sizebytes': GaugeMetricFamily(
+                   'druid_' + daemon + '_query_cache_total_sizebytes',
+                   'Size in bytes of cache entries.'),
+            'query/cache/total/hits': GaugeMetricFamily(
+                   'druid_' + daemon + '_query_cache_total_hits',
+                   'Number of cache hits.'),
+            'query/cache/total/misses': GaugeMetricFamily(
+                   'druid_' + daemon + '_query_cache_total_misses',
+                   'Number of cache misses.'),
+            'query/cache/total/evictions': GaugeMetricFamily(
+                   'druid_' + daemon + '_query_cache_total_evictions',
+                   'Number of cache evictions.'),
+            'query/cache/total/timeouts': GaugeMetricFamily(
+                   'druid_' + daemon + '_query_cache_total_timeouts',
+                   'Number of cache timeouts.'),
+            'query/cache/total/errors': GaugeMetricFamily(
+                   'druid_' + daemon + '_query_cache_total_errors',
+                   'Number of cache errors.'),
+        }
+
+    def _get_historical_health_metrics(self):
+        return {
+            'segment/max': GaugeMetricFamily(
+                   'druid_historical_max_segment_bytes',
+                   'Maximum byte limit available for segments.'),
+            'segment/count': GaugeMetricFamily(
+                   'druid_historical_segment_count',
+                   'Number of served segments.'),
+            'segment/used': GaugeMetricFamily(
+                   'druid_historical_segment_used_bytes',
+                   'Bytes used for served segments.'),
+            'segment/pendingDelete': GaugeMetricFamily(
+                   'druid_historical_segment_pending_delete_bytes',
+                   'On-disk size in bytes of segments that are waiting '
+                   'to be cleared out.'),
+        }
+
+    @scrape_duration.time()
+    def collect(self):
+
+        # Metrics common to Broekr and Historical
+        for daemon in ['broker', 'historical']:
+            query_metrics = self._get_query_histograms(daemon)
+            cache_metrics = self._get_cache_counters(daemon)
+
+            for metric in query_metrics:
+                if not self.histograms[metric]:
+                    continue
+                if daemon in self.histograms[metric].keys():
+                    for datasource in self.histograms[metric][daemon]:
+                        query_metrics[daemon][metric].add_metric(
+                            [datasource],
+                            
buckets=self.histograms[metric][daemon][datasource].items(),
+                            sum_value=sum([v for (b, v) in
+                                           
self.histograms[metric][daemon][datasource].items()])
+                        )
+                yield query_metrics[daemon][metric]
+
+            for metric in cache_metrics:
+                if not self.counters[metric]:
+                    cache_metrics[metric].add_metric([], float('nan'))
+                else:
+                    cache_metrics[metric].add_metric([], self.counters[metric])
+                yield cache_metrics[metric]
+
+        # Metrics only available for Historical
+        # For more info check 
'io.druid.server.metrics.HistoricalMetricsMonitor'
+        # in http://druid.io/docs/0.9.2/configuration/index.html
+        historical_health_metrics = self._get_historical_health_metrics()
+        for metric in historical_health_metrics:
+            if not self.counters[metric]:
+                historical_health_metrics[metric].add_metric([], float('nan'))
+            else:
+                historical_health_metrics[metric].add_metric([], 
self.counters[metric])
+            yield historical_health_metrics[metric]
+
+    def register_datapoint(self, datapoint):
+        if datapoint['metric'] not in self.supported_metric_names:
+            return
+
+        # Transform the metric name into a metrics' key
+        daemon_name = str(datapoint['service'].replace('druid/', '').lower())
+        metric_name = str(datapoint['metric'].lower())
+        metric_value = float(datapoint['value'])
+
+        if daemon_name not in self.allowed_daemons:
+            log.error('Received metric from a daemon that is not allowed ({}), 
'
+                      'dropping it.'.format(daemon_name))
+            return
+
+        if metric_name in self.histograms:
+            datasource = str(datapoint['datasource'])
+            if not self.histograms[metric_name]:
+                self.histograms[metric_name] = {
+                    daemon_name: {datasource: {}}
+                }
+                for bucket in self.metric_buckets[metric_name]:
+                    
self.histograms[metric_name][daemon_name][datasource][bucket] = 0
+                    if metric_value <= float(bucket):
+                        
self.histograms[metric_name][daemon_name][datasource][bucket] += 1
+            else:
+                if daemon_name not in self.histograms[metric_name].keys():
+                    self.histograms[metric_name][daemon_name] = {datasource: 
{}}
+                else:
+                    if datasource not in 
self.histograms[metric_name][daemon_name].keys():
+                        self.histograms[metric_name][daemon_name][datasource] 
= {}
+                for bucket in self.metric_buckets[metric_name]:
+                    if metric_value <= float(bucket):
+                        if bucket not in 
self.histograms[metric_name][daemon_name][datasource].keys():
+                            
self.histograms[metric_name][daemon_name][datasource][bucket] = 1
+                        else:
+                            
self.histograms[metric_name][daemon_name][datasource][bucket] += 1
+        elif metric_name in self.counters:
+            # Override by default with the last value emitted by Druid
+            self.counters[metric_name] = metric_value
diff --git a/druid_exporter/exporter.py b/druid_exporter/exporter.py
new file mode 100644
index 0000000..e7ac398
--- /dev/null
+++ b/druid_exporter/exporter.py
@@ -0,0 +1,99 @@
+#!/usr/bin/python
+# Copyright 2017 Luca Toscano
+#                Wikimedia Foundation
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import print_function
+from __future__ import absolute_import
+import collector
+from prometheus_client import make_wsgi_app, REGISTRY
+from wsgiref.simple_server import make_server
+import argparse
+import json
+import logging
+import sys
+
+log = logging.getLogger(__name__)
+app = make_wsgi_app()
+
+SUPPORTED_DAEMONS = ['broker', 'historical']
+
+
+def druid_app(environ, start_response):
+    if environ['REQUEST_METHOD'] == 'POST':
+        try:
+            request_body_size = int(environ.get('CONTENT_LENGTH', 0))
+            request_body = environ['wsgi.input'].read(request_body_size)
+            datapoints = json.loads(request_body)
+            # The HTTP metrics emitter can batch datapoints and send them to
+            # a specific endpoint stated in the logs (this tool).
+            for datapoint in datapoints:
+                druid_collector.register_datapoint(datapoints)
+            status = '200 OK'
+            headers = [('Content-Type', 'text/plain')]
+        except Exception as e:
+            log.exception(
+                "Error while processing the following POST data: {}"
+                .format(request_body)
+            )
+            status = '400 Bad Request'
+        headers = [('Content-Type', 'text/plain')]
+        start_response(status, headers)
+        return ''
+    else:
+        return app(environ, start_response)
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-l', '--listen', metavar='ADDRESS',
+                        help='Listen on this address', default=':8000')
+    parser.add_argument('-d', '--debug', action='store_true',
+                        help='Enable debug logging')
+    parser.add_argument('-c', '--collect-from',
+                        type=str, default='all',
+                        help='Comma separated list of daemons to collect '
+                             'metrics from. Default to the special value 
\'all\', '
+                             'that corresponds to the list of supported 
daemons: {}'
+                             .format(SUPPORTED_DAEMONS))
+    args = parser.parse_args()
+
+    if args.debug:
+        logging.basicConfig(level=logging.DEBUG)
+    else:
+        logging.basicConfig(level=logging.WARNING)
+
+    collect_metrics_from = []
+
+    if args.collect_from == 'all':
+        daemons = SUPPORTED_DAEMONS
+    else:
+        daemons = args.collect_from.split(',')
+        for daemon in daemons:
+            if daemon not in SUPPORTED_DAEMONS:
+                log.error('The following daemon is not supported: 
{}'.format(daemon))
+                return 1
+
+    address, port = args.listen.split(':', 1)
+    log.info('Starting druid_exporter on %s:%s', address, port)
+
+    global druid_collector
+    druid_collector = collector.DruidCollector(daemons)
+    REGISTRY.register(druid_collector)
+
+    httpd = make_server(address, int(port), druid_app)
+    httpd.serve_forever()
+
+
+if __name__ == "__main__":
+    sys.exit(main())
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..ae7b22f
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,19 @@
+from setuptools import setup
+
+setup(name='druid_exporter',
+      version='0.1',
+      description='Prometheus exporter for Druid',
+      url='https://github.com/wikimedia/operations-software-druid_exporter',
+      author='Luca Toscano',
+      author_email='[email protected]',
+      license='Apache License, Version 2.0',
+      packages=['druid_exporter'],
+      install_requires=[
+          'prometheus-client',
+      ],
+      entry_points={
+          'console_scripts': [
+              'druid_exporter = druid_exporter.exporter:main'
+          ]
+      },
+)
diff --git a/test/test_collector.py b/test/test_collector.py
new file mode 100644
index 0000000..f3afa2b
--- /dev/null
+++ b/test/test_collector.py
@@ -0,0 +1,23 @@
+# Copyright 2017 Luca Toscano
+#                Wikimedia Foundation
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+import unittest
+
+from druid_exporter.collector import DruidCollector
+
+
+class TestDruidollector(unittest.TestCase):
+    def setUp(self):
+        self.collector = DruidCollector()
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000..d8c695f
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,8 @@
+[tox]
+envlist = py3,py2
+
+[testenv]
+deps=nose
+commands=
+  nosetests \
+          []  # substitute with tox' positional arguments

-- 
To view, visit https://gerrit.wikimedia.org/r/389475
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I90d3d84304c5f9f783e1b52afa203f04eb066a05
Gerrit-PatchSet: 1
Gerrit-Project: operations/software/druid_exporter
Gerrit-Branch: master
Gerrit-Owner: Elukey <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to