metric_ut.pyx 3.8 KB

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