123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- # -- coding: utf-8 --
- from __future__ import print_function
- import sys # noqa
- import json
- from tempfile import TemporaryFile
- import pytest # noqa
- from library.python.monlib.metric_registry import MetricRegistry, HistogramType
- from library.python.monlib.encoder import dump, dumps, TimePrecision, load, loads # noqa
- def test_common_labels(request):
- labels = {'my': 'label'}
- registry = MetricRegistry(labels)
- assert registry.common_labels == labels
- with pytest.raises(TypeError):
- MetricRegistry('foo')
- with pytest.raises(TypeError):
- MetricRegistry([])
- def test_json_serialization(request):
- registry = MetricRegistry()
- labels = {'foo': 'gauge'}
- g = registry.gauge(labels)
- g.set(10.0)
- g.set(20)
- c = registry.counter({'foo': 'counter'})
- c.inc()
- r = registry.rate({'foo': 'rate'})
- r.add(10)
- out = dumps(registry, format='json', precision=TimePrecision.Seconds)
- expected = json.loads("""{
- "sensors":
- [
- {
- "kind":"RATE",
- "labels":
- {
- "foo":"rate"
- },
- "value":10
- },
- {
- "kind":"COUNTER",
- "labels":
- {
- "foo":"counter"
- },
- "value":1
- },
- {
- "kind":"GAUGE",
- "labels":
- {
- "foo":"gauge"
- },
- "value":20
- }
- ]
- }
- """)
- j = json.loads(out)
- assert j == expected
- EXPECTED_EXPLICIT = json.loads("""
- {
- "sensors":
- [
- {
- "kind":"HIST",
- "labels":
- {
- "foo":"hist"
- },
- "hist":
- {
- "bounds":
- [
- 2,
- 10,
- 500
- ],
- "buckets":
- [
- 1,
- 0,
- 0
- ],
- "inf":1
- }
- }
- ]
- }
- """)
- EXPECTED_EXPONENTIAL = json.loads("""{
- "sensors":
- [
- {
- "kind":"HIST",
- "labels":
- {
- "foo":"hist"
- },
- "hist":
- {
- "bounds":
- [
- 3,
- 6,
- 12,
- 24,
- 48
- ],
- "buckets":
- [
- 1,
- 0,
- 0,
- 0,
- 0
- ],
- "inf":1
- }
- }
- ]
- }
- """)
- EXPECTED_LINEAR = json.loads("""
- { "sensors":
- [
- {
- "kind":"HIST",
- "labels":
- {
- "foo":"hist"
- },
- "hist":
- {
- "bounds":
- [
- 1
- ],
- "buckets":
- [
- 1
- ],
- "inf":1
- }
- }
- ]
- }""")
- @pytest.mark.parametrize('type,args,expected', [
- (HistogramType.Linear, dict(bucket_count=2, start_value=1, bucket_width=1), EXPECTED_LINEAR),
- (HistogramType.Explicit, dict(buckets=[2, 10, 500]), EXPECTED_EXPLICIT),
- (HistogramType.Exponential, dict(bucket_count=6, base=2, scale=3), EXPECTED_EXPONENTIAL),
- ])
- @pytest.mark.parametrize('rate', [True, False])
- def test_histograms(request, type, args, expected, rate):
- registry = MetricRegistry()
- labels = {'foo': 'hist'}
- h = registry.histogram_counter(labels, type, **args) if not rate else registry.histogram_rate(labels, type, **args)
- h.collect(1)
- h.collect(1000, 1)
- s = dumps(registry, format='json')
- if rate:
- expected['sensors'][0]['kind'] = u'HIST_RATE'
- else:
- expected['sensors'][0]['kind'] = u'HIST'
- assert json.loads(s) == expected
- @pytest.mark.parametrize('fmt', ['json', 'spack'])
- def test_stream_load(request, fmt):
- expected = json.loads("""{"sensors":[{"kind":"GAUGE","labels":{"foo":"gauge"},"value":42}]}""")
- registry = MetricRegistry()
- labels = {'foo': 'gauge'}
- g = registry.gauge(labels)
- g.set(42)
- with TemporaryFile() as f:
- dump(registry, f, format=fmt)
- f.flush()
- f.seek(0, 0)
- s = load(f, from_format=fmt, to_format='json')
- assert json.loads(s) == expected
- @pytest.mark.parametrize('fmt', ['json', 'spack'])
- def test_stream_loads(request, fmt):
- expected = json.loads("""{"sensors":[{"kind":"GAUGE","labels":{"foo":"gauge"},"value":42}]}""")
- registry = MetricRegistry()
- labels = {'foo': 'gauge'}
- g = registry.gauge(labels)
- g.set(42)
- s = dumps(registry, format=fmt)
- j = loads(s, from_format=fmt, to_format='json')
- assert json.loads(j) == expected
- @pytest.mark.parametrize('fmt', ['json', 'spack'])
- def test_utf(request, fmt):
- expected = json.loads(u"""{"sensors":[{"kind":"GAUGE","labels":{"foo":"gaugeह", "bàr":"Münich"},"value":42}]}""".encode('utf-8'))
- registry = MetricRegistry()
- labels = {'foo': u'gaugeह', u'bàr': u'Münich'}
- g = registry.gauge(labels)
- g.set(42)
- s = dumps(registry, format=fmt)
- j = loads(s, from_format=fmt, to_format='json')
- assert json.loads(j) == expected
- def test_gauge_sensors():
- registry = MetricRegistry()
- g = registry.gauge({'a': 'b'})
- ig = registry.int_gauge({'c': 'd'})
- g.set(2)
- assert g.add(3.5) == 5.5
- assert g.get() == 5.5
- ig.set(2)
- assert ig.inc() == 3
- assert ig.dec() == 2
- assert ig.add(3) == 5
- assert ig.get() == 5
- UNISTAT_DATA = """[
- ["signal1_max", 10],
- ["signal2_hgram", [[0, 100], [50, 200], [200, 300]]],
- ["prj=some-project;signal3_summ", 3],
- ["signal4_summ", 5]
- ]"""
- EXPECTED = json.loads("""
- {
- "sensors": [
- {
- "kind": "GAUGE",
- "labels": {
- "sensor": "signal1_max"
- },
- "value": 10
- },
- {
- "hist": {
- "buckets": [
- 0,
- 100,
- 200
- ],
- "bounds": [
- 0,
- 50,
- 200
- ],
- "inf": 300
- },
- "kind": "HIST_RATE",
- "labels": {
- "sensor": "signal2_hgram"
- }
- },
- {
- "kind": "RATE",
- "labels": {
- "sensor": "signal3_summ",
- "prj": "some-project"
- },
- "value": 3
- },
- {
- "kind": "RATE",
- "labels": {
- "sensor": "signal4_summ"
- },
- "value": 5
- }
- ]
- }""")
- def test_unistat_conversion(request):
- j = loads(UNISTAT_DATA, from_format='unistat', to_format='json')
- assert json.loads(j) == EXPECTED
|