]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
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 | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
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. | |
15 | */ | |
16 | ||
17 | #include "jaegertracing/metrics/InMemoryStatsReporter.h" | |
18 | ||
19 | #include "jaegertracing/metrics/Counter.h" | |
20 | #include "jaegertracing/metrics/Gauge.h" | |
21 | #include "jaegertracing/metrics/Metric.h" | |
22 | #include "jaegertracing/metrics/Metrics.h" | |
23 | #include "jaegertracing/metrics/Timer.h" | |
24 | ||
25 | namespace jaegertracing { | |
26 | namespace metrics { | |
27 | namespace { | |
28 | ||
29 | template <typename Function> | |
30 | void updateMap(InMemoryStatsReporter::ValueMap& map, | |
31 | const std::string& name, | |
32 | int64_t newValue, | |
33 | const std::unordered_map<std::string, std::string>& tags, | |
34 | Function f) | |
35 | { | |
36 | const auto metricName = Metrics::addTagsToMetricName(name, tags); | |
37 | auto& initialValue = map[metricName]; | |
38 | initialValue = f(initialValue, newValue); | |
39 | } | |
40 | ||
41 | } // anonymous namespace | |
42 | ||
43 | void InMemoryStatsReporter::incCounter( | |
44 | const std::string& name, | |
45 | int64_t delta, | |
46 | const std::unordered_map<std::string, std::string>& tags) | |
47 | { | |
48 | updateMap(_counters, | |
49 | name, | |
50 | delta, | |
51 | tags, | |
52 | [](int64_t initialValue, int64_t newValue) { | |
53 | return initialValue + newValue; | |
54 | }); | |
55 | } | |
56 | ||
57 | void InMemoryStatsReporter::recordTimer( | |
58 | const std::string& name, | |
59 | int64_t time, | |
60 | const std::unordered_map<std::string, std::string>& tags) | |
61 | { | |
62 | updateMap( | |
63 | _timers, name, time, tags, [](int64_t initialValue, int64_t newValue) { | |
64 | return initialValue + newValue; | |
65 | }); | |
66 | } | |
67 | ||
68 | void InMemoryStatsReporter::updateGauge( | |
69 | const std::string& name, | |
70 | int64_t amount, | |
71 | const std::unordered_map<std::string, std::string>& tags) | |
72 | { | |
73 | updateMap(_gauges, name, amount, tags, [](int64_t, int64_t newValue) { | |
74 | return newValue; | |
75 | }); | |
76 | } | |
77 | ||
78 | void InMemoryStatsReporter::reset() | |
79 | { | |
80 | _counters.clear(); | |
81 | _gauges.clear(); | |
82 | _timers.clear(); | |
83 | } | |
84 | ||
85 | } // namespace metrics | |
86 | } // namespace jaegertracing |