-
Notifications
You must be signed in to change notification settings - Fork 314
/
registry.cc
154 lines (122 loc) · 4.47 KB
/
registry.cc
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "prometheus/registry.h"
#include <algorithm>
#include <iterator>
#include <stdexcept>
#include <tuple>
#include "prometheus/counter.h"
#include "prometheus/detail/future_std.h"
#include "prometheus/gauge.h"
#include "prometheus/histogram.h"
#include "prometheus/summary.h"
namespace prometheus {
namespace {
template <typename T>
void CollectAll(std::vector<MetricFamily>& results, const T& families) {
for (auto&& collectable : families) {
auto metrics = collectable->Collect();
results.insert(results.end(), std::make_move_iterator(metrics.begin()),
std::make_move_iterator(metrics.end()));
}
}
bool FamilyNameExists(const std::string& /* name */) { return false; }
template <typename T, typename... Args>
bool FamilyNameExists(const std::string& name, const T& families,
Args&&... args) {
auto sameName = [&name](const typename T::value_type& entry) {
return name == entry->GetName();
};
auto exists = std::find_if(std::begin(families), std::end(families),
sameName) != std::end(families);
return exists || FamilyNameExists(name, args...);
}
} // namespace
Registry::Registry(InsertBehavior insert_behavior)
: insert_behavior_{insert_behavior} {}
Registry::~Registry() = default;
std::vector<MetricFamily> Registry::Collect() const {
std::lock_guard<std::mutex> lock{mutex_};
auto results = std::vector<MetricFamily>{};
CollectAll(results, counters_);
CollectAll(results, gauges_);
CollectAll(results, histograms_);
CollectAll(results, summaries_);
return results;
}
template <>
std::vector<std::unique_ptr<Family<Counter>>>& Registry::GetFamilies() {
return counters_;
}
template <>
std::vector<std::unique_ptr<Family<Gauge>>>& Registry::GetFamilies() {
return gauges_;
}
template <>
std::vector<std::unique_ptr<Family<Histogram>>>& Registry::GetFamilies() {
return histograms_;
}
template <>
std::vector<std::unique_ptr<Family<Summary>>>& Registry::GetFamilies() {
return summaries_;
}
template <>
bool Registry::NameExistsInOtherType<Counter>(const std::string& name) const {
return FamilyNameExists(name, gauges_, histograms_, summaries_);
}
template <>
bool Registry::NameExistsInOtherType<Gauge>(const std::string& name) const {
return FamilyNameExists(name, counters_, histograms_, summaries_);
}
template <>
bool Registry::NameExistsInOtherType<Histogram>(const std::string& name) const {
return FamilyNameExists(name, counters_, gauges_, summaries_);
}
template <>
bool Registry::NameExistsInOtherType<Summary>(const std::string& name) const {
return FamilyNameExists(name, counters_, gauges_, histograms_);
}
template <typename T>
Family<T>& Registry::Add(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels) {
std::lock_guard<std::mutex> lock{mutex_};
if (NameExistsInOtherType<T>(name)) {
throw std::invalid_argument(
"Family name already exists with different type");
}
auto& families = GetFamilies<T>();
if (insert_behavior_ == InsertBehavior::Merge) {
auto same_name_and_labels =
[&name, &labels](const std::unique_ptr<Family<T>>& family) {
return std::tie(name, labels) ==
std::tie(family->GetName(), family->GetConstantLabels());
};
auto it =
std::find_if(families.begin(), families.end(), same_name_and_labels);
if (it != families.end()) {
return **it;
}
}
auto same_name = [&name](const std::unique_ptr<Family<T>>& family) {
return name == family->GetName();
};
auto it = std::find_if(families.begin(), families.end(), same_name);
if (it != families.end()) {
throw std::invalid_argument("Family name already exists");
}
auto family = detail::make_unique<Family<T>>(name, help, labels);
auto& ref = *family;
families.push_back(std::move(family));
return ref;
}
template Family<Counter>& Registry::Add(
const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
template Family<Gauge>& Registry::Add(
const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
template Family<Summary>& Registry::Add(
const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
template Family<Histogram>& Registry::Add(
const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
} // namespace prometheus