Merge ~andreserl/maas:prometheus_minor_reorg into maas:master

Proposed by Andres Rodriguez
Status: Merged
Approved by: Andres Rodriguez
Approved revision: 62aa33182e89a3677480821724d7637dc09ab0ca
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~andreserl/maas:prometheus_minor_reorg
Merge into: maas:master
Diff against target: 170 lines (+62/-12)
4 files modified
src/maasserver/prometheus.py (+6/-1)
src/maasserver/stats.py (+13/-4)
src/maasserver/tests/test_prometheus.py (+20/-1)
src/maasserver/tests/test_stats.py (+23/-6)
Reviewer Review Type Date Requested Status
Lee Trager (community) Approve
MAAS Lander unittests Pending
Review via email: mp+356768@code.launchpad.net

Commit message

Minor code re-org and addition of statuses to later use as a base for a /metrics endpoint.

Description of the change

This is a re-org which was originally part of https://code.launchpad.net/~andreserl/maas/+git/maas/+merge/356087 but decided to do this so I can start adding more stats without having later conflicts.

To post a comment you must log in.
62aa331... by Andres Rodriguez

Improvement

Revision history for this message
Lee Trager (ltrager) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/prometheus.py b/src/maasserver/prometheus.py
2index f736de9..d30aad3 100644
3--- a/src/maasserver/prometheus.py
4+++ b/src/maasserver/prometheus.py
5@@ -32,7 +32,7 @@ except:
6 log = LegacyLogger()
7
8
9-def push_stats_to_prometheus(maas_name, push_gateway):
10+def get_stats_for_prometheus():
11 registry = CollectorRegistry()
12 stats = json.loads(get_maas_stats())
13
14@@ -43,6 +43,11 @@ def push_stats_to_prometheus(maas_name, push_gateway):
15 for status, machines in stats['machine_status'].items():
16 counter.labels(status).set(machines)
17
18+ return registry
19+
20+
21+def push_stats_to_prometheus(maas_name, push_gateway):
22+ registry = get_stats_for_prometheus()
23 push_to_gateway(
24 push_gateway, job='stats_for_%s' % maas_name, registry=registry)
25
26diff --git a/src/maasserver/stats.py b/src/maasserver/stats.py
27index d0544b9..5f43bdc 100644
28--- a/src/maasserver/stats.py
29+++ b/src/maasserver/stats.py
30@@ -53,14 +53,23 @@ def get_machine_state_stats():
31 node_status = Counter(node_status)
32
33 return {
34+ # base status
35+ "new": node_status.get(NODE_STATUS.NEW, 0),
36 "ready": node_status.get(NODE_STATUS.READY, 0),
37 "allocated": node_status.get(NODE_STATUS.ALLOCATED, 0),
38- "deploying": node_status.get(NODE_STATUS.DEPLOYING, 0),
39 "deployed": node_status.get(NODE_STATUS.DEPLOYED, 0),
40+ # in progress status
41+ "commissioning": node_status.get(NODE_STATUS.COMMISSIONING, 0),
42+ "testing": node_status.get(NODE_STATUS.TESTING, 0),
43+ "deploying": node_status.get(NODE_STATUS.DEPLOYING, 0),
44+ # failure status
45 "failed_deployment": node_status.get(
46 NODE_STATUS.FAILED_DEPLOYMENT, 0),
47 "failed_commissioning": node_status.get(
48 NODE_STATUS.FAILED_COMMISSIONING, 0),
49+ "failed_testing": node_status.get(
50+ NODE_STATUS.FAILED_TESTING, 0),
51+ "broken": node_status.get(NODE_STATUS.BROKEN, 0),
52 }
53
54
55@@ -102,9 +111,9 @@ def get_maas_stats():
56 "machines": node_types.get(NODE_TYPE.MACHINE, 0),
57 "devices": node_types.get(NODE_TYPE.DEVICE, 0),
58 },
59- "machine_stats": stats,
60- "machine_status": machine_status,
61- "network_stats": netstats,
62+ "machine_stats": stats, # count of cpus, mem, storage
63+ "machine_status": machine_status, # machines by status
64+ "network_stats": netstats, # network status
65 })
66
67
68diff --git a/src/maasserver/tests/test_prometheus.py b/src/maasserver/tests/test_prometheus.py
69index 27dc7f4..66c1af6 100644
70--- a/src/maasserver/tests/test_prometheus.py
71+++ b/src/maasserver/tests/test_prometheus.py
72@@ -5,10 +5,15 @@
73
74 __all__ = []
75
76+import json
77+
78 from django.db import transaction
79 from maasserver import prometheus
80 from maasserver.models import Config
81-from maasserver.prometheus import push_stats_to_prometheus
82+from maasserver.prometheus import (
83+ get_stats_for_prometheus,
84+ push_stats_to_prometheus,
85+)
86 from maasserver.testing.factory import factory
87 from maasserver.testing.testcase import (
88 MAASServerTestCase,
89@@ -28,6 +33,20 @@ from twisted.internet.defer import fail
90
91 class TestPrometheus(MAASServerTestCase):
92
93+ def test_get_stats_for_prometheus(self):
94+ self.patch(prometheus, "CollectorRegistry")
95+ self.patch(prometheus, "Gauge")
96+ values = {
97+ "machine_status": {
98+ "random_status": 0,
99+ },
100+ }
101+ mock = self.patch(prometheus, "get_maas_stats")
102+ mock.return_value = json.dumps(values)
103+ get_stats_for_prometheus()
104+ self.assertThat(
105+ mock, MockCalledOnce())
106+
107 def test_push_stats_to_prometheus(self):
108 factory.make_RegionRackController()
109 maas_name = 'random.maas'
110diff --git a/src/maasserver/tests/test_stats.py b/src/maasserver/tests/test_stats.py
111index ee7a657..7f27ba0 100644
112--- a/src/maasserver/tests/test_stats.py
113+++ b/src/maasserver/tests/test_stats.py
114@@ -6,13 +6,16 @@
115 __all__ = []
116
117 import base64
118+from collections import Counter
119 import json
120
121 from django.db import transaction
122 from maasserver import stats
123+from maasserver.enum import NODE_STATUS
124 from maasserver.models import (
125 Config,
126 Fabric,
127+ Node,
128 Space,
129 Subnet,
130 VLAN,
131@@ -63,6 +66,9 @@ class TestMAASStats(MAASServerTestCase):
132 # calculates, so just get it directly from the database for the test.
133 total_storage = machine_stats['total_storage']
134
135+ node_status = Node.objects.values_list('status', flat=True)
136+ node_status = Counter(node_status)
137+
138 compare = {
139 "controllers": {
140 "regionracks": 1,
141@@ -79,12 +85,23 @@ class TestMAASStats(MAASServerTestCase):
142 "total_storage": total_storage,
143 },
144 "machine_status": {
145- "ready": 1,
146- "allocated": 0,
147- "deploying": 0,
148- "deployed": 0,
149- "failed_deployment": 1,
150- "failed_commissioning": 0,
151+ "new": node_status.get(NODE_STATUS.NEW, 0),
152+ "ready": node_status.get(NODE_STATUS.READY, 0),
153+ "allocated": node_status.get(NODE_STATUS.ALLOCATED, 0),
154+ "deployed": node_status.get(NODE_STATUS.DEPLOYED, 0),
155+ "commissioning": node_status.get(
156+ NODE_STATUS.COMMISSIONING, 0),
157+ "testing": node_status.get(
158+ NODE_STATUS.TESTING, 0),
159+ "deploying": node_status.get(
160+ NODE_STATUS.DEPLOYING, 0),
161+ "failed_deployment": node_status.get(
162+ NODE_STATUS.FAILED_DEPLOYMENT, 0),
163+ "failed_commissioning": node_status.get(
164+ NODE_STATUS.COMMISSIONING, 0),
165+ "failed_testing": node_status.get(
166+ NODE_STATUS.FAILED_TESTING, 0),
167+ "broken": node_status.get(NODE_STATUS.BROKEN, 0),
168 },
169 "network_stats": {
170 "spaces": Space.objects.count(),

Subscribers

People subscribed via source and target branches