forked from liutongxuan/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.hh
580 lines (510 loc) · 17.5 KB
/
metrics.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2016 ScyllaDB.
*/
#pragma once
#include <functional>
#include "sstring.hh"
#include "core/shared_ptr.hh"
#include "core/metrics_registration.hh"
#include <boost/lexical_cast.hpp>
#include <map>
#include "core/metrics_types.hh"
#include "util/std-compat.hh"
/*! \file metrics.hh
* \brief header for metrics creation.
*
* This header file contains the metrics creation method with their helper function.
* Include this file when need to create metrics.
* Typically this will be in your source file.
*
* Code that is under the impl namespace should not be used directly.
*
*/
namespace seastar {
/*!
* \namespace seastar::metrics
* \brief metrics creation and registration
*
* the metrics namespace holds the relevant method and classes to generate metrics.
*
* The metrics layer support registering metrics, that later will be
* exported via different API protocols.
*
* To be able to support multiple protocols the following simplifications where made:
* 1. The id of the metrics is based on the collectd id
* 2. A metric could be a single value either a reference or a function
*
* To add metrics definition to class A do the following:
* * Add a metrics_group memeber to A
* * Add a a set_metrics() method that would be called in the constructor.
*
*
* In A header file
* \code
* #include "core/metrics_registration.hh"
* class A {
* metric_groups _metrics
*
* void setup_metrics();
*
* };
* \endcode
*
* In A source file:
*
* \code
* include "core/metrics.hh"
*
* void A::setup_metrics() {
* namespace sm = seastar::metrics;
* _metrics = sm::create_metric_group();
* _metrics->add_group("cache", {sm::make_gauge("bytes", "used", [this] { return _region.occupancy().used_space(); })});
* }
* \endcode
*/
namespace metrics {
/*!
* \defgroup metrics_types metrics type definitions
* The following are for the metric layer use, do not use them directly
* Instead use the make_counter, make_gauge, make_absolute and make_derived
*
*/
using metric_type_def = sstring; /*!< Used to hold an inherit type (like bytes)*/
using metric_name_type = sstring; /*!< The metric name'*/
using instance_id_type = sstring; /*!< typically used for the shard id*/
/*!
* \brief Human-readable description of a metric/group.
*
*
* Uses a separate class to deal with type resolution
*
* Add this to metric creation:
*
* \code
* _metrics->add_group("groupname", {
* sm::make_gauge("metric_name", value, description("A documentation about the return value"))
* });
* \endcode
*
*/
class description {
public:
description(sstring s = sstring()) : _s(std::move(s))
{}
const sstring& str() const {
return _s;
}
private:
sstring _s;
};
/*!
* \brief Label a metrics
*
* Label are useful for adding information about a metric that
* later you would need to aggregate by.
* For example, if you have multiple queues on a shard.
* Adding the queue id as a Label will allow you to use the same name
* of the metrics with multiple id instances.
*
* label_instance holds an instance of label consist of a key and value.
*
* Typically you will not generate a label_instance yourself, but use a label
* object for that.
* @see label for more information
*
*
*/
class label_instance {
sstring _key;
sstring _value;
public:
/*!
* \brief create a label_instance
* label instance consists of key and value.
* The key is an sstring.
* T - the value type can be any type that can be lexical_cast to string
* (ie. if it support the redirection operator for stringstream).
*
* All primitive types are supported so all the following examples are valid:
* label_instance a("smp_queue", 1)
* label_instance a("my_key", "my_value")
* label_instance a("internal_id", -1)
*/
template<typename T>
label_instance(const sstring& key, T v) : _key(key), _value(boost::lexical_cast<std::string>(v)){}
/*!
* \brief returns the label key
*/
const sstring key() const {
return _key;
}
/*!
* \brief returns the label value
*/
const sstring value() const {
return _value;
}
bool operator<(const label_instance&) const;
bool operator==(const label_instance&) const;
bool operator!=(const label_instance&) const;
};
/*!
* \brief Class that creates label instances
*
* A factory class to create label instance
* Typically, the same Label name is used in multiple places.
* label is a label factory, you create it once, and use it to create the label_instance.
*
* In the example we would like to label the smp_queue with with the queue owner
*
* seastar::metrics::label smp_owner("smp_owner");
*
* now, when creating a new smp metric we can add a label to it:
*
* sm::make_queue_length("send_batch_queue_length", _last_snt_batch, {smp_owner(cpuid)})
*
* where cpuid in this case is unsiged.
*/
class label {
sstring key;
public:
using instance = label_instance;
/*!
* \brief creating a label
* key is the label name, it will be the key for all label_instance
* that will be created from this label.
*/
explicit label(const sstring& key) : key(key) {
}
/*!
* \brief creating a label instance
*
* Use the function operator to create a new label instance.
* T - the value type can be any type that can be lexical_cast to string
* (ie. if it support the redirection operator for stringstream).
*
* All primitive types are supported so if lab is a label, all the following examples are valid:
* lab(1)
* lab("my_value")
* lab(-1)
*/
template<typename T>
instance operator()(T value) const {
return label_instance(key, std::forward<T>(value));
}
/*!
* \brief returns the label name
*/
const sstring& name() const {
return key;
}
};
/*!
* \namesapce impl
* \brief holds the implementation parts of the metrics layer, do not use directly.
*
* The metrics layer define a thin API for adding metrics.
* Some of the implementation details need to be in the header file, they should not be use directly.
*/
namespace impl {
// The value binding data types
enum class data_type : uint8_t {
COUNTER, // unsigned int 64
GAUGE, // double
DERIVE, // signed int 64
ABSOLUTE, // unsigned int 64
HISTOGRAM,
};
/*!
* \breif A helper class that used to return metrics value.
*
* Do not use directly @see metrics_creation
*/
struct metric_value {
compat::variant<double, histogram> u;
data_type _type;
data_type type() const {
return _type;
}
double d() const {
return compat::get<double>(u);
}
uint64_t ui() const {
return compat::get<double>(u);
}
int64_t i() const {
return compat::get<double>(u);
}
metric_value()
: _type(data_type::GAUGE) {
}
metric_value(histogram&& h, data_type t = data_type::HISTOGRAM) :
u(std::move(h)), _type(t) {
}
metric_value(const histogram& h, data_type t = data_type::HISTOGRAM) :
u(h), _type(t) {
}
metric_value(double d, data_type t)
: u(d), _type(t) {
}
metric_value& operator=(const metric_value& c) = default;
metric_value& operator+=(const metric_value& c) {
*this = *this + c;
return *this;
}
metric_value operator+(const metric_value& c);
const histogram& get_histogram() const {
return compat::get<histogram>(u);
}
};
using metric_function = std::function<metric_value()>;
struct metric_type {
data_type base_type;
metric_type_def type_name;
};
struct metric_definition_impl {
metric_name_type name;
metric_type type;
metric_function f;
description d;
bool enabled = true;
std::map<sstring, sstring> labels;
metric_definition_impl& operator ()(bool enabled);
metric_definition_impl& operator ()(const label_instance& label);
metric_definition_impl(
metric_name_type name,
metric_type type,
metric_function f,
description d,
std::vector<label_instance> labels);
};
class metric_groups_def {
public:
metric_groups_def() = default;
virtual ~metric_groups_def() = default;
metric_groups_def(const metric_groups_def&) = delete;
metric_groups_def(metric_groups_def&&) = default;
virtual metric_groups_def& add_metric(group_name_type name, const metric_definition& md) = 0;
virtual metric_groups_def& add_group(group_name_type name, const std::initializer_list<metric_definition>& l) = 0;
virtual metric_groups_def& add_group(group_name_type name, const std::vector<metric_definition>& l) = 0;
};
instance_id_type shard();
template<typename T, typename En = std::true_type>
struct is_callable;
template<typename T>
struct is_callable<T, typename std::integral_constant<bool, !std::is_void<typename std::result_of<T()>::type>::value>::type> : public std::true_type {
};
template<typename T>
struct is_callable<T, typename std::enable_if<std::is_fundamental<T>::value, std::true_type>::type> : public std::false_type {
};
template<typename T, typename = std::enable_if_t<is_callable<T>::value>>
metric_function make_function(T val, data_type dt) {
return [dt, val] {
return metric_value(val(), dt);
};
}
template<typename T, typename = std::enable_if_t<!is_callable<T>::value>>
metric_function make_function(T& val, data_type dt) {
return [dt, &val] {
return metric_value(val, dt);
};
}
}
extern const bool metric_disabled;
extern label shard_label;
extern label type_label;
/*
* The metrics definition are defined to be compatible with collectd metrics defintion.
* Typically you should used gauge or derived.
*/
/*!
* \brief Gauge are a general purpose metric.
*
* They can support floating point and can increase or decrease
*/
template<typename T>
impl::metric_definition_impl make_gauge(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {}) {
return {name, {impl::data_type::GAUGE, "gauge"}, make_function(std::forward<T>(val), impl::data_type::GAUGE), d, labels};
}
/*!
* \brief Gauge are a general purpose metric.
*
* They can support floating point and can increase or decrease
*/
template<typename T>
impl::metric_definition_impl make_gauge(metric_name_type name,
description d, T&& val) {
return {name, {impl::data_type::GAUGE, "gauge"}, make_function(std::forward<T>(val), impl::data_type::GAUGE), d, {}};
}
/*!
* \brief Gauge are a general purpose metric.
*
* They can support floating point and can increase or decrease
*/
template<typename T>
impl::metric_definition_impl make_gauge(metric_name_type name,
description d, std::vector<label_instance> labels, T&& val) {
return {name, {impl::data_type::GAUGE, "gauge"}, make_function(std::forward<T>(val), impl::data_type::GAUGE), d, labels};
}
/*!
* \brief Derive are used when a rate is more interesting than the value.
*
* Derive is an integer value that can increase or decrease, typically it is used when looking at the
* derivation of the value.
*
* 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.
*/
template<typename T>
impl::metric_definition_impl make_derive(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {}) {
return {name, {impl::data_type::DERIVE, "derive"}, make_function(std::forward<T>(val), impl::data_type::DERIVE), d, labels};
}
/*!
* \brief Derive are used when a rate is more interesting than the value.
*
* Derive is an integer value that can increase or decrease, typically it is used when looking at the
* derivation of the value.
*
* 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.
*/
template<typename T>
impl::metric_definition_impl make_derive(metric_name_type name, description d,
T&& val) {
return {name, {impl::data_type::DERIVE, "derive"}, make_function(std::forward<T>(val), impl::data_type::DERIVE), d, {}};
}
/*!
* \brief Derive are used when a rate is more interesting than the value.
*
* Derive is an integer value that can increase or decrease, typically it is used when looking at the
* derivation of the value.
*
* 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.
*/
template<typename T>
impl::metric_definition_impl make_derive(metric_name_type name, description d, std::vector<label_instance> labels,
T&& val) {
return {name, {impl::data_type::DERIVE, "derive"}, make_function(std::forward<T>(val), impl::data_type::DERIVE), d, labels};
}
/*!
* \brief create a counter metric
*
* 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.
* It is better to use large enough data value than to use counter.
*
*/
template<typename T>
impl::metric_definition_impl make_counter(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {}) {
return {name, {impl::data_type::COUNTER, "counter"}, make_function(std::forward<T>(val), impl::data_type::COUNTER), d, labels};
}
/*!
* \brief create an absolute metric.
*
* Absolute are used for metric that are being erased after each time they are read.
* They are here for compatibility reasons and should general be avoided in most applications.
*/
template<typename T>
impl::metric_definition_impl make_absolute(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {}) {
return {name, {impl::data_type::ABSOLUTE, "absolute"}, make_function(std::forward<T>(val), impl::data_type::ABSOLUTE), d, labels};
}
/*!
* \brief create a histogram metric.
*
* Histograms are a list o buckets with upper values and counter for the number
* of entries in each bucket.
*/
template<typename T>
impl::metric_definition_impl make_histogram(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {}) {
return {name, {impl::data_type::HISTOGRAM, "histogram"}, make_function(std::forward<T>(val), impl::data_type::HISTOGRAM), d, labels};
}
/*!
* \brief create a histogram metric.
*
* Histograms are a list o buckets with upper values and counter for the number
* of entries in each bucket.
*/
template<typename T>
impl::metric_definition_impl make_histogram(metric_name_type name,
description d, std::vector<label_instance> labels, T&& val) {
return {name, {impl::data_type::HISTOGRAM, "histogram"}, make_function(std::forward<T>(val), impl::data_type::HISTOGRAM), d, labels};
}
/*!
* \brief create a histogram metric.
*
* Histograms are a list o buckets with upper values and counter for the number
* of entries in each bucket.
*/
template<typename T>
impl::metric_definition_impl make_histogram(metric_name_type name,
description d, T&& val) {
return {name, {impl::data_type::HISTOGRAM, "histogram"}, make_function(std::forward<T>(val), impl::data_type::HISTOGRAM), d, {}};
}
/*!
* \brief create a total_bytes metric.
*
* total_bytes are used for an ever growing counters, like the total bytes
* passed on a network.
*/
template<typename T>
impl::metric_definition_impl make_total_bytes(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {},
instance_id_type instance = impl::shard()) {
return make_derive(name, std::forward<T>(val), d, labels)(type_label("total_bytes"));
}
/*!
* \brief create a current_bytes metric.
*
* current_bytes are used to report on current status in bytes.
* For example the current free memory.
*/
template<typename T>
impl::metric_definition_impl make_current_bytes(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {},
instance_id_type instance = impl::shard()) {
return make_derive(name, std::forward<T>(val), d, labels)(type_label("bytes"));
}
/*!
* \brief create a queue_length metric.
*
* queue_length are used to report on queue length
*/
template<typename T>
impl::metric_definition_impl make_queue_length(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {},
instance_id_type instance = impl::shard()) {
return make_gauge(name, std::forward<T>(val), d, labels)(type_label("queue_length"));
}
/*!
* \brief create a total operation metric.
*
* total_operations are used for ever growing operation counter.
*/
template<typename T>
impl::metric_definition_impl make_total_operations(metric_name_type name,
T&& val, description d=description(), std::vector<label_instance> labels = {},
instance_id_type instance = impl::shard()) {
return make_derive(name, std::forward<T>(val), d, labels)(type_label("total_operations"));
}
/*! @} */
}
}