metric_ut.pyx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from library.python.monlib.labels cimport TLabels, TLabel
  2. from library.python.monlib.metric cimport (
  3. TGauge, TCounter,
  4. TRate, THistogram,
  5. IHistogramCollectorPtr, ExponentialHistogram,
  6. IHistogramSnapshotPtr
  7. )
  8. from library.python.monlib.metric_registry cimport TMetricRegistry
  9. from util.generic.string cimport TStringBuf, TString
  10. from util.generic.maybe cimport TMaybe
  11. from util.generic.ptr cimport THolder
  12. from libcpp.utility cimport move
  13. from cython.operator cimport dereference as deref
  14. import pytest
  15. import unittest
  16. class TestMetric(unittest.TestCase):
  17. def test_labels(self):
  18. cdef TLabels labels = TLabels()
  19. cdef TString name = "foo"
  20. cdef TString value = "bar"
  21. labels.Add(name, value)
  22. cdef TMaybe[TLabel] label = labels.Find(name)
  23. assert label.Defined()
  24. assert label.GetRef().Name() == "foo"
  25. assert label.GetRef().Value() == "bar"
  26. def test_metric_registry(self):
  27. cdef TLabels labels = TLabels()
  28. labels.Add(TString("common"), TString("label"))
  29. cdef THolder[TMetricRegistry] registry
  30. registry.Reset(new TMetricRegistry(labels))
  31. assert deref(registry.Get()).CommonLabels() == labels
  32. cdef TLabels metric_labels = TLabels()
  33. metric_labels.Add(TString("name"), TString("gauge"))
  34. g = deref(registry.Get()).Gauge(metric_labels)
  35. assert g.Get() == 0.
  36. metric_labels = TLabels()
  37. metric_labels.Add(TString("name"), TString("counter"))
  38. c = deref(registry.Get()).Counter(metric_labels)
  39. assert c.Get() == 0.
  40. metric_labels = TLabels()
  41. metric_labels.Add(TString("name"), TString("rate"))
  42. r = deref(registry.Get()).Rate(metric_labels)
  43. assert r.Get() == 0.
  44. metric_labels = TLabels()
  45. metric_labels.Add(TString("name"), TString("int_gauge"))
  46. ig = deref(registry.Get()).IntGauge(metric_labels)
  47. assert ig.Get() == 0
  48. def test_metric_registry_throws_on_duplicate(self):
  49. cdef THolder[TMetricRegistry] registry
  50. registry.Reset(new TMetricRegistry())
  51. cdef TLabels metric_labels = TLabels()
  52. metric_labels.Add(TString("my"), TString("metric"))
  53. g = deref(registry.Get()).Gauge(metric_labels)
  54. with pytest.raises(RuntimeError):
  55. deref(registry.Get()).Counter(metric_labels)
  56. def test_counter_histogram(self):
  57. cdef THolder[TMetricRegistry] registry
  58. registry.Reset(new TMetricRegistry())
  59. cdef TLabels metric_labels = TLabels()
  60. metric_labels.Add(TString("name"), TString("histogram"))
  61. cdef IHistogramCollectorPtr collector = move(ExponentialHistogram(6, 2, 3))
  62. collector_ptr = collector.Get()
  63. hist = registry.Get().HistogramCounter(metric_labels, move(collector))
  64. hist.Record(1)
  65. hist.Record(1000, 4)
  66. cdef IHistogramSnapshotPtr snapshot = collector_ptr.Snapshot()
  67. assert deref(snapshot.Get()).Count() == 6
  68. assert snapshot.Get().Value(0) == 1
  69. def test_rate_histogram(self):
  70. cdef THolder[TMetricRegistry] registry
  71. registry.Reset(new TMetricRegistry())
  72. cdef TLabels metric_labels = TLabels()
  73. metric_labels.Add(TString("name"), TString("histogram"))
  74. cdef IHistogramCollectorPtr collector = move(ExponentialHistogram(6, 2, 3))
  75. collector_ptr = collector.Get()
  76. hist = registry.Get().HistogramRate(metric_labels, move(collector))
  77. hist.Record(1)
  78. hist.Record(1000, 4)
  79. cdef IHistogramSnapshotPtr snapshot = collector_ptr.Snapshot()
  80. assert deref(snapshot.Get()).Count() == 6
  81. assert snapshot.Get().Value(0) == 1
  82. assert snapshot.Get().Value(5) == 4
  83. assert snapshot.Get().Value(5) == 4