Skip to content

Commit f2682f7

Browse files
author
Yahong Gu
committed
Merge branch 'develop'
* develop: Version bump to 0.3.1 Issue #19 fix lint issues on lines too long Issue #19 use internal private _metrics dict to track metrics added by add_sample method and merge to metrics list before posting element, this is O(1) instead of previous O(n) Rename check interval to TTL
2 parents 1e44b66 + b80c163 commit f2682f7

File tree

9 files changed

+60
-77
lines changed

9 files changed

+60
-77
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.3.0
2+
current_version = 0.3.1
33
commit = False
44
tag = False
55
files = setup.py netuitive/__init__.py

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ docs/_build
4545
# special
4646
testclient.py
4747
.coveralls.yml
48+
49+
## IDE
50+
.idea
51+
*.iml
52+
venv

netuitive/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
__author__ = 'Netuitive, Inc'
4-
__version__ = '0.3.0'
4+
__version__ = '0.3.1'
55

66
from .client import Client # nopep8 # flake8: noqa
77
from .element import Element # nopep8 # flake8: noqa

netuitive/check.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ class Check(object):
77
:type name: string
88
:param elementId: Associated Element ID
99
:type elementId: string
10-
:param interval: Check interval in seconds
11-
:type interval: int
10+
:param ttl: Check TTL in seconds
11+
:type ttl: int
1212
"""
1313

1414
def __init__(self,
1515
name,
1616
elementId,
17-
interval):
17+
ttl):
1818

1919
self.name = name
2020
self.elementId = elementId
21-
self.interval = interval
21+
self.ttl = ttl

netuitive/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def post(self, element):
6666
if element.id is None:
6767
raise Exception('element id is not set')
6868

69+
element.merge_metrics()
6970
payload = json.dumps(
7071
[element], default=lambda o: o.__dict__, sort_keys=True)
7172
logging.debug(payload)
@@ -165,7 +166,7 @@ def post_check(self, check):
165166
url = self.checkurl + '/' \
166167
+ check.name + '/' \
167168
+ check.elementId + '/' \
168-
+ str(check.interval)
169+
+ str(check.ttl)
169170
try:
170171
headers = {'User-Agent': self.agent}
171172
request = urllib2.Request(

netuitive/element.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,27 @@ def __init__(self, ElementType='SERVER', location=None):
2828
self.samples = []
2929
self.id = None
3030
self.name = None
31+
self._metrics = {}
3132

3233
if location is not None:
3334
self.location = location
3435

3536
def _sanitize(self, s):
3637
"""
3738
Sanitize the name of a metric to remove unwanted chars
38-
3939
"""
4040

4141
return re.sub('[^a-zA-Z0-9\\._-]', '_', s)
4242

43+
def merge_metrics(self):
44+
"""
45+
Merge metrics in the internal _metrics dict to metrics list
46+
and delete the internal _metrics
47+
"""
48+
49+
self.metrics.extend(self._metrics.values())
50+
del self._metrics
51+
4352
def add_attribute(self, name, value):
4453
"""
4554
:param name: Name of the attribute
@@ -134,23 +143,15 @@ def add_sample(self,
134143

135144
metricIdSan = self._sanitize(metricId)
136145

137-
if len(self.metrics) > 0:
138-
t = list(self.metrics)
146+
if self._metrics is None:
147+
self._metrics = {}
139148

140-
if metricIdSan not in t:
141-
self.metrics.append(
142-
Metric(metricIdSan,
143-
metricType,
144-
sparseDataStrategy,
145-
unit,
146-
Tags))
147-
else:
148-
self.metrics.append(
149-
Metric(metricIdSan,
150-
metricType,
151-
sparseDataStrategy,
152-
unit,
153-
Tags))
149+
if self._metrics.get(metricIdSan) is None:
150+
self._metrics[metricIdSan] = Metric(metricIdSan,
151+
metricType,
152+
sparseDataStrategy,
153+
unit,
154+
Tags)
154155

155156
if timestamp is None:
156157
ts = to_ms_timestamp_int(datetime.datetime.utcnow())
@@ -172,4 +173,5 @@ def add_sample(self,
172173

173174
def clear_samples(self):
174175
self.metrics = []
176+
self._metrics = {}
175177
self.samples = []

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
setup(
2626
name='netuitive',
27-
version='0.3.0',
27+
version='0.3.1',
2828
description="Python Client for Netuitive Cloud",
2929
long_description=readme + '\n\n' + history,
3030
author="Netuitive",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"attributes": [], "id": "hostname", "metrics": [{"id": "nonsparseDataStrategy", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}, {"id": "sparseDataStrategy", "sparseDataStrategy": "ReplaceWithZero", "type": "COUNTER", "unit": ""}, {"id": "unit", "sparseDataStrategy": "None", "type": "COUNTER", "unit": "Bytes"}, {"id": "nonunit", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}, {"id": "tagged", "sparseDataStrategy": "None", "tags": [{"name": "utilization", "value": "true"}], "type": "COUNTER", "unit": ""}, {"id": "min", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}, {"id": "max", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}, {"id": "avg", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}, {"id": "sum", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}, {"id": "cnt", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}, {"id": "min.max.avg.sum.cnt", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}, {"id": "cnt2", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}], "name": "hostname", "relations": [], "samples": [{"metricId": "nonsparseDataStrategy", "timestamp": 1434110794000, "val": 1}, {"metricId": "sparseDataStrategy", "timestamp": 1434110794000, "val": 1}, {"metricId": "unit", "timestamp": 1434110794000, "val": 1}, {"metricId": "nonunit", "timestamp": 1434110794000, "val": 1}, {"metricId": "tagged", "timestamp": 1434110794000, "val": 1}, {"metricId": "min", "min": 0, "timestamp": 1434110794000, "val": 1}, {"max": 100, "metricId": "max", "timestamp": 1434110794000, "val": 1}, {"avg": 50, "metricId": "avg", "timestamp": 1434110794000, "val": 1}, {"metricId": "sum", "sum": 2, "timestamp": 1434110794000, "val": 1}, {"cnt": 3, "metricId": "cnt", "timestamp": 1434110794000, "val": 1}, {"avg": 50, "cnt": 3, "max": 100, "metricId": "min.max.avg.sum.cnt", "min": 0, "sum": 2, "timestamp": 1434110794000, "val": 1}, {"cnt": 3, "metricId": "cnt2", "timestamp": 1475158966202, "val": 1}], "tags": [], "type": "SERVER"}]
1+
[{"attributes": [], "id": "hostname", "metrics": [{"id": "min.max.avg.sum.cnt", "sparseDataStrategy": "None", "type": "COUNTER", "unit": ""}], "name": "hostname", "relations": [], "samples": [{"avg": 50, "cnt": 3, "max": 100, "metricId": "min.max.avg.sum.cnt", "min": 0, "sum": 2, "timestamp": 1434110794000, "val": 1}], "tags": [], "type": "SERVER"}]

tests/test_netuitive.py

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def test_element_localtion(self):
133133
def test_post_format(self):
134134

135135
a = netuitive.Element('SERVER', 'here')
136+
a.merge_metrics()
136137
ajson = json.dumps(
137138
[a], default=lambda o: o.__dict__, sort_keys=True)
138139

@@ -157,7 +158,7 @@ def test(self):
157158
self.assertEqual(self.a.attributes[1].value, 'TestValue2')
158159

159160
def test_post_format(self):
160-
161+
self.a.merge_metrics()
161162
ajson = json.dumps(
162163
[self.a], default=lambda o: o.__dict__, sort_keys=True)
163164

@@ -180,7 +181,7 @@ def test(self):
180181
self.assertEqual(self.a.relations[1].fqn, 'Test2')
181182

182183
def test_post_format(self):
183-
184+
self.a.merge_metrics()
184185
ajson = json.dumps(
185186
[self.a], default=lambda o: o.__dict__, sort_keys=True)
186187

@@ -220,8 +221,8 @@ def test_add_sample(self):
220221
self.assertEqual(a.id, 'hostname')
221222
self.assertEqual(a.name, 'hostname')
222223

223-
self.assertEqual(a.metrics[0].id, 'metricId')
224-
self.assertEqual(a.metrics[0].type, 'COUNTER')
224+
self.assertEqual(a._metrics['metricId'].id, 'metricId')
225+
self.assertEqual(a._metrics['metricId'].type, 'COUNTER')
225226

226227
def test_add_sample_with_tags(self):
227228
a = netuitive.Element()
@@ -231,32 +232,40 @@ def test_add_sample_with_tags(self):
231232
self.assertEqual(a.id, 'hostname')
232233
self.assertEqual(a.name, 'hostname')
233234

234-
self.assertEqual(a.metrics[0].id, 'tagged')
235-
self.assertEqual(a.metrics[0].type, 'COUNTER')
236-
self.assertEqual(a.metrics[0].tags[0].name, 'utilization')
237-
self.assertEqual(a.metrics[0].tags[0].value, 'true')
235+
self.assertEqual(a._metrics['tagged'].id, 'tagged')
236+
self.assertEqual(a._metrics['tagged'].type, 'COUNTER')
237+
self.assertEqual(a._metrics['tagged'].tags[0].name, 'utilization')
238+
self.assertEqual(a._metrics['tagged'].tags[0].value, 'true')
238239

239240
def test_duplicate_metrics(self):
240241
a = netuitive.Element()
241242

242243
a.add_sample(
243244
'metricId', 1434110794, 1, 'COUNTER', host='hostname')
245+
a.add_sample(
246+
'metricId', 1434110795, 2, 'COUNTER', host='hostname')
244247

245248
# don't allow duplicate metrics
246-
self.assertEqual(len(a.metrics), 1)
249+
self.assertEqual(len(a._metrics), 1)
250+
self.assertEqual(a._metrics['metricId'].id, 'metricId')
251+
self.assertEqual(a._metrics['metricId'].type, 'COUNTER')
247252

248253
self.assertEqual(a.samples[0].metricId, 'metricId')
249254
self.assertEqual(a.samples[0].timestamp, 1434110794000)
250255
self.assertEqual(a.samples[0].val, 1)
256+
self.assertEqual(a.samples[1].metricId, 'metricId')
257+
self.assertEqual(a.samples[1].timestamp, 1434110795000)
258+
self.assertEqual(a.samples[1].val, 2)
251259

252260
def test_clear_samples(self):
253261
a = netuitive.Element()
254262
a.add_sample(
255263
'metricId', 1434110794, 1, 'COUNTER', host='hostname')
256264
# test clear_samples
257-
self.assertEqual(len(a.metrics), 1)
265+
self.assertEqual(len(a._metrics), 1)
258266
a.clear_samples()
259267
self.assertEqual(len(a.metrics), 0)
268+
self.assertEqual(len(a._metrics), 0)
260269
self.assertEqual(len(a.samples), 0)
261270

262271
def test_with_sparseDataStrategy(self):
@@ -268,9 +277,8 @@ def test_with_sparseDataStrategy(self):
268277
a.add_sample(
269278
'sparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname', sparseDataStrategy='ReplaceWithZero')
270279

271-
self.assertEqual(a.metrics[0].sparseDataStrategy, 'None')
272-
self.assertEqual(
273-
a.metrics[1].sparseDataStrategy, 'ReplaceWithZero')
280+
self.assertEqual(a._metrics['nonsparseDataStrategy'].sparseDataStrategy, 'None')
281+
self.assertEqual(a._metrics['sparseDataStrategy'].sparseDataStrategy, 'ReplaceWithZero')
274282

275283
a.clear_samples()
276284

@@ -284,10 +292,9 @@ def test_with_unit(self):
284292
a.add_sample(
285293
'nonunit', 1434110794, 1, 'COUNTER', host='hostname')
286294

287-
self.assertEqual(
288-
a.metrics[0].unit, 'Bytes')
295+
self.assertEqual(a._metrics['unit'].unit, 'Bytes')
289296

290-
self.assertEqual(a.metrics[1].unit, '')
297+
self.assertEqual(a._metrics['nonunit'].unit, '')
291298

292299
def test_with_min(self):
293300
a = netuitive.Element()
@@ -339,47 +346,15 @@ def test_add_sanitize(self):
339346
a.add_sample(
340347
'mongo.wiredTiger.cache.eviction$server populating queue,:but not evicting pages', 1434110794, 1, 'COUNTER', host='hostname')
341348

342-
self.assertEqual(a.metrics[
343-
0].id, 'mongo.wiredTiger.cache.eviction_server_populating_queue__but_not_evicting_pages')
349+
self.assertEqual(a._metrics['mongo.wiredTiger.cache.eviction_server_populating_queue__but_not_evicting_pages'].id, 'mongo.wiredTiger.cache.eviction_server_populating_queue__but_not_evicting_pages')
344350

345351
def test_post_format(self):
346352
a = netuitive.Element()
347353

348-
a.add_sample(
349-
'nonsparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname')
350-
a.add_sample(
351-
'sparseDataStrategy', 1434110794, 1, 'COUNTER', host='hostname', sparseDataStrategy='ReplaceWithZero')
352-
353-
a.add_sample(
354-
'unit', 1434110794, 1, 'COUNTER', host='hostname', unit='Bytes')
355-
356-
a.add_sample(
357-
'nonunit', 1434110794, 1, 'COUNTER', host='hostname')
358-
359-
a.add_sample(
360-
'tagged', 1434110794, 1, 'COUNTER', host='hostname', tags=[{'utilization': 'true'}])
361-
362-
a.add_sample(
363-
'min', 1434110794, 1, 'COUNTER', host='hostname', min=0)
364-
365-
a.add_sample(
366-
'max', 1434110794, 1, 'COUNTER', host='hostname', max=100)
367-
368-
a.add_sample(
369-
'avg', 1434110794, 1, 'COUNTER', host='hostname', avg=50)
370-
371-
a.add_sample(
372-
'sum', 1434110794, 1, 'COUNTER', host='hostname', sum=2)
373-
374-
a.add_sample(
375-
'cnt', 1434110794, 1, 'COUNTER', host='hostname', cnt=3)
376-
377354
a.add_sample(
378355
'min.max.avg.sum.cnt', 1434110794, 1, 'COUNTER', host='hostname', min=0, max=100, avg=50, sum=2, cnt=3)
379356

380-
a.add_sample(
381-
'cnt2', 1475158966202, 1, 'COUNTER', host='hostname', cnt=3, ts_is_ms=True)
382-
357+
a.merge_metrics()
383358
ajson = json.dumps(
384359
[a], default=lambda o: o.__dict__, sort_keys=True)
385360

@@ -518,7 +493,7 @@ def test_check(self):
518493

519494
self.assertEqual(self.check.name, 'checkName')
520495
self.assertEqual(self.check.elementId, 'elementId')
521-
self.assertEqual(self.check.interval, 60)
496+
self.assertEqual(self.check.ttl, 60)
522497

523498
def tearDown(self):
524499
pass

0 commit comments

Comments
 (0)