-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathprometheus_metrics.h
163 lines (137 loc) · 6.81 KB
/
prometheus_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
158
159
160
161
162
163
//
//
// 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.
//
//
#ifdef TRPC_BUILD_INCLUDE_PROMETHEUS
#pragma once
#include <map>
#include <memory>
#include <string>
#include "prometheus/counter.h"
#include "prometheus/family.h"
#include "prometheus/gauge.h"
#include "prometheus/histogram.h"
#include "prometheus/summary.h"
#include "trpc/metrics/metrics.h"
#include "trpc/metrics/prometheus/prometheus_common.h"
#include "trpc/metrics/prometheus/prometheus_conf.h"
#include "trpc/util/log/logging.h"
#include "trpc/util/prometheus.h"
namespace trpc {
class PrometheusMetrics : public Metrics {
public:
std::string Name() const override { return trpc::prometheus::kPrometheusMetricsName; }
int Init() noexcept override;
void Start() noexcept override;
void Stop() noexcept override;
int ModuleReport(const ModuleMetricsInfo& info) override;
int SingleAttrReport(const SingleAttrMetricsInfo& info) override;
int SingleAttrReport(SingleAttrMetricsInfo&& info) override;
int MultiAttrReport(const MultiAttrMetricsInfo& info) override;
int MultiAttrReport(MultiAttrMetricsInfo&& info) override;
/// @brief Reports metrics data with SET type
/// @note This interface is for internal use only and should not be used by users. May be modified in the future.
int SetDataReport(const std::map<std::string, std::string>& labels, double value);
/// @brief Reports metrics data with SUM type
/// @note This interface is for internal use only and should not be used by users. May be modified in the future.
int SumDataReport(const std::map<std::string, std::string>& labels, double value);
/// @brief Reports metrics data with MID type
/// @note This interface is for internal use only and should not be used by users. May be modified in the future.
int MidDataReport(const std::map<std::string, std::string>& labels, double value);
/// @brief Reports metrics data with QUANTILES type
/// @note This interface is for internal use only and should not be used by users. May be modified in the future.
int QuantilesDataReport(const std::map<std::string, std::string>& labels, const SummaryQuantiles& quantiles,
double value);
/// @brief Reports metrics data with HISTOGRAM type
/// @note This interface is for internal use only and should not be used by users. May be modified in the future.
int HistogramDataReport(const std::map<std::string, std::string>& labels, HistogramBucket&& bucket, double value);
int HistogramDataReport(const std::map<std::string, std::string>& labels, const HistogramBucket& bucket,
double value);
private:
template <typename T>
int SingleAttrReportTemplate(T&& info) {
std::map<std::string, std::string> labels = {{std::forward<T>(info).name, std::forward<T>(info).dimension}};
switch (info.policy) {
case SET:
return SetDataReport(labels, info.value);
case SUM:
return SumDataReport(labels, info.value);
case MID:
return MidDataReport(labels, info.value);
case QUANTILES:
return QuantilesDataReport(labels, info.quantiles, info.value);
case HISTOGRAM:
return HistogramDataReport(labels, std::forward<T>(info).bucket, info.value);
default:
TRPC_LOG_ERROR("unknown policy type: " << static_cast<int>(info.policy));
}
return -1;
}
template <typename T>
int MultiAttrReportTemplate(T&& info) {
if (info.values.empty()) {
TRPC_LOG_ERROR("values can not be empty");
return -1;
}
MetricsPolicy policy = info.values[0].first;
double value = info.values[0].second;
switch (policy) {
case SET:
return SetDataReport(info.tags, value);
case SUM:
return SumDataReport(info.tags, value);
case MID:
return MidDataReport(info.tags, value);
case QUANTILES:
return QuantilesDataReport(info.tags, info.quantiles, value);
case HISTOGRAM:
return HistogramDataReport(info.tags, std::forward<T>(info).bucket, value);
default:
TRPC_LOG_ERROR("unknown policy type: " << static_cast<int>(policy));
}
return -1;
}
private:
PrometheusConfig prometheus_conf_;
uint64_t push_gateway_task_id_ = 0;
// metrics family for number of client-side RPC calls
::prometheus::Family<::prometheus::Counter>* rpc_client_counter_family_;
static constexpr char kRpcClientCounterName[] = "rpc_client_counter_metric";
static constexpr char kRpcClientCounterDesc[] = "Total number of RPCs started on the client.";
// metrics family for distribution of client-side execution time
::prometheus::Family<::prometheus::Histogram>* rpc_client_histogram_family_;
static constexpr char kRpcClientHistogramName[] = "rpc_client_histogram_metric";
static constexpr char kRpcClientHistogramDesc[] = "Histogram of RPC response latency observed by client.";
// metrics family for number of times the server is called
::prometheus::Family<::prometheus::Counter>* rpc_server_counter_family_;
static constexpr char kRpcServerCounterName[] = "rpc_server_counter_metric";
static constexpr char kRpcServerCounterDesc[] = "Total number of RPCs received by server.";
// metrics family for distribution of server-side execution time
::prometheus::Family<::prometheus::Histogram>* rpc_server_histogram_family_;
static constexpr char kRpcServerHistogramName[] = "rpc_server_histogram_metric";
static constexpr char kRpcServerHistogramDesc[] = "Histogram of RPC response latency observed by server.";
// custom metrics data
::prometheus::Family<::prometheus::Counter>* prometheus_counter_family_;
static constexpr char kPrometheusCounterName[] = "prometheus_counter_metric";
static constexpr char kPrometheusCounterDesc[] = "trpc-cpp prometheus counter metric.";
::prometheus::Family<::prometheus::Gauge>* prometheus_gauge_family_;
static constexpr char kPrometheusGaugeName[] = "prometheus_gauge_metric";
static constexpr char kPrometheusGaugeDesc[] = "trpc-cpp prometheus gauge metric.";
::prometheus::Family<::prometheus::Summary>* prometheus_summary_family_;
static constexpr char kPrometheusSummaryName[] = "prometheus_summary_metric";
static constexpr char kPrometheusSummaryDesc[] = "trpc-cpp prometheus summary metric.";
::prometheus::Family<::prometheus::Histogram>* prometheus_histogram_family_;
static constexpr char kPrometheusHistogramName[] = "prometheus_histogram_metric";
static constexpr char kPrometheusHistogramDesc[] = "trpc-cpp prometheus histogram metric.";
};
using PrometheusMetricsPtr = RefPtr<PrometheusMetrics>;
} // namespace trpc
#endif