forked from trpc-group/trpc-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.h
157 lines (123 loc) · 4.58 KB
/
metrics.h
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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#pragma once
#include <any>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "trpc/common/plugin.h"
namespace trpc {
constexpr int kMetricsCallerSource = 0;
constexpr int kMetricsCalleeSource = 1;
/// @brief Metrics data for inter-module calls
struct ModuleMetricsInfo {
/// Reporting source, where kMetricsCallerSource indicates reporting from the caller and kMetricsCalleeSource
/// indicates reporting from the callee
int source;
/// Statistical information
std::map<std::string, std::string> infos;
/// Call's status, where 0 indicates success, 1 indicates exception, and 2 indicates timeout
int state;
/// Call's status code of framework
int frame_ret_code;
/// Call's status code of user interface
int interface_ret_code;
/// Call's duration
uint32_t cost_time;
/// Call's protocol
std::string protocol;
/// Extend information, can be used to provide more information when need
std::any extend_info;
};
/// @brief Bucket for histogram statistics
using HistogramBucket = std::vector<double>;
/// @brief Quantiles for summary statistics
using SummaryQuantiles = std::vector<std::vector<double>>;
/// @brief Statistical strategy
enum MetricsPolicy {
/// Overwrite the statistical value
SET = 0,
/// Calculate the sum value
SUM = 1,
/// Calculate the mean value
AVG = 2,
/// Calculate the maximum value
MAX = 3,
/// Calculate the minimum value
MIN = 4,
/// Calculate the median value
MID = 5,
/// Calculate the quartiles
QUANTILES = 6,
/// Calculate the histogram
HISTOGRAM = 7,
};
/// @brief Metrics data with single-dimensional attribute
/// @note If the attribute label has only one key, you can directly use "name" or "dimension".
/// If the attribute label is a key-value pair, you can use "name" and "dimension" to form the key-value pair.
struct SingleAttrMetricsInfo {
/// Metrics name
std::string name;
/// Metrics dimension
std::string dimension;
/// Metrics policy
MetricsPolicy policy;
/// Statistical value
double value;
/// The bucket used to gather histogram statistics
HistogramBucket bucket;
/// The quantiles used to gather summary statistics
SummaryQuantiles quantiles;
};
/// @brief Metrics data with multi-dimensional attributes
/// @note If the attribute labels are composed of single keys, you can use the "dimensions" field.
/// If the attribute labels are composed of key-value pairs, you can use the "tags" field.
struct MultiAttrMetricsInfo {
/// Metrics name
std::string name;
/// Metrics tags
std::map<std::string, std::string> tags;
/// Metrics dimensions
std::vector<std::string> dimensions;
/// Statistical values
std::vector<std::pair<MetricsPolicy, double>> values;
/// The bucket used to gather histogram statistics
HistogramBucket bucket;
/// The quantiles used to gather summary statistics
SummaryQuantiles quantiles;
};
/// @brief The abstract interface definition for metrics plugins.
class Metrics : public Plugin {
public:
/// @brief Gets the plugin type
/// @return plugin type
PluginType Type() const override { return PluginType::kMetrics; }
/// @brief Reports the metrics data for inter-module calls
/// @param info metrics data
/// @return the reported result, where 0 indicates success and non-zero indicates failure
virtual int ModuleReport(const ModuleMetricsInfo& info) = 0;
virtual int ModuleReport(ModuleMetricsInfo&& info) { return ModuleReport(info); }
/// @brief Reports the metrics data with single-dimensional attribute
/// @param info metrics data
/// @return the reported result, where 0 indicates success and non-zero indicates failure
virtual int SingleAttrReport(const SingleAttrMetricsInfo& info) = 0;
virtual int SingleAttrReport(SingleAttrMetricsInfo&& info) { return SingleAttrReport(info); }
/// @brief Reports the metrics data with multi-dimensional attributes
/// @param info metrics data
/// @return the reported result, where 0 indicates success and non-zero indicates failure
virtual int MultiAttrReport(const MultiAttrMetricsInfo& info) = 0;
virtual int MultiAttrReport(MultiAttrMetricsInfo&& info) { return MultiAttrReport(info); }
};
using MetricsPtr = RefPtr<Metrics>;
} // namespace trpc