metric_sub_registry_ut.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "metric_sub_registry.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NMonitoring;
  4. Y_UNIT_TEST_SUITE(TMetricSubRegistryTest) {
  5. Y_UNIT_TEST(WrapRegistry) {
  6. TMetricRegistry registry;
  7. {
  8. TMetricSubRegistry subRegistry{{{"common", "label"}}, &registry};
  9. IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}}));
  10. UNIT_ASSERT(g);
  11. g->Set(42);
  12. }
  13. TIntGauge* g = registry.IntGauge({{"my", "gauge"}, {"common", "label"}});
  14. UNIT_ASSERT(g);
  15. UNIT_ASSERT_VALUES_EQUAL(g->Get(), 42);
  16. }
  17. Y_UNIT_TEST(CommonLabelsDoNotOverrideGeneralLabel) {
  18. TMetricRegistry registry;
  19. {
  20. TMetricSubRegistry subRegistry{{{"common", "label"}, {"my", "notOverride"}}, &registry};
  21. IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}}));
  22. UNIT_ASSERT(g);
  23. g->Set(1234);
  24. }
  25. TIntGauge* knownGauge = registry.IntGauge({{"my", "gauge"}, {"common", "label"}});
  26. UNIT_ASSERT(knownGauge);
  27. UNIT_ASSERT_VALUES_EQUAL(knownGauge->Get(), 1234);
  28. TIntGauge* newGauge = registry.IntGauge({{"common", "label"}, {"my", "notOverride"}});
  29. UNIT_ASSERT(newGauge);
  30. UNIT_ASSERT_VALUES_EQUAL(newGauge->Get(), 0);
  31. }
  32. Y_UNIT_TEST(RemoveMetric) {
  33. TMetricRegistry registry;
  34. {
  35. TMetricSubRegistry subRegistry{{{"common", "label"}}, &registry};
  36. IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}}));
  37. UNIT_ASSERT(g);
  38. g->Set(1234);
  39. }
  40. IIntGauge* g1 = registry.IntGauge({{"my", "gauge"}, {"common", "label"}});
  41. UNIT_ASSERT(g1);
  42. UNIT_ASSERT_VALUES_EQUAL(g1->Get(), 1234);
  43. {
  44. TMetricSubRegistry subRegistry{{{"common", "label"}}, &registry};
  45. subRegistry.RemoveMetric(TLabels{{"my", "gauge"}});
  46. }
  47. IIntGauge* g2 = registry.IntGauge({{"my", "gauge"}, {"common", "label"}});
  48. UNIT_ASSERT(g2);
  49. UNIT_ASSERT_VALUES_EQUAL(g2->Get(), 0);
  50. }
  51. }