-
Notifications
You must be signed in to change notification settings - Fork 314
/
family.cc
129 lines (110 loc) · 3.65 KB
/
family.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
#include "prometheus/family.h"
#include <algorithm>
#include <cassert>
#include <map>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include "prometheus/check_names.h"
#include "prometheus/counter.h"
#include "prometheus/gauge.h"
#include "prometheus/histogram.h"
#include "prometheus/summary.h"
namespace prometheus {
template <typename T>
Family<T>::Family(const std::string& name, const std::string& help,
const Labels& constant_labels)
: name_(name), help_(help), constant_labels_(constant_labels) {
if (!CheckMetricName(name_)) {
throw std::invalid_argument("Invalid metric name");
}
for (auto& label_pair : constant_labels_) {
auto& label_name = label_pair.first;
if (!CheckLabelName(label_name)) {
throw std::invalid_argument("Invalid label name");
}
}
}
template <typename T>
T& Family<T>::Add(const Labels& labels, std::unique_ptr<T> object) {
std::lock_guard<std::mutex> lock{mutex_};
auto insert_result =
metrics_.insert(std::make_pair(labels, std::move(object)));
if (insert_result.second) {
// insertion took place, retroactively check for unlikely issues
for (auto& label_pair : labels) {
const auto& label_name = label_pair.first;
if (!CheckLabelName(label_name)) {
metrics_.erase(insert_result.first);
throw std::invalid_argument("Invalid label name");
}
if (constant_labels_.count(label_name)) {
metrics_.erase(insert_result.first);
throw std::invalid_argument("Duplicate label name");
}
}
}
auto& stored_object = insert_result.first->second;
assert(stored_object);
return *stored_object;
}
template <typename T>
void Family<T>::Remove(T* metric) {
std::lock_guard<std::mutex> lock{mutex_};
for (auto it = metrics_.begin(); it != metrics_.end(); ++it) {
if (it->second.get() == metric) {
metrics_.erase(it);
break;
}
}
}
template <typename T>
bool Family<T>::Has(const Labels& labels) const {
std::lock_guard<std::mutex> lock{mutex_};
return metrics_.count(labels) != 0u;
}
template <typename T>
const std::string& Family<T>::GetName() const {
return name_;
}
template <typename T>
const Labels Family<T>::GetConstantLabels() const {
return constant_labels_;
}
template <typename T>
std::vector<MetricFamily> Family<T>::Collect() const {
std::lock_guard<std::mutex> lock{mutex_};
if (metrics_.empty()) {
return {};
}
auto family = MetricFamily{};
family.name = name_;
family.help = help_;
family.type = T::metric_type;
family.metric.reserve(metrics_.size());
for (const auto& m : metrics_) {
family.metric.push_back(std::move(CollectMetric(m.first, m.second.get())));
}
return {family};
}
template <typename T>
ClientMetric Family<T>::CollectMetric(const Labels& metric_labels,
T* metric) const {
auto collected = metric->Collect();
collected.label.reserve(constant_labels_.size() + metric_labels.size());
const auto add_label =
[&collected](const std::pair<std::string, std::string>& label_pair) {
auto label = ClientMetric::Label{};
label.name = label_pair.first;
label.value = label_pair.second;
collected.label.push_back(std::move(label));
};
std::for_each(constant_labels_.cbegin(), constant_labels_.cend(), add_label);
std::for_each(metric_labels.cbegin(), metric_labels.cend(), add_label);
return collected;
}
template class PROMETHEUS_CPP_CORE_EXPORT Family<Counter>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Gauge>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Histogram>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Summary>;
} // namespace prometheus