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 
24 #include <seastar/core/metrics.hh>
25 #include <seastar/util/modules.hh>
26 #include <seastar/core/sharded.hh>
27 #ifndef SEASTAR_MODULE
28 #include <unordered_map>
29 #include <boost/functional/hash.hpp>
30 #endif
31 
32 #include <deque>
33 
41 namespace seastar {
42 namespace metrics {
43 namespace impl {
44 
45 using labels_type = std::map<sstring, sstring>;
46 }
47 }
48 }
49 
50 namespace std {
51 
52 template<>
53 struct hash<seastar::metrics::impl::labels_type> {
54  using argument_type = seastar::metrics::impl::labels_type;
55  using result_type = ::std::size_t;
56  result_type operator()(argument_type const& s) const {
57  result_type h = 0;
58  for (auto&& i : s) {
59  boost::hash_combine(h, std::hash<seastar::sstring>{}(i.second));
60  }
61  return h;
62  }
63 };
64 
65 }
66 
67 namespace seastar {
68 namespace metrics {
69 
70 SEASTAR_MODULE_EXPORT
71 struct relabel_config;
72 
84 SEASTAR_MODULE_EXPORT
86  size_t metrics_relabeled_due_to_collision;
87 };
88 
89 namespace impl {
90 
104 class metric_id {
105 public:
106  metric_id() = default;
108  labels_type labels = {})
109  : _group(std::move(group)), _name(
110  std::move(name)), _labels(labels) {
111  }
112  metric_id(metric_id &&) = default;
113  metric_id(const metric_id &) = default;
114 
115  metric_id & operator=(metric_id &&) = default;
116  metric_id & operator=(const metric_id &) = default;
117 
118  const group_name_type & group_name() const {
119  return _group;
120  }
121  void group_name(const group_name_type & name) {
122  _group = name;
123  }
124  const instance_id_type & instance_id() const {
125  return _labels.at(shard_label.name());
126  }
127  const metric_name_type & name() const {
128  return _name;
129  }
130  const labels_type& labels() const {
131  return _labels;
132  }
133  labels_type& labels() {
134  return _labels;
135  }
136  sstring full_name() const;
137 
138  bool operator<(const metric_id&) const;
139  bool operator==(const metric_id&) const;
140 private:
141  auto as_tuple() const {
142  return std::tie(group_name(), instance_id(), name(), labels());
143  }
144  group_name_type _group;
145  metric_name_type _name;
146  labels_type _labels;
147 };
148 }
149 }
150 }
151 
152 namespace std {
153 
154 template<>
155 struct hash<seastar::metrics::impl::metric_id>
156 {
158  typedef ::std::size_t result_type;
159  result_type operator()(argument_type const& s) const
160  {
161  result_type const h1 ( std::hash<seastar::sstring>{}(s.group_name()) );
162  result_type const h2 ( std::hash<seastar::sstring>{}(s.instance_id()) );
163  return h1 ^ (h2 << 1); // or use boost::hash_combine
164  }
165 };
166 
167 }
168 
169 namespace seastar {
170 namespace metrics {
171 namespace impl {
172 
180  data_type type;
181  metric_type_def inherit_type;
182  description d;
183  sstring name;
184  std::vector<std::string> aggregate_labels;
185 };
186 
187 
191 struct metric_info {
192  metric_id id;
193  labels_type original_labels;
194  bool enabled;
195  skip_when_empty should_skip_when_empty;
196 };
197 
198 
199 using metrics_registration = std::vector<metric_id>;
200 
202  metrics_registration _registration;
203 public:
204  metric_groups_impl() = default;
206  metric_groups_impl(const metric_groups_impl&) = delete;
208  metric_groups_impl& add_metric(group_name_type name, const metric_definition& md);
209  metric_groups_impl& add_group(group_name_type name, const std::initializer_list<metric_definition>& l);
210  metric_groups_impl& add_group(group_name_type name, const std::vector<metric_definition>& l);
211 };
212 
213 class impl;
214 
215 class registered_metric final {
216  metric_info _info;
217  metric_function _f;
218  shared_ptr<impl> _impl;
219 public:
220  registered_metric(metric_id id, metric_function f, bool enabled=true, skip_when_empty skip=skip_when_empty::no);
221  metric_value operator()() const {
222  return _f();
223  }
224 
225  bool is_enabled() const {
226  return _info.enabled;
227  }
228 
229  void set_enabled(bool b) {
230  _info.enabled = b;
231  }
232  void set_skip_when_empty(skip_when_empty skip) noexcept {
233  _info.should_skip_when_empty = skip;
234  }
235  const metric_id& get_id() const {
236  return _info.id;
237  }
238 
239  const metric_info& info() const {
240  return _info;
241  }
242  metric_info& info() {
243  return _info;
244  }
245  const metric_function& get_function() const {
246  return _f;
247  }
248 };
249 
251 using metric_instances = std::map<labels_type, register_ref>;
252 
254  metric_instances _instances;
255  metric_family_info _info;
256 public:
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 
325 using value_map = std::map<sstring, metric_family>;
326 
327 using metric_metadata_fifo = std::deque<metric_info>;
328 
338  metric_metadata_fifo metrics;
339 };
340 
341 using value_vector = std::vector<metric_value>;
342 using metric_metadata = std::vector<metric_family_metadata>;
343 using metric_values = std::deque<value_vector>;
344 
345 struct values_copy {
347  metric_values values;
348 };
349 
350 struct config {
351  sstring hostname;
352 };
353 
354 class impl {
355  value_map _value_map;
356  config _config;
357  bool _dirty = true;
358  shared_ptr<metric_metadata> _metadata;
359  std::set<sstring> _labels;
360  std::vector<std::deque<metric_function>> _current_metrics;
361  std::vector<relabel_config> _relabel_configs;
362 public:
363  value_map& get_value_map() {
364  return _value_map;
365  }
366 
367  const value_map& get_value_map() const {
368  return _value_map;
369  }
370 
371  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);
372  void remove_registration(const metric_id& id);
373  future<> stop() {
374  return make_ready_future<>();
375  }
376  const config& get_config() const {
377  return _config;
378  }
379  void set_config(const config& c) {
380  _config = c;
381  }
382 
383  shared_ptr<metric_metadata> metadata();
384 
385  std::vector<std::deque<metric_function>>& functions();
386 
387  void update_metrics_if_needed();
388 
389  void dirty() {
390  _dirty = true;
391  }
392 
393  const std::set<sstring>& get_labels() const noexcept {
394  return _labels;
395  }
396 
397  future<metric_relabeling_result> set_relabel_configs(const std::vector<relabel_config>& relabel_configs);
398 
399  const std::vector<relabel_config>& get_relabel_configs() const noexcept {
400  return _relabel_configs;
401  }
402 };
403 
404 const value_map& get_value_map();
406 
407 foreign_ptr<values_reference> get_values();
408 
409 shared_ptr<impl> get_local_impl();
410 
411 void unregister_metric(const metric_id & id);
412 
419 std::unique_ptr<metric_groups_def> create_metric_groups();
420 
421 }
422 
429 
430  options(program_options::option_group* parent_group);
431 };
432 
437 
483 future<metric_relabeling_result> set_relabel_configs(const std::vector<relabel_config>& relabel_configs);
484 /*
485  * \brief return the current relabel_configs
486  * This function returns a vector of the current relabel configs
487  */
488 const std::vector<relabel_config>& get_relabel_configs();
489 
490 }
491 }
Definition: sharded.hh:845
Human-readable description of a metric/group.
Definition: metrics.hh:134
Definition: metrics_api.hh:253
Definition: metrics_api.hh:201
Definition: metrics_api.hh:104
A helper class that used to return metrics value.
Definition: metrics.hh:295
Definition: metrics_api.hh:215
const sstring & name() const
returns the label name
Definition: metrics.hh:249
Definition: metrics_registration.hh:67
Definition: program-options.hh:290
sstring metric_type_def
Definition: metrics.hh:114
sstring metric_name_type
Definition: metrics.hh:115
sstring instance_id_type
Definition: metrics.hh:116
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.
future< metric_relabeling_result > set_relabel_configs(const std::vector< relabel_config > &relabel_configs)
Perform relabeling and operation on metrics dynamically.
sstring group_name_type
Definition: metrics_registration.hh:64
future configure(const options &opts)
set the metrics configuration
result of metric relabeling
Definition: metrics_api.hh:85
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: metrics_api.hh:350
holds metadata information of a metric family
Definition: metrics_api.hh:179
holds a metric family metadata
Definition: metrics_api.hh:336
holds metric metadata
Definition: metrics_api.hh:191
Definition: metrics.hh:370
Definition: metrics_api.hh:345
Metrics configuration options.
Definition: metrics_api.hh:424
program_options::value< std::string > metrics_hostname
The hostname used by the metrics.
Definition: metrics_api.hh:428