Seastar
High performance C++ framework for concurrent servers
metrics_api.hh
Go to the documentation of this file.
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2016 ScyllaDB.
20 */
21
22#pragma once
23
25#include <seastar/util/modules.hh>
26#include <seastar/core/sharded.hh>
27#ifndef SEASTAR_MODULE
28#include <boost/functional/hash.hpp>
29#endif
30
31#include <deque>
32
40namespace seastar {
41namespace metrics {
42namespace impl {
43
44using labels_type = std::map<sstring, sstring>;
45}
46}
47}
48
49namespace std {
50
51template<>
52struct hash<seastar::metrics::impl::labels_type> {
53 using argument_type = seastar::metrics::impl::labels_type;
54 using result_type = ::std::size_t;
55 result_type operator()(argument_type const& s) const {
56 result_type h = 0;
57 for (auto&& i : s) {
58 boost::hash_combine(h, std::hash<seastar::sstring>{}(i.second));
59 }
60 return h;
61 }
62};
63
64}
65
66namespace seastar {
67namespace metrics {
68
69SEASTAR_MODULE_EXPORT
70struct relabel_config;
71
72SEASTAR_MODULE_EXPORT
73struct metric_family_config;
85SEASTAR_MODULE_EXPORT
87 size_t metrics_relabeled_due_to_collision;
88};
89
90namespace impl {
91
106public:
107 metric_id() = default;
109 labels_type labels = {})
110 : _group(std::move(group)), _name(
111 std::move(name)), _labels(labels) {
112 }
113 metric_id(metric_id &&) = default;
114 metric_id(const metric_id &) = default;
115
116 metric_id & operator=(metric_id &&) = default;
117 metric_id & operator=(const metric_id &) = default;
118
119 const group_name_type & group_name() const {
120 return _group;
121 }
122 void group_name(const group_name_type & name) {
123 _group = name;
124 }
125 const instance_id_type & instance_id() const {
126 return _labels.at(shard_label.name());
127 }
128 const metric_name_type & name() const {
129 return _name;
130 }
131 const labels_type& labels() const {
132 return _labels;
133 }
134 labels_type& labels() {
135 return _labels;
136 }
137 sstring full_name() const;
138
139 bool operator<(const metric_id&) const;
140 bool operator==(const metric_id&) const;
141private:
142 auto as_tuple() const {
143 return std::tie(group_name(), instance_id(), name(), labels());
144 }
145 group_name_type _group;
146 metric_name_type _name;
147 labels_type _labels;
148};
149}
150}
151}
152
153namespace std {
154
155template<>
156struct hash<seastar::metrics::impl::metric_id>
157{
159 typedef ::std::size_t result_type;
160 result_type operator()(argument_type const& s) const
161 {
162 result_type const h1 ( std::hash<seastar::sstring>{}(s.group_name()) );
163 result_type const h2 ( std::hash<seastar::sstring>{}(s.instance_id()) );
164 return h1 ^ (h2 << 1); // or use boost::hash_combine
165 }
166};
167
168}
169
170namespace seastar {
171namespace metrics {
172namespace impl {
173
181 data_type type;
182 metric_type_def inherit_type;
183 description d;
184 sstring name;
185 std::vector<std::string> aggregate_labels;
186};
187
188
193 metric_id id;
194 labels_type original_labels;
195 bool enabled;
196 skip_when_empty should_skip_when_empty;
197};
198
199
200using metrics_registration = std::vector<metric_id>;
201
203 metrics_registration _registration;
204public:
205 metric_groups_impl() = default;
207 metric_groups_impl(const metric_groups_impl&) = delete;
209 metric_groups_impl& add_metric(group_name_type name, const metric_definition& md);
210 metric_groups_impl& add_group(group_name_type name, const std::initializer_list<metric_definition>& l);
211 metric_groups_impl& add_group(group_name_type name, const std::vector<metric_definition>& l);
212};
213
214class impl;
215
216class registered_metric final {
217 metric_info _info;
218 metric_function _f;
219 shared_ptr<impl> _impl;
220public:
221 registered_metric(metric_id id, metric_function f, bool enabled=true, skip_when_empty skip=skip_when_empty::no);
222 metric_value operator()() const {
223 return _f();
224 }
225
226 bool is_enabled() const {
227 return _info.enabled;
228 }
229
230 void set_enabled(bool b) {
231 _info.enabled = b;
232 }
233 void set_skip_when_empty(skip_when_empty skip) noexcept {
234 _info.should_skip_when_empty = skip;
235 }
236 const metric_id& get_id() const {
237 return _info.id;
238 }
239
240 const metric_info& info() const {
241 return _info;
242 }
243 metric_info& info() {
244 return _info;
245 }
246 const metric_function& get_function() const {
247 return _f;
248 }
249};
250
252using metric_instances = std::map<labels_type, register_ref>;
253
255 metric_instances _instances;
256 metric_family_info _info;
257public:
258 using iterator = metric_instances::iterator;
259 using const_iterator = metric_instances::const_iterator;
260
261 metric_family() = default;
262 metric_family(const metric_family&) = default;
263 metric_family(const metric_instances& instances) : _instances(instances) {
264 }
265 metric_family(const metric_instances& instances, const metric_family_info& info) : _instances(instances), _info(info) {
266 }
267 metric_family(metric_instances&& instances, metric_family_info&& info) : _instances(std::move(instances)), _info(std::move(info)) {
268 }
269 metric_family(metric_instances&& instances) : _instances(std::move(instances)) {
270 }
271
272 register_ref& operator[](const labels_type& l) {
273 return _instances[l];
274 }
275
276 const register_ref& at(const labels_type& l) const {
277 return _instances.at(l);
278 }
279
280 metric_family_info& info() {
281 return _info;
282 }
283
284 const metric_family_info& info() const {
285 return _info;
286 }
287
288 iterator find(const labels_type& l) {
289 return _instances.find(l);
290 }
291
292 const_iterator find(const labels_type& l) const {
293 return _instances.find(l);
294 }
295
296 iterator begin() {
297 return _instances.begin();
298 }
299
300 const_iterator begin() const {
301 return _instances.cbegin();
302 }
303
304 iterator end() {
305 return _instances.end();
306 }
307
308 bool empty() const {
309 return _instances.empty();
310 }
311
312 iterator erase(const_iterator position) {
313 return _instances.erase(position);
314 }
315
316 const_iterator end() const {
317 return _instances.cend();
318 }
319
320 uint32_t size() const {
321 return _instances.size();
322 }
323
324};
325
326using value_map = std::map<sstring, metric_family>;
327
328using metric_metadata_fifo = std::deque<metric_info>;
329
344 metric_metadata_fifo metrics;
345};
346
347using value_vector = std::deque<metric_value>;
348using metric_metadata = std::vector<metric_family_metadata>;
349using metric_values = std::deque<value_vector>;
350
353 metric_values values;
354};
355
356struct config {
357 sstring hostname;
358};
359
360class impl {
361 value_map _value_map;
362 config _config;
363 bool _dirty = true;
365 std::set<sstring> _labels;
366 std::vector<std::deque<metric_function>> _current_metrics;
367 std::vector<relabel_config> _relabel_configs;
368 std::vector<metric_family_config> _metric_family_configs;
369public:
370 value_map& get_value_map() {
371 return _value_map;
372 }
373
374 const value_map& get_value_map() const {
375 return _value_map;
376 }
377
378 void add_registration(const metric_id& id, const metric_type& type, metric_function f, const description& d, bool enabled, skip_when_empty skip, const std::vector<std::string>& aggregate_labels);
379 void remove_registration(const metric_id& id);
380 future<> stop() {
381 return make_ready_future<>();
382 }
383 const config& get_config() const {
384 return _config;
385 }
386 void set_config(const config& c) {
387 _config = c;
388 }
389
391
392 std::vector<std::deque<metric_function>>& functions();
393
394 void update_metrics_if_needed();
395
396 void dirty() {
397 _dirty = true;
398 }
399
400 const std::set<sstring>& get_labels() const noexcept {
401 return _labels;
402 }
403
404 future<metric_relabeling_result> set_relabel_configs(const std::vector<relabel_config>& relabel_configs);
405
406 const std::vector<relabel_config>& get_relabel_configs() const noexcept {
407 return _relabel_configs;
408 }
409 const std::vector<metric_family_config>& get_metric_family_configs() const noexcept {
410 return _metric_family_configs;
411 }
412
413 void set_metric_family_configs(const std::vector<metric_family_config>& metrics_config);
414
415 void update_aggregate(metric_family_info& mf) const noexcept;
416};
417
418const value_map& get_value_map();
420
422
423shared_ptr<impl> get_local_impl();
424
425void unregister_metric(const metric_id & id);
426
433std::unique_ptr<metric_groups_def> create_metric_groups();
434
435}
436
443
445};
446
451
497future<metric_relabeling_result> set_relabel_configs(const std::vector<relabel_config>& relabel_configs);
498/*
499 * \brief return the current relabel_configs
500 * This function returns a vector of the current relabel configs
501 */
502const std::vector<relabel_config>& get_relabel_configs();
503
504/*
505 * \brief change the metrics family config
506 *
507 * Family config is a configuration that relates to all metrics with the same name but with different labels.
508 * set_metric_family_configs allows changing that configuration during run time.
509 * Specifically, change the label aggregation based on a metric name.
510 *
511 * The following is an example for setting the aggregate labels for the metric test_gauge_1
512 * and all metrics matching the regex test_gauge1.*:
513 *
514 * std::vector<sm::metric_family_config> fc(2);
515 * fc[0].name = "test_gauge_1";
516 * fc[0].aggregate_labels = { "lb" };
517 * fc[1].regex_name = "test_gauge1.*";
518 * fc[1].aggregate_labels = { "ll", "aa" };
519 * sm::set_metric_family_configs(fc);
520 */
521void set_metric_family_configs(const std::vector<metric_family_config>& metrics_config);
522
523/*
524 * \brief return the current metric_family_config
525 * This function returns a vector of the current metrics family config
526 */
527const std::vector<metric_family_config>& get_metric_family_configs();
528}
529}
Definition: sharded.hh:927
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
Human-readable description of a metric/group.
Definition: metrics.hh:133
Definition: metrics_api.hh:254
Definition: metrics_api.hh:202
Definition: metrics_api.hh:105
A helper class that used to return metrics value.
Definition: metrics.hh:294
Definition: metrics_api.hh:216
const sstring & name() const
returns the label name
Definition: metrics.hh:248
Definition: metrics_registration.hh:67
Definition: program-options.hh:292
sstring metric_type_def
Definition: metrics.hh:113
sstring metric_name_type
Definition: metrics.hh:114
sstring instance_id_type
Definition: metrics.hh:115
header for metrics creation.
std::unique_ptr< metric_groups_def > create_metric_groups()
initialize metric group
holds the implementation parts of the metrics layer, do not use directly.
sstring group_name_type
Definition: metrics_registration.hh:64
future configure(const options &opts)
set the metrics configuration
future< metric_relabeling_result > set_relabel_configs(const std::vector< relabel_config > &relabel_configs)
Perform relabeling and operation on metrics dynamically.
result of metric relabeling
Definition: metrics_api.hh:86
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
STL namespace.
Definition: metrics_api.hh:356
holds metadata information of a metric family
Definition: metrics_api.hh:180
holds a metric family metadata
Definition: metrics_api.hh:342
holds metric metadata
Definition: metrics_api.hh:192
Definition: metrics.hh:369
Definition: metrics_api.hh:351
Metrics configuration options.
Definition: metrics_api.hh:438
program_options::value< std::string > metrics_hostname
The hostname used by the metrics.
Definition: metrics_api.hh:442