-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【腾讯犀牛鸟开源课题实战】prometheus插件专项建设(PUSH模式支持等) #175
base: main
Are you sure you want to change the base?
Changes from 9 commits
809dfe0
ccfc856
8a1de0e
c879973
e1916f8
b980723
7f42fbd
cd24612
816bc28
3d907d6
2309769
1a5bb30
8cb45d4
a931cfc
3a9fe81
35ef136
f87a954
973bf4b
18dc6d7
919b8b3
c986e4f
12afca9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,13 @@ cc_library( | |
"@trpc_cpp//trpc/metrics/prometheus:prometheus_metrics_api", | ||
], | ||
) | ||
|
||
cc_binary( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不需要push这个文件,去掉与之相关的编译引入 |
||
name = "push", | ||
srcs = ["push.cc"], | ||
deps = [ | ||
"@trpc_cpp//trpc/metrics/prometheus:prometheus_metrics_api", | ||
"@trpc_cpp//trpc/log:trpc_log", | ||
|
||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,12 @@ ::trpc::Status ForwardServiceImpl::Route(::trpc::ServerContextPtr context, | |
"counter_name", "counter_desc", {{"const_counter_key", "const_counter_value"}}); | ||
::prometheus::Counter& counter = counter_family->Add({{"counter_key", "counter_value"}}); | ||
counter.Increment(random_num); | ||
|
||
if (::trpc::prometheus::PushMetricsInfo()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为啥这里还需要手动调用呢?不能配置一下yaml文件就生效吗? |
||
TRPC_FMT_INFO("Successfully pushed metrics to Pushgateway"); | ||
} else { | ||
TRPC_FMT_ERROR("Failed to push metrics to Pushgateway"); | ||
} | ||
#endif | ||
|
||
auto client_context = ::trpc::MakeClientContext(context, greeter_proxy_); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <chrono> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个文件和框架无关,没必要增加,用法放在文档就好了 |
||
#include <thread> | ||
#include "trpc/metrics/prometheus/prometheus_metrics_api.h" | ||
#include "trpc/log/trpc_log.h" | ||
|
||
|
||
|
||
int main(int argc, char** argv) { | ||
|
||
while (true) { | ||
if (::trpc::prometheus::PushMetricsInfo()) | ||
{ | ||
std::cout << "Successfully pushed metrics to Pushgateway" << std::endl; | ||
} else { | ||
std::cerr << "Failed to push metrics to Pushgateway" << std::endl; | ||
} | ||
|
||
std::this_thread::sleep_for(std::chrono::seconds(5)); // 每60秒推送一次 | ||
} | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,11 @@ plugins: | |
const_labels: | ||
const_key1: const_value1 | ||
const_key2: const_value2 | ||
push_mode: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 例子需要演示pull模式和push模式,应该给出2个文件配置 |
||
enabled: true | ||
gateway_url: "http://pushgateway:9091" | ||
job_name: "test_job" | ||
push_interval_seconds: 2 | ||
log: | ||
default: | ||
- name: default | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<div id="layer1"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 去掉不需要的文件 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已删除。 |
||
<p class="nonplot-variable">gcc_version: <span id="value-gcc_version">11.4.0</span></p> | ||
<br> | ||
<p class="nonplot-variable">kernel: <span id="value-kernel">Linux 1dab1b7b0173 5.15.0-113-generic #123~20.04.1-Ubuntu SMP Wed Jun 12 17:33:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux | ||
</span></p> | ||
<br> | ||
<p class="nonplot-variable">core_nums: <span id="value-core_nums">80</span></p> | ||
<br> | ||
<p class="nonplot-variable">user_name: <span id="value-user_name">unknown</span></p> | ||
<br> | ||
<p class="nonplot-variable">work_directory: <span id="value-work_directory">/home/TRPC/trpc-cpp</span></p> | ||
<br> | ||
<p class="nonplot-variable">command_line: <span id="value-command_line">./bazel-bin/examples/features/prometheus/proxy/forward_server | ||
--config=examples/features/prometheus/proxy/trpc_cpp_fiber.yaml | ||
</span></p> | ||
<br> | ||
<p class="nonplot-variable">running_time: <span id="value-running_time">15.100000(hours)</span></p> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_loadavg_1m: <span id="value-proc_loadavg_1m">18.080000</span></u></font></p> | ||
<div class="detail"><div id="proc_loadavg_1m" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_loadavg_5m: <span id="value-proc_loadavg_5m">33.330000</span></u></font></p> | ||
<div class="detail"><div id="proc_loadavg_5m" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_loadavg_15m: <span id="value-proc_loadavg_15m">33.260000</span></u></font></p> | ||
<div class="detail"><div id="proc_loadavg_15m" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_real_time: <span id="value-proc_real_time">1076(secs)</span></u></font></p> | ||
<div class="detail"><div id="proc_real_time" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_sys_time: <span id="value-proc_sys_time">700(secs)</span></u></font></p> | ||
<div class="detail"><div id="proc_sys_time" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_user_time: <span id="value-proc_user_time">376(secs)</span></u></font></p> | ||
<div class="detail"><div id="proc_user_time" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="nonplot-variable">pgrp: <span id="value-pgrp">1271218</span></p> | ||
<br> | ||
<p class="nonplot-variable">ppid: <span id="value-ppid">1262277</span></p> | ||
<br> | ||
<p class="nonplot-variable">pid: <span id="value-pid">1271218</span></p> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_faults_major: <span id="value-proc_faults_major">0</span></u></font></p> | ||
<div class="detail"><div id="proc_faults_major" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_faults_minor_second: <span id="value-proc_faults_minor_second">567953</span></u></font></p> | ||
<div class="detail"><div id="proc_faults_minor_second" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="nonplot-variable">fd_count: <span id="value-fd_count">21</span></p> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_io_read_bytes_second: <span id="value-proc_io_read_bytes_second">8192</span></u></font></p> | ||
<div class="detail"><div id="proc_io_read_bytes_second" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_io_read_second: <span id="value-proc_io_read_second">257383</span></u></font></p> | ||
<div class="detail"><div id="proc_io_read_second" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_io_write_bytes_second: <span id="value-proc_io_write_bytes_second">44539904</span></u></font></p> | ||
<div class="detail"><div id="proc_io_write_bytes_second" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_io_write_second: <span id="value-proc_io_write_second">10908</span></u></font></p> | ||
<div class="detail"><div id="proc_io_write_second" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_mem_drs: <span id="value-proc_mem_drs">171592</span></u></font></p> | ||
<div class="detail"><div id="proc_mem_drs" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_mem_resident: <span id="value-proc_mem_resident">33043</span></u></font></p> | ||
<div class="detail"><div id="proc_mem_resident" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_mem_share: <span id="value-proc_mem_share">3177</span></u></font></p> | ||
<div class="detail"><div id="proc_mem_share" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_mem_trs: <span id="value-proc_mem_trs">3385</span></u></font></p> | ||
<div class="detail"><div id="proc_mem_trs" class="flot-placeholder"></div></div> | ||
<br> | ||
<p class="variable"><font color='#0000FF'><u>proc_mem_size: <span id="value-proc_mem_size">486951</span></u></font></p> | ||
<div class="detail"><div id="proc_mem_size" class="flot-placeholder"></div></div> | ||
<br> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,66 @@ | |
|
||
namespace trpc::admin { | ||
|
||
PrometheusHandler::PrometheusHandler() { description_ = "[GET /metrics] get prometheus metrics"; } | ||
PrometheusHandler::PrometheusHandler() { | ||
description_ = "[GET /metrics] get prometheus metrics"; | ||
bool ret = TrpcConfig::GetInstance()->GetPluginConfig<PrometheusConfig>( | ||
"metrics", trpc::prometheus::kPrometheusMetricsName, prometheus_conf_); | ||
if (!ret) { | ||
TRPC_LOG_WARN( | ||
"Failed to obtain Prometheus plugin configuration from the framework configuration file. Default configuration " | ||
"will be used."); | ||
} | ||
auto& cfg = prometheus_conf_.auth_cfg; | ||
if (cfg.count("username") && cfg.count("password")) { | ||
username_ = cfg["username"]; | ||
password_ = cfg["password"]; | ||
has_cfg = true; | ||
} else { | ||
TRPC_LOG_INFO("can not found prometheus auth config"); | ||
has_cfg = false; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 构造函数做了太复杂的事情,可以定义一个Init函数,把这部分逻辑放在Init函数里 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改。 |
||
} | ||
|
||
void PrometheusHandler::CommandHandle(http::HttpRequestPtr req, rapidjson::Value& result, | ||
rapidjson::Document::AllocatorType& alloc) { | ||
static std::unique_ptr<::prometheus::Serializer> serializer = std::make_unique<::prometheus::TextSerializer>(); | ||
|
||
if (has_cfg) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. has_cfg变量名不好,看起来只跟鉴权判断相关,同时可以考虑把鉴权相关信息统一放在一个结构体里。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改。 |
||
std::string token = req->GetHeader("Authorization"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这块逻辑有啥用?看起来只是判断用户名和密码是否匹配,判断之后有啥用? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 只有用户名密码都正确的情况下,才会返回metric数据,否则拒绝请求。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 用户名和密码,在prometheus的gateway服务哪里能配置呢?文档有给出吗? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 想起来了,这是pull模式的,那用户名和密码在prometheus服务器里哪里能配置呢? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 文档我还没有写,确认一下,Prometheus鉴权相关的使用方法是直接添加在prometheus_metrics.md吗? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以的 |
||
auto splited = Split(token, ' '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 鉴权部分单独提出一个类私有成员接口 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改。 |
||
if (splited.size() != 2) { | ||
result.AddMember("message", "wrong request without authorization", alloc); | ||
TRPC_LOG_INFO("error token: " << token); | ||
return; | ||
} | ||
if (splited[0] != "Basic") { | ||
result.AddMember("message", "wrong request without right auth", alloc); | ||
TRPC_LOG_INFO("error token: " << token); | ||
return; | ||
} | ||
|
||
std::string username_pwd = http::Base64Decode(std::begin(splited[1]), std::end(splited[1])); | ||
auto sp = Split(username_pwd, ':'); | ||
if (sp.size() != 2) { | ||
result.AddMember("message", "wrong request without authorization", alloc); | ||
TRPC_LOG_INFO("error token: " << token); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使用错误日志宏 TRPC_FMT_ERROR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改。 |
||
return; | ||
weimch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
auto username = sp[0]; | ||
if (username != username_) { | ||
result.AddMember("message", "wrong request without right username", alloc); | ||
TRPC_LOG_INFO("error username: " << username << ",right username: " << username_); | ||
return; | ||
} | ||
auto pwd = sp[1]; | ||
if (pwd != password_) { | ||
result.AddMember("message", "wrong request without right password", alloc); | ||
TRPC_LOG_INFO("error password: " << pwd << ",right password: " << password_); | ||
return; | ||
} | ||
} | ||
|
||
|
||
std::string prometheus_str = serializer->Serialize(trpc::prometheus::Collect()); | ||
result.AddMember(rapidjson::StringRef("trpc-html"), rapidjson::Value(prometheus_str, alloc).Move(), alloc); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,13 @@ | |
|
||
#include "trpc/admin/admin_handler.h" | ||
#include "trpc/util/prometheus.h" | ||
#include "trpc/util/http/base64.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 用clang-format格式化一下,头文件顺序需要按照字母序顺序排列 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改。 |
||
#include "trpc/util/string/string_helper.h" | ||
#include "trpc/util/time.h" | ||
#include "trpc/log/trpc_log.h" | ||
#include "trpc/metrics/prometheus/prometheus_metrics.h" | ||
#include "trpc/common/config/trpc_config.h" | ||
#include "trpc/metrics/prometheus/prometheus_conf_parser.h" | ||
|
||
namespace trpc::admin { | ||
|
||
|
@@ -26,6 +33,12 @@ class PrometheusHandler : public AdminHandlerBase { | |
|
||
void CommandHandle(http::HttpRequestPtr req, rapidjson::Value& result, | ||
rapidjson::Document::AllocatorType& alloc) override; | ||
private: | ||
PrometheusConfig prometheus_conf_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 还是不理解鉴权相关的参数放admin服务的意图,可以描述下 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 因为Prometheus拉取数据是要走admin服务的,我感觉只有在这里才能拿到http包头中的用户名密码信息,才能进行鉴权。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 看实现,只需要填充username和password就好了吧?不需要保留prometheus_conf_,只需要填充CommandHandle里的username和password字段 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改。 |
||
|
||
std::string username_; | ||
std::string password_; | ||
bool has_cfg; | ||
}; | ||
|
||
} // namespace trpc::admin | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,7 @@ class Plugin : public RefCounted<Plugin> { | |
|
||
/// @brief Stop the runtime environment of the plugin | ||
virtual void Stop() noexcept {} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 记得用clang-format把所有代码文件都格式化一遍(使用项目根目录的.clang-format配置的格式化规范) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里出现了不必要的空格 |
||
/// @brief destroy plugin internal resources | ||
virtual void Destroy() noexcept {} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,16 @@ filegroup( | |
]), | ||
) | ||
|
||
cc_library( | ||
name = "prometheus_pusher", | ||
srcs = ["prometheus_pusher.cc"], | ||
hdrs = ["prometheus_pusher.h"], | ||
deps = [ | ||
"//trpc/util/log:logging", | ||
"@com_github_jupp0r_prometheus_cpp//push", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "prometheus_conf", | ||
srcs = ["prometheus_conf.cc"], | ||
|
@@ -73,15 +83,20 @@ cc_library( | |
":prometheus_conf", | ||
":prometheus_conf_parser", | ||
"//trpc/util:prometheus", | ||
"@com_github_jupp0r_prometheus_cpp//core", | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不必要的换行,BUILD文件使用 buildifier 格式化一下 |
||
"//trpc/common/config:trpc_config", | ||
":prometheus_pusher", | ||
"//trpc/metrics", | ||
] + select({ | ||
"//conditions:default": [], | ||
"//trpc:trpc_include_prometheus": [ | ||
"@com_github_jupp0r_prometheus_cpp//pull", | ||
"@com_github_jupp0r_prometheus_cpp//push", | ||
], | ||
"//trpc:include_metrics_prometheus": [ | ||
"@com_github_jupp0r_prometheus_cpp//pull", | ||
"@com_github_jupp0r_prometheus_cpp//push", | ||
], | ||
}), | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
默认关闭prometheus,这行可以删掉