diff options
author | robot-ydb-importer <robot-ydb-importer@yandex-team.com> | 2024-02-14 19:47:36 +0300 |
---|---|---|
committer | robot-ydb-importer <robot-ydb-importer@yandex-team.com> | 2024-02-14 21:51:48 +0300 |
commit | ccc9ad1a6914b4cce50935b1b3fd868ed69fed13 (patch) | |
tree | 9dea935eaf96e944bf8262a295eb8bccb7bce077 /library/python/monlib/ut/metric_ut.pyx | |
parent | 37ca0ae098448d6f7d13b7c651f38c282915ad3a (diff) | |
download | ydb-ccc9ad1a6914b4cce50935b1b3fd868ed69fed13.tar.gz |
YDB Import 566
96265cd0cc64e1b9bb31fe97b915ed2a09caf1cb
Diffstat (limited to 'library/python/monlib/ut/metric_ut.pyx')
-rw-r--r-- | library/python/monlib/ut/metric_ut.pyx | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/library/python/monlib/ut/metric_ut.pyx b/library/python/monlib/ut/metric_ut.pyx new file mode 100644 index 0000000000..3513eaf9d1 --- /dev/null +++ b/library/python/monlib/ut/metric_ut.pyx @@ -0,0 +1,113 @@ +from library.python.monlib.labels cimport TLabels, TLabel +from library.python.monlib.metric cimport ( + TGauge, TCounter, + TRate, THistogram, + IHistogramCollectorPtr, ExponentialHistogram, + IHistogramSnapshotPtr +) + +from library.python.monlib.metric_registry cimport TMetricRegistry + +from util.generic.string cimport TStringBuf, TString +from util.generic.maybe cimport TMaybe +from util.generic.ptr cimport THolder + +from cython.operator cimport dereference as deref + +import pytest +import unittest + + +cdef extern from "<utility>" namespace "std" nogil: + cdef IHistogramCollectorPtr&& move(IHistogramCollectorPtr t) + + +class TestMetric(unittest.TestCase): + def test_labels(self): + cdef TLabels labels = TLabels() + cdef TString name = "foo" + cdef TString value = "bar" + + labels.Add(name, value) + + cdef TMaybe[TLabel] label = labels.Find(name) + + assert label.Defined() + assert label.GetRef().Name() == "foo" + assert label.GetRef().Value() == "bar" + + def test_metric_registry(self): + cdef TLabels labels = TLabels() + + labels.Add(TString("common"), TString("label")) + + cdef THolder[TMetricRegistry] registry + registry.Reset(new TMetricRegistry(labels)) + + assert deref(registry.Get()).CommonLabels() == labels + + cdef TLabels metric_labels = TLabels() + metric_labels.Add(TString("name"), TString("gauge")) + g = deref(registry.Get()).Gauge(metric_labels) + assert g.Get() == 0. + + metric_labels = TLabels() + metric_labels.Add(TString("name"), TString("counter")) + c = deref(registry.Get()).Counter(metric_labels) + assert c.Get() == 0. + + metric_labels = TLabels() + metric_labels.Add(TString("name"), TString("rate")) + r = deref(registry.Get()).Rate(metric_labels) + assert r.Get() == 0. + + metric_labels = TLabels() + metric_labels.Add(TString("name"), TString("int_gauge")) + ig = deref(registry.Get()).IntGauge(metric_labels) + assert ig.Get() == 0 + + def test_metric_registry_throws_on_duplicate(self): + cdef THolder[TMetricRegistry] registry + registry.Reset(new TMetricRegistry()) + + cdef TLabels metric_labels = TLabels() + metric_labels.Add(TString("my"), TString("metric")) + g = deref(registry.Get()).Gauge(metric_labels) + with pytest.raises(RuntimeError): + deref(registry.Get()).Counter(metric_labels) + + def test_counter_histogram(self): + cdef THolder[TMetricRegistry] registry + registry.Reset(new TMetricRegistry()) + + cdef TLabels metric_labels = TLabels() + metric_labels.Add(TString("name"), TString("histogram")) + + cdef IHistogramCollectorPtr collector = move(ExponentialHistogram(6, 2, 3)) + collector_ptr = collector.Get() + hist = registry.Get().HistogramCounter(metric_labels, move(collector)) + hist.Record(1) + hist.Record(1000, 4) + + cdef IHistogramSnapshotPtr snapshot = collector_ptr.Snapshot() + assert deref(snapshot.Get()).Count() == 6 + assert snapshot.Get().Value(0) == 1 + + def test_rate_histogram(self): + cdef THolder[TMetricRegistry] registry + registry.Reset(new TMetricRegistry()) + + cdef TLabels metric_labels = TLabels() + metric_labels.Add(TString("name"), TString("histogram")) + + cdef IHistogramCollectorPtr collector = move(ExponentialHistogram(6, 2, 3)) + collector_ptr = collector.Get() + hist = registry.Get().HistogramRate(metric_labels, move(collector)) + hist.Record(1) + hist.Record(1000, 4) + + cdef IHistogramSnapshotPtr snapshot = collector_ptr.Snapshot() + assert deref(snapshot.Get()).Count() == 6 + assert snapshot.Get().Value(0) == 1 + assert snapshot.Get().Value(5) == 4 + assert snapshot.Get().Value(5) == 4 |