2 * Copyright (c) 2017 Uber Technologies, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "jaegertracing/metrics/Counter.h"
18 #include "jaegertracing/metrics/Gauge.h"
19 #include "jaegertracing/metrics/InMemoryStatsReporter.h"
20 #include "jaegertracing/metrics/Metrics.h"
21 #include "jaegertracing/metrics/StatsFactoryImpl.h"
22 #include "jaegertracing/metrics/Timer.h"
24 #include <gtest/gtest.h>
27 #include <unordered_map>
30 namespace jaegertracing
{
33 class MetricsTest
: public ::testing::Test
{
37 , _metrics(Metrics::fromStatsReporter(_metricsReporter
))
42 InMemoryStatsReporter _metricsReporter
;
43 std::unique_ptr
<Metrics
> _metrics
;
46 TEST_F(MetricsTest
, testCounter
)
48 constexpr auto counterValue
= static_cast<int64_t>(3);
49 constexpr auto metricName
= "jaeger.test-counter";
50 StatsFactoryImpl
factory(_metricsReporter
);
51 auto counter
= factory
.createCounter(metricName
);
52 counter
->inc(counterValue
);
53 const auto& counters
= _metricsReporter
.counters();
54 ASSERT_EQ(1, counters
.size());
55 auto itr
= counters
.find(metricName
);
56 ASSERT_TRUE(itr
!= std::end(counters
));
57 ASSERT_EQ(counterValue
, itr
->second
);
60 TEST_F(MetricsTest
, testGauge
)
62 constexpr auto gaugeValue
= static_cast<int64_t>(3);
63 constexpr auto metricName
= "jaeger.test-gauge";
64 StatsFactoryImpl
factory(_metricsReporter
);
65 auto gauge
= factory
.createGauge(metricName
);
66 gauge
->update(gaugeValue
);
67 const auto& gauges
= _metricsReporter
.gauges();
68 ASSERT_EQ(1, gauges
.size());
69 auto itr
= gauges
.find(metricName
);
70 ASSERT_TRUE(itr
!= std::end(gauges
));
71 ASSERT_EQ(gaugeValue
, itr
->second
);
74 TEST_F(MetricsTest
, testTimer
)
76 constexpr auto timeValue
= static_cast<int64_t>(5);
77 constexpr auto metricName
= "jaeger.test-timer";
78 StatsFactoryImpl
factory(_metricsReporter
);
79 auto timer
= factory
.createTimer(metricName
);
80 timer
->record(timeValue
);
81 const auto& timers
= _metricsReporter
.timers();
82 ASSERT_EQ(1, timers
.size());
83 auto itr
= timers
.find(metricName
);
84 ASSERT_TRUE(itr
!= std::end(timers
));
85 ASSERT_EQ(timeValue
, itr
->second
);
88 TEST_F(MetricsTest
, testReset
)
90 _metrics
->tracesStartedSampled().inc(1);
91 const auto& counters
= _metricsReporter
.counters();
92 ASSERT_EQ(1, counters
.size());
94 _metrics
->reporterQueueLength().update(1);
95 const auto& gauges
= _metricsReporter
.gauges();
96 ASSERT_EQ(1, gauges
.size());
98 constexpr auto time
= static_cast<int64_t>(1);
99 constexpr auto timerName
= "jaeger.test-timer";
100 _metricsReporter
.recordTimer(timerName
, time
);
101 const auto& timers
= _metricsReporter
.timers();
102 ASSERT_EQ(1, timers
.size());
104 _metricsReporter
.reset();
105 ASSERT_TRUE(counters
.empty());
106 ASSERT_TRUE(gauges
.empty());
107 ASSERT_TRUE(timers
.empty());
110 } // namespace metrics
111 } // namespace jaegertracing