]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #ifdef ENABLE_METRICS_PREVIEW | |
5 | # include <gtest/gtest.h> | |
6 | # include <thread> | |
7 | ||
8 | # include "opentelemetry/sdk/_metrics/aggregator/gauge_aggregator.h" | |
9 | ||
10 | using namespace opentelemetry::sdk::metrics; | |
11 | namespace metrics_api = opentelemetry::metrics; | |
12 | ||
13 | TEST(GaugeAggregator, Update) | |
14 | { | |
15 | // This tests that the aggregator updates the maintained value correctly | |
16 | // after a call to the update() function. | |
17 | auto agg = new GaugeAggregator<int>(metrics_api::InstrumentKind::Counter); | |
18 | ||
19 | // Verify default value | |
20 | ASSERT_EQ(agg->get_values()[0], 0); | |
21 | ||
22 | // Verify that the value updates correctly | |
23 | agg->update(1); | |
24 | ASSERT_EQ(agg->get_values()[0], 1); | |
25 | ||
26 | // Verify that the value continually updates correctly | |
27 | for (int i = 0; i < 10; ++i) | |
28 | { | |
29 | agg->update(i); | |
30 | } | |
31 | ASSERT_EQ(agg->get_values()[0], 9); | |
32 | delete agg; | |
33 | } | |
34 | ||
35 | TEST(GaugeAggregator, Checkpoint) | |
36 | { | |
37 | // This tests that the aggregator correctly updates the | |
38 | // checkpoint_ value after a call to update() followed | |
39 | // by a call to checkpoint(). | |
40 | GaugeAggregator<int> agg(metrics_api::InstrumentKind::Counter); | |
41 | ||
42 | // Verify default checkpoint, before updates | |
43 | ASSERT_EQ(agg.get_checkpoint()[0], 0); | |
44 | ||
45 | agg.update(10); | |
46 | agg.checkpoint(); | |
47 | ||
48 | // Verify that the new checkpoint contains the update value | |
49 | ASSERT_EQ(agg.get_checkpoint()[0], 10); | |
50 | } | |
51 | ||
52 | TEST(GaugeAggregator, Merge) | |
53 | { | |
54 | // This tests that the values_ vector is updated correctly after | |
55 | // two aggregators are merged together. | |
56 | GaugeAggregator<int> agg1(metrics_api::InstrumentKind::Counter); | |
57 | GaugeAggregator<int> agg2(metrics_api::InstrumentKind::Counter); | |
58 | ||
59 | agg1.update(1); | |
60 | agg2.update(2); | |
61 | ||
62 | agg1.merge(agg2); | |
63 | ||
64 | // Verify that the aggregators merged and the value was updated correctly | |
65 | ASSERT_EQ(agg1.get_values()[0], 2); | |
66 | } | |
67 | ||
68 | TEST(GaugeAggregator, BadMerge) | |
69 | { | |
70 | // This verifies that we encounter and error when we try to merge | |
71 | // two aggregators of different numeric types together. | |
72 | GaugeAggregator<int> agg1(metrics_api::InstrumentKind::Counter); | |
73 | GaugeAggregator<int> agg2(metrics_api::InstrumentKind::ValueRecorder); | |
74 | ||
75 | agg1.update(1); | |
76 | agg2.update(2); | |
77 | agg1.merge(agg2); | |
78 | ||
79 | // Verify that the aggregators did NOT merge | |
80 | std::vector<int> correct{1}; | |
81 | ASSERT_EQ(agg1.get_values(), correct); | |
82 | } | |
83 | ||
84 | TEST(GaugeAggregator, Types) | |
85 | { | |
86 | // This test verifies that we do not encounter any errors when | |
87 | // using various numeric types. | |
88 | GaugeAggregator<int> agg_int(metrics_api::InstrumentKind::Counter); | |
89 | GaugeAggregator<long> agg_long(metrics_api::InstrumentKind::Counter); | |
90 | GaugeAggregator<float> agg_float(metrics_api::InstrumentKind::Counter); | |
91 | GaugeAggregator<double> agg_double(metrics_api::InstrumentKind::Counter); | |
92 | ||
93 | for (int i = 1; i <= 10; ++i) | |
94 | { | |
95 | agg_int.update(i); | |
96 | agg_long.update(i); | |
97 | } | |
98 | ||
99 | for (float i = 1.0; i <= 10.0; i += 1) | |
100 | { | |
101 | agg_float.update(i); | |
102 | agg_double.update(i); | |
103 | } | |
104 | ||
105 | ASSERT_EQ(agg_int.get_values()[0], 10); | |
106 | ASSERT_EQ(agg_long.get_values()[0], 10); | |
107 | ASSERT_EQ(agg_float.get_values()[0], 10.0); | |
108 | ASSERT_EQ(agg_double.get_values()[0], 10.0); | |
109 | } | |
110 | ||
111 | static void callback(GaugeAggregator<int> &agg) | |
112 | { | |
113 | for (int i = 1; i <= 10000; ++i) | |
114 | { | |
115 | agg.update(i); | |
116 | } | |
117 | } | |
118 | ||
119 | TEST(GaugeAggregator, Concurrency) | |
120 | { | |
121 | // This test checks that the aggregator updates appropriately | |
122 | // when called in a multi-threaded context. | |
123 | GaugeAggregator<int> agg(metrics_api::InstrumentKind::Counter); | |
124 | ||
125 | std::thread first(&callback, std::ref(agg)); | |
126 | std::thread second(&callback, std::ref(agg)); | |
127 | ||
128 | first.join(); | |
129 | second.join(); | |
130 | ||
131 | ASSERT_EQ(agg.get_values()[0], 10000); | |
132 | } | |
133 | #endif |