forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.cc
229 lines (183 loc) · 6.34 KB
/
metrics.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2016 ScyllaDB.
*/
#include "metrics.hh"
#include "metrics_api.hh"
#include <boost/range/algorithm.hpp>
namespace seastar {
namespace metrics {
metric_groups::metric_groups() noexcept : _impl(impl::create_metric_groups()) {
}
void metric_groups::clear() {
_impl = impl::create_metric_groups();
}
metric_groups::metric_groups(std::initializer_list<metric_group_definition> mg) : _impl(impl::create_metric_groups()) {
for (auto&& i : mg) {
add_group(i.name, i.metrics);
}
}
metric_groups& metric_groups::add_group(const group_name_type& name, const std::initializer_list<metric_definition>& l) {
_impl->add_group(name, l);
return *this;
}
metric_group::metric_group() noexcept = default;
metric_group::~metric_group() = default;
metric_group::metric_group(const group_name_type& name, std::initializer_list<metric_definition> l) {
add_group(name, l);
}
metric_group_definition::metric_group_definition(const group_name_type& name, std::initializer_list<metric_definition> l) : name(name), metrics(l) {
}
metric_group_definition::~metric_group_definition() = default;
metric_groups::~metric_groups() = default;
metric_definition::metric_definition(metric_definition&& m) noexcept : _impl(std::move(m._impl)) {
}
metric_definition::~metric_definition() = default;
metric_definition::metric_definition(impl::metric_definition_impl const& m) noexcept :
_impl(std::make_unique<impl::metric_definition_impl>(m)) {
}
bool label_instance::operator<(const label_instance& id2) const {
auto& id1 = *this;
return std::tie(id1.key(), id1.value())
< std::tie(id2.key(), id2.value());
}
bool label_instance::operator==(const label_instance& id2) const {
auto& id1 = *this;
return std::tie(id1.key(), id1.value())
== std::tie(id2.key(), id2.value());
}
bool label_instance::operator!=(const label_instance& id2) const {
auto& id1 = *this;
return !(id1 == id2);
}
label shard_label("shard");
namespace impl {
registered_metric::registered_metric(data_type type, metric_function f, description d, bool enabled) :
_type(type), _d(d), _enabled(enabled), _f(f), _impl(get_local_impl()) {
}
metric_value metric_value::operator+(const metric_value& c) {
metric_value res(*this);
switch (_type) {
case data_type::GAUGE:
res.u._d += c.u._d;
break;
case data_type::DERIVE:
res.u._i += c.u._i;
break;
default:
res.u._ui += c.u._ui;
break;
}
return res;
}
metric_definition_impl::metric_definition_impl(
metric_name_type name,
instance_id_type id,
metric_type type,
metric_function f,
description d,
bool enabled,
std::vector<label_instance> _labels)
: name(name), id(id), type(type), f(f)
, d(d), enabled(enabled) {
for (auto i: _labels) {
labels[i.key()] = i.value();
}
if (labels.find(shard_label.name()) == labels.end()) {
labels[shard_label.name()] = shard();
}
}
std::unique_ptr<metric_groups_def> create_metric_groups() {
return std::make_unique<metric_groups_impl>();
}
std::unique_ptr<metric_id> get_id(group_name_type group, instance_id_type instance, metric_name_type name,
metric_type_def iht) {
return std::make_unique<metric_id>(group, instance, name, iht);
}
metric_groups_impl::~metric_groups_impl() {
for (auto i : _registration) {
unregister_metric(i);
}
}
metric_groups_impl& metric_groups_impl::add_metric(group_name_type name, const metric_definition& md) {
metric_id id(name, md._impl->id, md._impl->name, md._impl->type.type_name, md._impl->labels);
shared_ptr<registered_metric> rm =
::make_shared<registered_metric>(md._impl->type.base_type, md._impl->f, md._impl->d, md._impl->enabled);
get_local_impl()->add_registration(id, rm);
_registration.push_back(id);
return *this;
}
metric_groups_impl& metric_groups_impl::add_group(group_name_type name, const std::vector<metric_definition>& l) {
for (auto i = l.begin(); i != l.end(); ++i) {
add_metric(name, *(i->_impl.get()));
}
return *this;
}
metric_groups_impl& metric_groups_impl::add_group(group_name_type name, const std::initializer_list<metric_definition>& l) {
for (auto i = l.begin(); i != l.end(); ++i) {
add_metric(name, *i);
}
return *this;
}
bool metric_id::operator<(
const metric_id& id2) const {
return as_tuple() < id2.as_tuple();
}
bool metric_id::operator==(
const metric_id & id2) const {
return as_tuple() == id2.as_tuple();
}
// Unfortunately, metrics_impl can not be shared because it
// need to be available before the first users (reactor) will call it
shared_ptr<impl> get_local_impl() {
static thread_local auto the_impl = make_shared<impl>();
return the_impl;
}
void unregister_metric(const metric_id & id) {
shared_ptr<impl> map = get_local_impl();
auto i = map->get_value_map().find(id);
if (i != map->get_value_map().end()) {
i->second = nullptr;
}
}
const value_map& get_value_map() {
return get_local_impl()->get_value_map();
}
values_copy get_values() {
values_copy res;
for (auto i : get_local_impl()->get_value_map()) {
if (i.second.get() && i.second->is_enabled()) {
res[i.first] = (*(i.second))();
}
}
return std::move(res);
}
instance_id_type shard() {
if (engine_is_ready()) {
return to_sstring(engine().cpu_id());
}
return sstring("0");
}
void impl::add_registration(const metric_id& id, shared_ptr<registered_metric> rm) {
_value_map[id] = rm;
}
}
const bool metric_disabled = false;
}
}