Merge lp:~cmars/charm-helpers/add-metricenv into lp:charm-helpers

Proposed by Casey Marshall on 2017-01-30
Status: Merged
Merged at revision: 679
Proposed branch: lp:~cmars/charm-helpers/add-metricenv
Merge into: lp:charm-helpers
Diff against target: 74 lines (+59/-0)
2 files modified
charmhelpers/core/hookenv.py (+31/-0)
tests/core/test_hookenv.py (+28/-0)
To merge this branch: bzr merge lp:~cmars/charm-helpers/add-metricenv
Reviewer Review Type Date Requested Status
Marco Ceppi 2017-01-30 Approve on 2017-02-01
Chris Holcombe (community) Needs Fixing on 2017-01-31
Review via email: mp+315952@code.launchpad.net

Description of the Change

Add functions to charmhelpers.core.hookenv related to Juju Metrics (https://jujucharms.com/docs/stable/developer-metrics).

To post a comment you must log in.
679. By Casey Marshall on 2017-01-31

Add metrics functions to charmhelpers.core.hookenv.

Chris Holcombe (xfactor973) wrote :

Other than a little nit this looks ok to me.

review: Needs Fixing
680. By Casey Marshall on 2017-02-01

Catch EnvironmentError exception, covers subclasses such as IOError and OSError.

Casey Marshall (cmars) wrote :

Updated, PTAL

Marco Ceppi (marcoceppi) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/hookenv.py'
2--- charmhelpers/core/hookenv.py 2016-12-16 14:57:13 +0000
3+++ charmhelpers/core/hookenv.py 2017-02-01 00:13:09 +0000
4@@ -1035,3 +1035,34 @@
5 '''
6 cmd = ['network-get', '--primary-address', binding]
7 return subprocess.check_output(cmd).decode('UTF-8').strip()
8+
9+
10+def add_metric(*args, **kwargs):
11+ """Add metric values. Values may be expressed with keyword arguments. For
12+ metric names containing dashes, these may be expressed as one or more
13+ 'key=value' positional arguments. May only be called from the collect-metrics
14+ hook."""
15+ _args = ['add-metric']
16+ _kvpairs = []
17+ _kvpairs.extend(args)
18+ _kvpairs.extend(['{}={}'.format(k, v) for k, v in kwargs.items()])
19+ _args.extend(sorted(_kvpairs))
20+ try:
21+ subprocess.check_call(_args)
22+ return
23+ except EnvironmentError as e:
24+ if e.errno != errno.ENOENT:
25+ raise
26+ log_message = 'add-metric failed: {}'.format(' '.join(_kvpairs))
27+ log(log_message, level='INFO')
28+
29+
30+def meter_status():
31+ """Get the meter status, if running in the meter-status-changed hook."""
32+ return os.environ.get('JUJU_METER_STATUS')
33+
34+
35+def meter_info():
36+ """Get the meter status information, if running in the meter-status-changed
37+ hook."""
38+ return os.environ.get('JUJU_METER_INFO')
39
40=== modified file 'tests/core/test_hookenv.py'
41--- tests/core/test_hookenv.py 2016-12-16 14:57:13 +0000
42+++ tests/core/test_hookenv.py 2017-02-01 00:13:09 +0000
43@@ -1663,3 +1663,31 @@
44 check_output.side_effect = OSError(2, 'network-get')
45 self.assertRaises(NotImplementedError, hookenv.network_get_primary_address,
46 'mybinding')
47+
48+ @patch('subprocess.check_call')
49+ def test_add_metric(self, check_call_):
50+ hookenv.add_metric(flips='1.5', flops='2.1')
51+ hookenv.add_metric('juju-units=6')
52+ hookenv.add_metric('foo-bar=3.333', 'baz-quux=8', users='2')
53+ calls = [
54+ call(['add-metric', 'flips=1.5', 'flops=2.1']),
55+ call(['add-metric', 'juju-units=6']),
56+ call(['add-metric', 'baz-quux=8', 'foo-bar=3.333', 'users=2']),
57+ ]
58+ check_call_.assert_has_calls(calls)
59+
60+ @patch('subprocess.check_call')
61+ @patch.object(hookenv, 'log')
62+ def test_add_metric_enoent(self, log, _check_call):
63+ _check_call.side_effect = OSError(2, 'fail')
64+ hookenv.add_metric(flips='1')
65+ log.assert_called_with('add-metric failed: flips=1', level='INFO')
66+
67+ @patch('charmhelpers.core.hookenv.os')
68+ def test_meter_status(self, os_):
69+ os_.environ = {
70+ 'JUJU_METER_STATUS': 'GREEN',
71+ 'JUJU_METER_INFO': 'all good',
72+ }
73+ self.assertEqual(hookenv.meter_status(), 'GREEN')
74+ self.assertEqual(hookenv.meter_info(), 'all good')

Subscribers

People subscribed via source and target branches