Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
200class impl;
201
202class registered_metric final {
203 metric_info _info;
204 metric_function _f;
205public:
206 registered_metric(metric_id id, metric_function f, bool enabled=true, skip_when_empty skip=skip_when_empty::no);
207 metric_value operator()() const {
208 return _f();
209 }
210
211 bool is_enabled() const {
212 return _info.enabled;
213 }
214
215 void set_enabled(bool b) {
216 _info.enabled = b;
217 }
218 void set_skip_when_empty(skip_when_empty skip) noexcept {
219 _info.should_skip_when_empty = skip;
220 }
221 const metric_id& get_id() const {
222 return _info.id;
223 }
224
225 const metric_info& info() const {
226 return _info;
227 }
228 metric_info& info() {
229 return _info;
230 }
231 const metric_function& get_function() const {
232 return _f;
233 }
234};
235
237using metric_instances = std::map<labels_type, register_ref>;
238using metrics_registration = std::vector<register_ref>;
239
241 metrics_registration _registration;
242 shared_ptr<impl> _impl; // keep impl alive while metrics are registered
243public:
246 metric_groups_impl(const metric_groups_impl&) = delete;
248 metric_groups_impl& add_metric(group_name_type name, const metric_definition& md);
249 metric_groups_impl& add_group(group_name_type name, const std::initializer_list<metric_definition>& l);
250 metric_groups_impl& add_group(group_name_type name, const std::vector<metric_definition>& l);
251};
252
254 metric_instances _instances;
255 metric_family_info _info;
256public:
257 using iterator = metric_instances::iterator;
258 using const_iterator = metric_instances::const_iterator;
259
260 metric_family() = default;
261 metric_family(const metric_family&) = default;
262 metric_family(const metric_instances& instances) : _instances(instances) {
263 }
264 metric_family(const metric_instances& instances, const metric_family_info& info) : _instances(instances), _info(info) {
265 }
266 metric_family(metric_instances&& instances, metric_family_info&& info) : _instances(std::move(instances)), _info(std::move(info)) {
267 }
268 metric_family(metric_instances&& instances) : _instances(std::move(instances)) {
269 }
270
271 register_ref& operator[](const labels_type& l) {
272 return _instances[l];
273 }
274
275 const register_ref& at(const labels_type& l) const {
276 return _instances.at(l);
277 }
278
279 metric_family_info& info() {
280 return _info;
281 }
282
283 const metric_family_info& info() const {
284 return _info;
285 }
286
287 iterator find(const labels_type& l) {
288 return _instances.find(l);
289 }
290
291 const_iterator find(const labels_type& l) const {
292 return _instances.find(l);
293 }
294
295 iterator begin() {
296 return _instances.begin();
297 }
298
299 const_iterator begin() const {
300 return _instances.cbegin();
301 }
302
303 iterator end() {
304 return _instances.end();
305 }
306
307 bool empty() const {
308 return _instances.empty();
309 }
310
311 iterator erase(const_iterator position) {
312 return _instances.erase(position);
313 }
314
315 const_iterator end() const {
316 return _instances.cend();
317 }
318
319 uint32_t size() const {
320 return _instances.size();
321 }
322
323};
324
325using value_map = std::map<sstring, metric_family>;
326
327using metric_metadata_fifo = std::deque<metric_info>;
328
343 metric_metadata_fifo metrics;
344};
345
346using value_vector = std::deque<metric_value>;
347using metric_metadata = std::vector<metric_family_metadata>;
348using metric_values = std::deque<value_vector>;
349
352 metric_values values;
353};
354
355struct config {
356 sstring hostname;
357};
358
359class impl {
360 value_map _value_map;
361 config _config;
362 bool _dirty = true;
364 std::set<sstring> _labels;
365 std::vector<std::deque<metric_function>> _current_metrics;
366 std::vector<relabel_config> _relabel_configs;
367 std::vector<metric_family_config> _metric_family_configs;
368public:
369 value_map& get_value_map() {
370 return _value_map;
371 }
372
373 const value_map& get_value_map() const {
374 return _value_map;
375 }
376
377 register_ref 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);
378 void remove_registration(const metric_id& id);
379 future<> stop() {
380 return make_ready_future<>();
381 }
382 const config& get_config() const {
383 return _config;
384 }
385 void set_config(const config& c) {
386 _config = c;
387 }
388
390
391 std::vector<std::deque<metric_function>>& functions();
392
393 void update_metrics_if_needed();
394
395 void dirty() {
396 _dirty = true;
397 }
398
399 const std::set<sstring>& get_labels() const noexcept {
400 return _labels;
401 }
402
403 future<metric_relabeling_result> set_relabel_configs(const std::vector<relabel_config>& relabel_configs);
404
405 const std::vector<relabel_config>& get_relabel_configs() const noexcept {
406 return _relabel_configs;
407 }
408 const std::vector<metric_family_config>& get_metric_family_configs() const noexcept {
409 return _metric_family_configs;
410 }
411
412 void set_metric_family_configs(const std::vector<metric_family_config>& metrics_config);
413
414 void update_aggregate(metric_family_info& mf) const noexcept;
415};
416
417const value_map& get_value_map();
419
421
422shared_ptr<impl> get_local_impl();
423
424void unregister_metric(const metric_id & id);
425
432std::unique_ptr<metric_groups_def> create_metric_groups();
433
434}
435
442
444};
445
450
496future<metric_relabeling_result> set_relabel_configs(const std::vector<relabel_config>& relabel_configs);
497/*
498 * \brief return the current relabel_configs
499 * This function returns a vector of the current relabel configs
500 */
501const std::vector<relabel_config>& get_relabel_configs();
502
503/*
504 * \brief change the metrics family config
505 *
506 * Family config is a configuration that relates to all metrics with the same name but with different labels.
507 * set_metric_family_configs allows changing that configuration during run time.
508 * Specifically, change the label aggregation based on a metric name.
509 *
510 * The following is an example for setting the aggregate labels for the metric test_gauge_1
511 * and all metrics matching the regex test_gauge1.*:
512 *
513 * std::vector<sm::metric_family_config> fc(2);
514 * fc[0].name = "test_gauge_1";
515 * fc[0].aggregate_labels = { "lb" };
516 * fc[1].regex_name = "test_gauge1.*";
517 * fc[1].aggregate_labels = { "ll", "aa" };
518 * sm::set_metric_family_configs(fc);
519 */
520void set_metric_family_configs(const std::vector<metric_family_config>& metrics_config);
521
522/*
523 * \brief return the current metric_family_config
524 * This function returns a vector of the current metrics family config
525 */
526const std::vector<metric_family_config>& get_metric_family_configs();
527}
528}
Definition: sharded.hh:945
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:253
Definition: metrics_api.hh:240
Definition: metrics_api.hh:105
A helper class that used to return metrics value.
Definition: metrics.hh:294
Definition: metrics_api.hh:202
const sstring & name() const
returns the label name
Definition: metrics.hh:248
Definition: metrics_registration.hh:67
Definition: program-options.hh:293
Definition: shared_ptr.hh:507
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:355
holds metadata information of a metric family
Definition: metrics_api.hh:180
holds a metric family metadata
Definition: metrics_api.hh:341
holds metric metadata
Definition: metrics_api.hh:192
Definition: metrics.hh:369
Definition: metrics_api.hh:350
Metrics configuration options.
Definition: metrics_api.hh:437
program_options::value< std::string > metrics_hostname
The hostname used by the metrics.
Definition: metrics_api.hh:441