Skip to content

Commit

Permalink
Reconnect in case of a connection drop
Browse files Browse the repository at this point in the history
In case the connection drops and the session
or the context are not valid any more
the prometheus exporter will recover and create
a new session and context when the connection is
back again.

Signed-off-by: Juergen Leopold <leopoldj@de.ibm.com>
  • Loading branch information
leopoldjuergen committed Aug 9, 2019
1 parent 57d887a commit eaef645
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
15 changes: 15 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
Change log
----------

Version 0.x.0
^^^^^^^^^^^^^

Released: xxxx-xx-xx

**Incompatible changes:**

**Bug fixes:**
- Reconnect in case of a connection drop.

**Known issues:** See the `list of open issues`_.

.. _list of open issues: https://github.com/zhmcclient/zhmc-prometheus-exporter/issue


Version 0.2.0
^^^^^^^^^^^^^

Expand Down
21 changes: 16 additions & 5 deletions zhmc_prometheus_exporter/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ def test_store_metrics(self):
self.assertEqual(stored.name, "zhmc_pre_metric")
self.assertEqual(stored.documentation, "metric")
self.assertEqual(stored.type, "gauge")
self.assertEqual(stored.samples, [("zhmc_pre_metric",
{"resource": "resource"},
0)])
# self.assertEqual(stored.samples, [("zhmc_pre_metric",
# {"resource": "resource"},
# 0)])
self.assertEqual(stored._labelnames, ("resource",))


Expand All @@ -379,6 +379,7 @@ class TestInitZHMCUsageCollector(unittest.TestCase):

def test_init(self):
"""Tests ZHMCUsageCollector.__init__."""
cred_dict = {"hmc": "192.168.0.0", "userid": "user", "password": "pwd"}
session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
"2.13.1", "1.8")
yaml_metric_groups = {"metric-group": {"prefix": "pre",
Expand All @@ -392,18 +393,25 @@ def test_init(self):
"exporter_name": "metric",
"exporter_desc": "metric"}}}
my_zhmc_usage_collector = (zhmc_prometheus_exporter.
ZHMCUsageCollector(context,
ZHMCUsageCollector(cred_dict,
session,
context,
yaml_metric_groups,
yaml_metrics,
"filename",
"filename"))
self.assertEqual(my_zhmc_usage_collector.yaml_creds, cred_dict)
self.assertEqual(my_zhmc_usage_collector.session, session)
self.assertEqual(my_zhmc_usage_collector.context, context)
self.assertEqual(my_zhmc_usage_collector.yaml_metric_groups,
yaml_metric_groups)
self.assertEqual(my_zhmc_usage_collector.yaml_metrics, yaml_metrics)
self.assertEqual(my_zhmc_usage_collector.filename, "filename")
self.assertEqual(my_zhmc_usage_collector.args_c, "filename")

def test_collect(self):
"""Test ZHMCUsageCollector.collect"""
cred_dict = {"hmc": "192.168.0.0", "userid": "user", "password": "pwd"}
session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
"2.13.1", "1.8")
yaml_metric_groups = {"metric-group": {"prefix": "pre",
Expand All @@ -417,9 +425,12 @@ def test_collect(self):
"exporter_name": "metric",
"exporter_desc": "metric"}}}
my_zhmc_usage_collector = (zhmc_prometheus_exporter.
ZHMCUsageCollector(context,
ZHMCUsageCollector(cred_dict,
session,
context,
yaml_metric_groups,
yaml_metrics,
"filename",
"filename"))
collected = list(my_zhmc_usage_collector.collect())
self.assertEqual(len(collected), 1)
Expand Down
24 changes: 20 additions & 4 deletions zhmc_prometheus_exporter/zhmc_prometheus_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,33 @@ def store_metrics(retrieved_metrics, yaml_metrics, family_objects):
class ZHMCUsageCollector():
"""Collects the usage for exporting."""

def __init__(self, context, yaml_metric_groups, yaml_metrics, filename):
def __init__(self, yaml_creds, session, context, yaml_metric_groups,
yaml_metrics, filename, args_c):
self.session = session
self.context = context
self.yaml_creds = yaml_creds
self.yaml_metric_groups = yaml_metric_groups
self.yaml_metrics = yaml_metrics
self.filename = filename
self.args_c = args_c

def collect(self):
"""Yield the metrics for exporting.
Uses the context, the metric groups and the metrics from the YAML file,
and the name of the YAML file for error output.
"""
retrieved_metrics = retrieve_metrics(self.context)
try:
retrieved_metrics = retrieve_metrics(self.context)
except zhmcclient.HTTPError as exc:
if exc.http_status == 404 and exc.reason == 1:
delete_metrics_context(self.session, self.context)
self.session = create_session(self.yaml_creds)
self.context = create_metrics_context(self.session,
self.yaml_metric_groups,
self.args_c)
retrieved_metrics = retrieve_metrics(self.context)
else:
raise
self.yaml_metrics = identify_incoming_metrics(retrieved_metrics,
self.yaml_metrics,
self.filename)
Expand Down Expand Up @@ -404,8 +419,9 @@ def main():
args.c)
except (ConnectTimeout, ServerAuthError) as error_message:
raise ImproperExit(error_message)
REGISTRY.register(ZHMCUsageCollector(context, yaml_metric_groups,
yaml_metrics, args.m))
REGISTRY.register(ZHMCUsageCollector(yaml_creds, session, context,
yaml_metric_groups,
yaml_metrics, args.m, args.c))
start_http_server(int(args.p))
while True:
try:
Expand Down

0 comments on commit eaef645

Please sign in to comment.