aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/metric_sub_registry_ut.cpp
blob: 0c5d48b876590d72d30ccb2c93379b174db0fb1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "metric_sub_registry.h"

#include <library/cpp/testing/unittest/registar.h>

using namespace NMonitoring;

Y_UNIT_TEST_SUITE(TMetricSubRegistryTest) {
    Y_UNIT_TEST(WrapRegistry) {
        TMetricRegistry registry;

        {
            TMetricSubRegistry subRegistry{{{"common", "label"}}, &registry};
            IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}}));
            UNIT_ASSERT(g);
            g->Set(42);
        }

        TIntGauge* g = registry.IntGauge({{"my", "gauge"}, {"common", "label"}});
        UNIT_ASSERT(g);
        UNIT_ASSERT_VALUES_EQUAL(g->Get(), 42);
    }

    Y_UNIT_TEST(CommonLabelsDoNotOverrideGeneralLabel) {
        TMetricRegistry registry;

        {
            TMetricSubRegistry subRegistry{{{"common", "label"}, {"my", "notOverride"}}, &registry};
            IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}}));
            UNIT_ASSERT(g);
            g->Set(1234);
        }

        TIntGauge* knownGauge = registry.IntGauge({{"my", "gauge"}, {"common", "label"}});
        UNIT_ASSERT(knownGauge);
        UNIT_ASSERT_VALUES_EQUAL(knownGauge->Get(), 1234);

        TIntGauge* newGauge = registry.IntGauge({{"common", "label"}, {"my", "notOverride"}});
        UNIT_ASSERT(newGauge);
        UNIT_ASSERT_VALUES_EQUAL(newGauge->Get(), 0);
    }

    Y_UNIT_TEST(RemoveMetric) {
        TMetricRegistry registry;

        {
            TMetricSubRegistry subRegistry{{{"common", "label"}}, &registry};
            IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}}));
            UNIT_ASSERT(g);
            g->Set(1234);
        }

        IIntGauge* g1 = registry.IntGauge({{"my", "gauge"}, {"common", "label"}});
        UNIT_ASSERT(g1);
        UNIT_ASSERT_VALUES_EQUAL(g1->Get(), 1234);

        {
            TMetricSubRegistry subRegistry{{{"common", "label"}}, &registry};
            subRegistry.RemoveMetric(TLabels{{"my", "gauge"}});
        }

        IIntGauge* g2 = registry.IntGauge({{"my", "gauge"}, {"common", "label"}});
        UNIT_ASSERT(g2);
        UNIT_ASSERT_VALUES_EQUAL(g2->Get(), 0);
    }
}