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 <unordered_map>
26 #include <seastar/core/sharded.hh>
27 #include <boost/functional/hash.hpp>
35 namespace seastar {
36 namespace metrics {
37 namespace impl {
38 
39 using labels_type = std::map<sstring, sstring>;
40 }
41 }
42 }
43 
44 namespace std {
45 
46 template<>
47 struct hash<seastar::metrics::impl::labels_type> {
48  using argument_type = seastar::metrics::impl::labels_type;
49  using result_type = ::std::size_t;
50  result_type operator()(argument_type const& s) const {
51  result_type h = 0;
52  for (auto&& i : s) {
53  boost::hash_combine(h, std::hash<seastar::sstring>{}(i.second));
54  }
55  return h;
56  }
57 };
58 
59 }
60 
61 namespace seastar {
62 namespace metrics {
63 namespace impl {
64 
78 class metric_id {
79 public:
80  metric_id() = default;
82  labels_type labels = {})
83  : _group(std::move(group)), _name(
84  std::move(name)), _labels(labels) {
85  }
86  metric_id(metric_id &&) = default;
87  metric_id(const metric_id &) = default;
88 
89  metric_id & operator=(metric_id &&) = default;
90  metric_id & operator=(const metric_id &) = default;
91 
92  const group_name_type & group_name() const {
93  return _group;
94  }
95  void group_name(const group_name_type & name) {
96  _group = name;
97  }
98  const instance_id_type & instance_id() const {
99  return _labels.at(shard_label.name());
100  }
101  const metric_name_type & name() const {
102  return _name;
103  }
104  const labels_type& labels() const {
105  return _labels;
106  }
107  sstring full_name() const;
108 
109  bool operator<(const metric_id&) const;
110  bool operator==(const metric_id&) const;
111 private:
112  auto as_tuple() const {
113  return std::tie(group_name(), instance_id(), name(), labels());
114  }
115  group_name_type _group;
116  metric_name_type _name;
117  labels_type _labels;
118 };
119 }
120 }
121 }
122 
123 namespace std {
124 
125 template<>
126 struct hash<seastar::metrics::impl::metric_id>
127 {
129  typedef ::std::size_t result_type;
130  result_type operator()(argument_type const& s) const
131  {
132  result_type const h1 ( std::hash<seastar::sstring>{}(s.group_name()) );
133  result_type const h2 ( std::hash<seastar::sstring>{}(s.instance_id()) );
134  return h1 ^ (h2 << 1); // or use boost::hash_combine
135  }
136 };
137 
138 }
139 
140 namespace seastar {
141 namespace metrics {
142 namespace impl {
143 
151  data_type type;
152  metric_type_def inherit_type;
153  description d;
154  sstring name;
155 };
156 
157 
161 struct metric_info {
162  metric_id id;
163  bool enabled;
164 };
165 
166 
167 using metrics_registration = std::vector<metric_id>;
168 
170  metrics_registration _registration;
171 public:
172  metric_groups_impl() = default;
174  metric_groups_impl(const metric_groups_impl&) = delete;
176  metric_groups_impl& add_metric(group_name_type name, const metric_definition& md);
177  metric_groups_impl& add_group(group_name_type name, const std::initializer_list<metric_definition>& l);
178  metric_groups_impl& add_group(group_name_type name, const std::vector<metric_definition>& l);
179 };
180 
181 class impl;
182 
184  metric_info _info;
185  metric_function _f;
186  shared_ptr<impl> _impl;
187 public:
188  registered_metric(metric_id id, metric_function f, bool enabled=true);
189  virtual ~registered_metric() {}
190  virtual metric_value operator()() const {
191  return _f();
192  }
193 
194  bool is_enabled() const {
195  return _info.enabled;
196  }
197 
198  void set_enabled(bool b) {
199  _info.enabled = b;
200  }
201 
202  const metric_id& get_id() const {
203  return _info.id;
204  }
205 
206  const metric_info& info() const {
207  return _info;
208  }
209  metric_function& get_function() {
210  return _f;
211  }
212 };
213 
215 using metric_instances = std::map<labels_type, register_ref>;
216 
218  metric_instances _instances;
219  metric_family_info _info;
220 public:
221  using iterator = metric_instances::iterator;
222  using const_iterator = metric_instances::const_iterator;
223 
224  metric_family() = default;
225  metric_family(const metric_family&) = default;
226  metric_family(const metric_instances& instances) : _instances(instances) {
227  }
228  metric_family(const metric_instances& instances, const metric_family_info& info) : _instances(instances), _info(info) {
229  }
230  metric_family(metric_instances&& instances, metric_family_info&& info) : _instances(std::move(instances)), _info(std::move(info)) {
231  }
232  metric_family(metric_instances&& instances) : _instances(std::move(instances)) {
233  }
234 
235  register_ref& operator[](const labels_type& l) {
236  return _instances[l];
237  }
238 
239  const register_ref& at(const labels_type& l) const {
240  return _instances.at(l);
241  }
242 
243  metric_family_info& info() {
244  return _info;
245  }
246 
247  const metric_family_info& info() const {
248  return _info;
249  }
250 
251  iterator find(const labels_type& l) {
252  return _instances.find(l);
253  }
254 
255  const_iterator find(const labels_type& l) const {
256  return _instances.find(l);
257  }
258 
259  iterator begin() {
260  return _instances.begin();
261  }
262 
263  const_iterator begin() const {
264  return _instances.cbegin();
265  }
266 
267  iterator end() {
268  return _instances.end();
269  }
270 
271  bool empty() const {
272  return _instances.empty();
273  }
274 
275  iterator erase(const_iterator position) {
276  return _instances.erase(position);
277  }
278 
279  const_iterator end() const {
280  return _instances.cend();
281  }
282 
283  uint32_t size() const {
284  return _instances.size();
285  }
286 
287 };
288 
289 using value_map = std::map<sstring, metric_family>;
290 
291 using metric_metadata_vector = std::vector<metric_info>;
292 
302  metric_metadata_vector metrics;
303 };
304 
305 using value_vector = std::vector<metric_value>;
306 using metric_metadata = std::vector<metric_family_metadata>;
307 using metric_values = std::vector<value_vector>;
308 
309 struct values_copy {
311  metric_values values;
312 };
313 
314 struct config {
315  sstring hostname;
316 };
317 
318 class impl {
319  value_map _value_map;
320  config _config;
321  bool _dirty = true;
322  shared_ptr<metric_metadata> _metadata;
323  std::vector<std::vector<metric_function>> _current_metrics;
324 public:
325  value_map& get_value_map() {
326  return _value_map;
327  }
328 
329  const value_map& get_value_map() const {
330  return _value_map;
331  }
332 
333  void add_registration(const metric_id& id, const metric_type& type, metric_function f, const description& d, bool enabled);
334  void remove_registration(const metric_id& id);
335  future<> stop() {
336  return make_ready_future<>();
337  }
338  const config& get_config() const {
339  return _config;
340  }
341  void set_config(const config& c) {
342  _config = c;
343  }
344 
345  shared_ptr<metric_metadata> metadata();
346 
347  std::vector<std::vector<metric_function>>& functions();
348 
349  void update_metrics_if_needed();
350 
351  void dirty() {
352  _dirty = true;
353  }
354 };
355 
356 const value_map& get_value_map();
358 
359 foreign_ptr<values_reference> get_values();
360 
361 shared_ptr<impl> get_local_impl();
362 
363 void unregister_metric(const metric_id & id);
364 
371 std::unique_ptr<metric_groups_def> create_metric_groups();
372 
373 }
377 future<> configure(const boost::program_options::variables_map & opts);
378 
383 boost::program_options::options_description get_options_description();
384 
385 }
386 }
seastar::shared_ptr< impl >
seastar::metrics::impl::config
Definition: metrics_api.hh:314
seastar
Seastar API namespace.
Definition: abort_on_ebadf.hh:24
seastar::metrics::impl::values_copy
Definition: metrics_api.hh:309
seastar::metrics::impl::registered_metric
Definition: metrics_api.hh:183
seastar::metrics::impl::metric_family
Definition: metrics_api.hh:217
seastar::metrics::description
Human-readable description of a metric/group.
Definition: metrics.hh:123
seastar::metrics::get_options_description
boost::program_options::options_description get_options_description()
get the metrics configuration desciprtion
seastar::metrics::impl::metric_groups_impl
Definition: metrics_api.hh:169
seastar::metrics::impl::metric_id
Definition: metrics_api.hh:78
seastar::metrics::label::name
const sstring & name() const
returns the label name
Definition: metrics.hh:238
seastar::metrics::instance_id_type
sstring instance_id_type
Definition: metrics.hh:106
seastar::metrics::impl::metric_family_metadata
holds a metric family metadata
Definition: metrics_api.hh:300
metrics.hh
header for metrics creation.
seastar::metrics::group_name_type
sstring group_name_type
Definition: metrics_registration.hh:59
seastar::metrics::metric_definition
Definition: metrics_registration.hh:62
seastar::metrics::configure
future configure(const boost::program_options::variables_map &opts)
set the metrics configuration
seastar::metrics::impl::metric_type
Definition: metrics.hh:315
seastar::future
A representation of a possibly not-yet-computed value.
Definition: future.hh:1337
seastar::foreign_ptr
Definition: sharded.hh:798
seastar::metrics::metric_type_def
sstring metric_type_def
Definition: metrics.hh:104
impl
holds the implementation parts of the metrics layer, do not use directly.
seastar::metrics::impl::metric_value
A helper class that used to return metrics value.
Definition: metrics.hh:266
seastar::metrics::impl::metric_family_info
holds metadata information of a metric family
Definition: metrics_api.hh:150
seastar::metrics::impl::metric_groups_def
Definition: metrics.hh:338
seastar::metrics::metric_name_type
sstring metric_name_type
Definition: metrics.hh:105
seastar::metrics::impl::create_metric_groups
std::unique_ptr< metric_groups_def > create_metric_groups()
initialize metric group
seastar::metrics::impl::metric_info
holds metric metadata
Definition: metrics_api.hh:161