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

Change subject: WIP: add prometheus metrics
......................................................................

WIP: add prometheus metrics

Bug: T171710
Change-Id: I3fb22fce93d0f9fed814af3a6e8c7b7d07c9b135
---
M pybal/instrumentation.py
M pybal/monitor.py
M requirements.txt
M tox.ini
4 files changed, 26 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/debs/pybal 
refs/changes/62/370962/1

diff --git a/pybal/instrumentation.py b/pybal/instrumentation.py
index da04b11..e807f1a 100644
--- a/pybal/instrumentation.py
+++ b/pybal/instrumentation.py
@@ -16,6 +16,8 @@
 """
 
 from twisted.web.resource import Resource
+from prometheus_client.twisted import MetricsResource
+
 import json
 
 
@@ -50,6 +52,8 @@
             return PoolsRoot()
         if path == 'alerts':
             return Alerts()
+        if path == 'metrics':
+            return MetricsResource()
         else:
             return Resp404()
 
diff --git a/pybal/monitor.py b/pybal/monitor.py
index dd62b5a..686c8d9 100644
--- a/pybal/monitor.py
+++ b/pybal/monitor.py
@@ -8,6 +8,8 @@
 from . import util
 import logging
 
+from prometheus_client import Counter
+
 _log = util._log
 
 
@@ -16,6 +18,13 @@
     Base class for all monitoring protocols. Declares a few obligatory
     abstract methods, and some commonly useful functions.
     """
+
+    metric_labels = [ 'service', 'host', 'monitor' ]
+
+    metric_counters = {
+        'ups_total': Counter('ups_total', 'Monitor up event count', 
metric_labels),
+        'downs_total': Counter('downs_total', 'Monitor down event count', 
metric_labels),
+    }
 
     def __init__(self, coordinator, server, configuration={}, reactor=reactor):
         """Constructor"""
@@ -31,6 +40,12 @@
 
         # Install cleanup handler
         self.reactor.addSystemEventTrigger('before', 'shutdown', self.stop)
+
+        self.metric_labels = {
+            'service': self.server.lvsservice.name,
+            'host': self.server.host,
+            'monitor': self.name()
+        }
 
     def run(self):
         """Start the monitoring"""
@@ -55,6 +70,9 @@
             if self.coordinator:
                 self.coordinator.resultUp(self)
 
+            
self.metric_counters['ups_total'].labels(**self.metric_labels).inc()
+
+
     def _resultDown(self, reason=None):
         """Sets own monitoring state to Down and notifies the
         coordinator if this implies a state change."""
@@ -64,6 +82,8 @@
             if self.coordinator:
                 self.coordinator.resultDown(self, reason)
 
+            
self.metric_counters['downs_total'].labels(**self.metric_labels).inc()
+
     def report(self, text, level=logging.DEBUG):
         """Common method for reporting/logging check results."""
         msg = "%s (%s): %s" % (
diff --git a/requirements.txt b/requirements.txt
index 67d9e47..13e5974 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,2 +1,3 @@
 twisted
 PyOpenSSL
+prometheus_client
diff --git a/tox.ini b/tox.ini
index 3de1bc3..a0a04fe 100644
--- a/tox.ini
+++ b/tox.ini
@@ -10,6 +10,7 @@
   twisted
   mock
   PyOpenSSL
+  prometheus_client
 
 [testenv:cover]
 commands =

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3fb22fce93d0f9fed814af3a6e8c7b7d07c9b135
Gerrit-PatchSet: 1
Gerrit-Project: operations/debs/pybal
Gerrit-Branch: master
Gerrit-Owner: Ema <e...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to