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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright (C) 2016 ScyllaDB.
25 #include <seastar/core/sstring.hh>
26 #include <seastar/core/shared_ptr.hh>
27 #include <seastar/core/metrics_registration.hh>
28 #include <boost/lexical_cast.hpp>
30 #include <seastar/core/metrics_types.hh>
31 #include <seastar/util/std-compat.hh>
34 * \brief header for metrics creation.
36 * This header file contains the metrics creation method with their helper function.
37 * Include this file when need to create metrics.
38 * Typically this will be in your source file.
40 * Code that is under the impl namespace should not be used directly.
50 * \namespace seastar::metrics
51 * \brief metrics creation and registration
53 * the metrics namespace holds the relevant method and classes to generate metrics.
55 * The metrics layer support registering metrics, that later will be
56 * exported via different API protocols.
58 * To be able to support multiple protocols the following simplifications where made:
59 * 1. The id of the metrics is based on the collectd id
60 * 2. A metric could be a single value either a reference or a function
62 * To add metrics definition to class A do the following:
63 * * Add a metrics_group memeber to A
64 * * Add a a set_metrics() method that would be called in the constructor.
69 * #include "core/metrics_registration.hh"
71 * metric_groups _metrics
73 * void setup_metrics();
81 * include "core/metrics.hh"
83 * void A::setup_metrics() {
84 * namespace sm = seastar::metrics;
85 * _metrics = sm::create_metric_group();
86 * _metrics->add_group("cache", {sm::make_gauge("bytes", "used", [this] { return _region.occupancy().used_space(); })});
93 class double_registration : public std::runtime_error {
95 double_registration(std::string what);
99 * \defgroup metrics_types metrics type definitions
100 * The following are for the metric layer use, do not use them directly
101 * Instead use the make_counter, make_gauge, make_absolute and make_derived
104 using metric_type_def = sstring; /*!< Used to hold an inherit type (like bytes)*/
105 using metric_name_type = sstring; /*!< The metric name'*/
106 using instance_id_type = sstring; /*!< typically used for the shard id*/
109 * \brief Human-readable description of a metric/group.
112 * Uses a separate class to deal with type resolution
114 * Add this to metric creation:
117 * _metrics->add_group("groupname", {
118 * sm::make_gauge("metric_name", value, description("A documentation about the return value"))
125 description(sstring s = sstring()) : _s(std::move(s))
127 const sstring& str() const {
135 * \brief Label a metrics
137 * Label are useful for adding information about a metric that
138 * later you would need to aggregate by.
139 * For example, if you have multiple queues on a shard.
140 * Adding the queue id as a Label will allow you to use the same name
141 * of the metrics with multiple id instances.
143 * label_instance holds an instance of label consist of a key and value.
145 * Typically you will not generate a label_instance yourself, but use a label
147 * @see label for more information
151 class label_instance {
156 * \brief create a label_instance
157 * label instance consists of key and value.
158 * The key is an sstring.
159 * T - the value type can be any type that can be lexical_cast to string
160 * (ie. if it support the redirection operator for stringstream).
162 * All primitive types are supported so all the following examples are valid:
163 * label_instance a("smp_queue", 1)
164 * label_instance a("my_key", "my_value")
165 * label_instance a("internal_id", -1)
168 label_instance(const sstring& key, T v) : _key(key), _value(boost::lexical_cast<std::string>(v)){}
171 * \brief returns the label key
173 const sstring key() const {
178 * \brief returns the label value
180 const sstring value() const {
183 bool operator<(const label_instance&) const;
184 bool operator==(const label_instance&) const;
185 bool operator!=(const label_instance&) const;
190 * \brief Class that creates label instances
192 * A factory class to create label instance
193 * Typically, the same Label name is used in multiple places.
194 * label is a label factory, you create it once, and use it to create the label_instance.
196 * In the example we would like to label the smp_queue with with the queue owner
198 * seastar::metrics::label smp_owner("smp_owner");
200 * now, when creating a new smp metric we can add a label to it:
202 * sm::make_queue_length("send_batch_queue_length", _last_snt_batch, {smp_owner(cpuid)})
204 * where cpuid in this case is unsiged.
209 using instance = label_instance;
211 * \brief creating a label
212 * key is the label name, it will be the key for all label_instance
213 * that will be created from this label.
215 explicit label(const sstring& key) : key(key) {
219 * \brief creating a label instance
221 * Use the function operator to create a new label instance.
222 * T - the value type can be any type that can be lexical_cast to string
223 * (ie. if it support the redirection operator for stringstream).
225 * All primitive types are supported so if lab is a label, all the following examples are valid:
231 instance operator()(T value) const {
232 return label_instance(key, std::forward<T>(value));
236 * \brief returns the label name
238 const sstring& name() const {
245 * \brief holds the implementation parts of the metrics layer, do not use directly.
247 * The metrics layer define a thin API for adding metrics.
248 * Some of the implementation details need to be in the header file, they should not be use directly.
252 // The value binding data types
253 enum class data_type : uint8_t {
254 COUNTER, // unsigned int 64
256 DERIVE, // signed int 64
257 ABSOLUTE, // unsigned int 64
262 * \brief A helper class that used to return metrics value.
264 * Do not use directly @see metrics_creation
266 struct metric_value {
267 std::variant<double, histogram> u;
269 data_type type() const {
274 return std::get<double>(u);
277 uint64_t ui() const {
278 return std::get<double>(u);
282 return std::get<double>(u);
286 : _type(data_type::GAUGE) {
289 metric_value(histogram&& h, data_type t = data_type::HISTOGRAM) :
290 u(std::move(h)), _type(t) {
292 metric_value(const histogram& h, data_type t = data_type::HISTOGRAM) :
296 metric_value(double d, data_type t)
300 metric_value& operator=(const metric_value& c) = default;
302 metric_value& operator+=(const metric_value& c) {
307 metric_value operator+(const metric_value& c);
308 const histogram& get_histogram() const {
309 return std::get<histogram>(u);
313 using metric_function = std::function<metric_value()>;
317 metric_type_def type_name;
320 struct metric_definition_impl {
321 metric_name_type name;
326 std::map<sstring, sstring> labels;
327 metric_definition_impl& operator ()(bool enabled);
328 metric_definition_impl& operator ()(const label_instance& label);
329 metric_definition_impl& set_type(const sstring& type_name);
330 metric_definition_impl(
331 metric_name_type name,
335 std::vector<label_instance> labels);
338 class metric_groups_def {
340 metric_groups_def() = default;
341 virtual ~metric_groups_def() = default;
342 metric_groups_def(const metric_groups_def&) = delete;
343 metric_groups_def(metric_groups_def&&) = default;
344 virtual metric_groups_def& add_metric(group_name_type name, const metric_definition& md) = 0;
345 virtual metric_groups_def& add_group(group_name_type name, const std::initializer_list<metric_definition>& l) = 0;
346 virtual metric_groups_def& add_group(group_name_type name, const std::vector<metric_definition>& l) = 0;
349 instance_id_type shard();
351 template<typename T, typename En = std::true_type>
355 struct is_callable<T, typename std::integral_constant<bool, !std::is_void<typename std::result_of<T()>::type>::value>::type> : public std::true_type {
359 struct is_callable<T, typename std::enable_if<std::is_fundamental<T>::value, std::true_type>::type> : public std::false_type {
362 template<typename T, typename = std::enable_if_t<is_callable<T>::value>>
363 metric_function make_function(T val, data_type dt) {
365 return metric_value(val(), dt);
369 template<typename T, typename = std::enable_if_t<!is_callable<T>::value>>
370 metric_function make_function(T& val, data_type dt) {
372 return metric_value(val, dt);
377 extern const bool metric_disabled;
379 extern label shard_label;
382 * The metrics definition are defined to be compatible with collectd metrics defintion.
383 * Typically you should used gauge or derived.
388 * \brief Gauge are a general purpose metric.
390 * They can support floating point and can increase or decrease
393 impl::metric_definition_impl make_gauge(metric_name_type name,
394 T&& val, description d=description(), std::vector<label_instance> labels = {}) {
395 return {name, {impl::data_type::GAUGE, "gauge"}, make_function(std::forward<T>(val), impl::data_type::GAUGE), d, labels};
399 * \brief Gauge are a general purpose metric.
401 * They can support floating point and can increase or decrease
404 impl::metric_definition_impl make_gauge(metric_name_type name,
405 description d, T&& val) {
406 return {name, {impl::data_type::GAUGE, "gauge"}, make_function(std::forward<T>(val), impl::data_type::GAUGE), d, {}};
410 * \brief Gauge are a general purpose metric.
412 * They can support floating point and can increase or decrease
415 impl::metric_definition_impl make_gauge(metric_name_type name,
416 description d, std::vector<label_instance> labels, T&& val) {
417 return {name, {impl::data_type::GAUGE, "gauge"}, make_function(std::forward<T>(val), impl::data_type::GAUGE), d, labels};
422 * \brief Derive are used when a rate is more interesting than the value.
424 * Derive is an integer value that can increase or decrease, typically it is used when looking at the
425 * derivation of the value.
427 * It is OK to use it when counting things and if no wrap-around is expected (it shouldn't) it's prefer over counter metric.
430 impl::metric_definition_impl make_derive(metric_name_type name,
431 T&& val, description d=description(), std::vector<label_instance> labels = {}) {
432 return {name, {impl::data_type::DERIVE, "derive"}, make_function(std::forward<T>(val), impl::data_type::DERIVE), d, labels};
437 * \brief Derive are used when a rate is more interesting than the value.
439 * Derive is an integer value that can increase or decrease, typically it is used when looking at the
440 * derivation of the value.
442 * It is OK to use it when counting things and if no wrap-around is expected (it shouldn't) it's prefer over counter metric.
445 impl::metric_definition_impl make_derive(metric_name_type name, description d,
447 return {name, {impl::data_type::DERIVE, "derive"}, make_function(std::forward<T>(val), impl::data_type::DERIVE), d, {}};
452 * \brief Derive are used when a rate is more interesting than the value.
454 * Derive is an integer value that can increase or decrease, typically it is used when looking at the
455 * derivation of the value.
457 * It is OK to use it when counting things and if no wrap-around is expected (it shouldn't) it's prefer over counter metric.
460 impl::metric_definition_impl make_derive(metric_name_type name, description d, std::vector<label_instance> labels,
462 return {name, {impl::data_type::DERIVE, "derive"}, make_function(std::forward<T>(val), impl::data_type::DERIVE), d, labels};
467 * \brief create a counter metric
469 * Counters are similar to derived, but they assume monotony, so if a counter value decrease in a series it is count as a wrap-around.
470 * It is better to use large enough data value than to use counter.
474 impl::metric_definition_impl make_counter(metric_name_type name,
475 T&& val, description d=description(), std::vector<label_instance> labels = {}) {
476 return {name, {impl::data_type::COUNTER, "counter"}, make_function(std::forward<T>(val), impl::data_type::COUNTER), d, labels};
480 * \brief create an absolute metric.
482 * Absolute are used for metric that are being erased after each time they are read.
483 * They are here for compatibility reasons and should general be avoided in most applications.
486 impl::metric_definition_impl make_absolute(metric_name_type name,
487 T&& val, description d=description(), std::vector<label_instance> labels = {}) {
488 return {name, {impl::data_type::ABSOLUTE, "absolute"}, make_function(std::forward<T>(val), impl::data_type::ABSOLUTE), d, labels};
492 * \brief create a histogram metric.
494 * Histograms are a list o buckets with upper values and counter for the number
495 * of entries in each bucket.
498 impl::metric_definition_impl make_histogram(metric_name_type name,
499 T&& val, description d=description(), std::vector<label_instance> labels = {}) {
500 return {name, {impl::data_type::HISTOGRAM, "histogram"}, make_function(std::forward<T>(val), impl::data_type::HISTOGRAM), d, labels};
504 * \brief create a histogram metric.
506 * Histograms are a list o buckets with upper values and counter for the number
507 * of entries in each bucket.
510 impl::metric_definition_impl make_histogram(metric_name_type name,
511 description d, std::vector<label_instance> labels, T&& val) {
512 return {name, {impl::data_type::HISTOGRAM, "histogram"}, make_function(std::forward<T>(val), impl::data_type::HISTOGRAM), d, labels};
517 * \brief create a histogram metric.
519 * Histograms are a list o buckets with upper values and counter for the number
520 * of entries in each bucket.
523 impl::metric_definition_impl make_histogram(metric_name_type name,
524 description d, T&& val) {
525 return {name, {impl::data_type::HISTOGRAM, "histogram"}, make_function(std::forward<T>(val), impl::data_type::HISTOGRAM), d, {}};
530 * \brief create a total_bytes metric.
532 * total_bytes are used for an ever growing counters, like the total bytes
533 * passed on a network.
537 impl::metric_definition_impl make_total_bytes(metric_name_type name,
538 T&& val, description d=description(), std::vector<label_instance> labels = {},
539 instance_id_type instance = impl::shard()) {
540 return make_derive(name, std::forward<T>(val), d, labels).set_type("total_bytes");
544 * \brief create a current_bytes metric.
546 * current_bytes are used to report on current status in bytes.
547 * For example the current free memory.
551 impl::metric_definition_impl make_current_bytes(metric_name_type name,
552 T&& val, description d=description(), std::vector<label_instance> labels = {},
553 instance_id_type instance = impl::shard()) {
554 return make_derive(name, std::forward<T>(val), d, labels).set_type("bytes");
559 * \brief create a queue_length metric.
561 * queue_length are used to report on queue length
565 impl::metric_definition_impl make_queue_length(metric_name_type name,
566 T&& val, description d=description(), std::vector<label_instance> labels = {},
567 instance_id_type instance = impl::shard()) {
568 return make_gauge(name, std::forward<T>(val), d, labels).set_type("queue_length");
573 * \brief create a total operation metric.
575 * total_operations are used for ever growing operation counter.
579 impl::metric_definition_impl make_total_operations(metric_name_type name,
580 T&& val, description d=description(), std::vector<label_instance> labels = {},
581 instance_id_type instance = impl::shard()) {
582 return make_derive(name, std::forward<T>(val), d, labels).set_type("total_operations");