-
Notifications
You must be signed in to change notification settings - Fork 83
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 all commits
809dfe0
ccfc856
8a1de0e
c879973
e1916f8
b980723
7f42fbd
cd24612
816bc28
3d907d6
2309769
1a5bb30
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 |
---|---|---|
|
@@ -20,10 +20,62 @@ namespace trpc::admin { | |
|
||
PrometheusHandler::PrometheusHandler() { description_ = "[GET /metrics] get prometheus metrics"; } | ||
|
||
void PrometheusHandler::Init() { | ||
PrometheusConfig prometheus_conf; | ||
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")) { | ||
auth_conf_.username = cfg["username"]; | ||
auth_conf_.password = cfg["password"]; | ||
} else { | ||
TRPC_LOG_WARN("can not found prometheus auth config"); | ||
} | ||
} | ||
|
||
bool PrometheusHandler::CheckAuth(std::string token) { | ||
auto splited = Split(token, ' '); | ||
if (splited.size() != 2) { | ||
TRPC_FMT_ERROR("error token: {}", token); | ||
return false; | ||
} | ||
if (splited[0] != "Basic") { | ||
TRPC_FMT_ERROR("error token: {}", token); | ||
return false; | ||
} | ||
|
||
std::string username_pwd = http::Base64Decode(std::begin(splited[1]), std::end(splited[1])); | ||
auto sp = Split(username_pwd, ':'); | ||
if (sp.size() != 2) { | ||
TRPC_FMT_ERROR("error token: {}", token); | ||
return false; | ||
} | ||
|
||
auto username = sp[0], pwd = sp[1]; | ||
if (username != auth_conf_.username || pwd != auth_conf_.password) { | ||
TRPC_FMT_ERROR("error username or password: username: {}, password: {}", username, pwd); | ||
return 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. 已修改。 |
||
return true; | ||
} | ||
|
||
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 (auth_conf_.username.size() && auth_conf_.password.size()) { | ||
std::string token = req->GetHeader("authorization"); | ||
if (!CheckAuth(token)) { | ||
result.AddMember("message", "wrong request without right username or password", alloc); | ||
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 |
---|---|---|
|
@@ -15,7 +15,13 @@ | |
#pragma once | ||
|
||
#include "trpc/admin/admin_handler.h" | ||
#include "trpc/common/config/trpc_config.h" | ||
#include "trpc/log/trpc_log.h" | ||
#include "trpc/metrics/prometheus/prometheus_metrics.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/prometheus.h" | ||
#include "trpc/util/string/string_helper.h" | ||
#include "trpc/util/time.h" | ||
|
||
namespace trpc::admin { | ||
|
||
|
@@ -24,8 +30,18 @@ class PrometheusHandler : public AdminHandlerBase { | |
public: | ||
PrometheusHandler(); | ||
|
||
void Init(); | ||
|
||
void CommandHandle(http::HttpRequestPtr req, rapidjson::Value& result, | ||
rapidjson::Document::AllocatorType& alloc) override; | ||
|
||
private: | ||
bool CheckAuth(std::string token); | ||
|
||
struct AuthConf { | ||
std::string username; | ||
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. 为啥不使用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. 是可以使用token,一开始的实现也是token,但是我查了资料,pushgateway没办法用token来鉴权。所以如果pull模式用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. push和pull确实是两套鉴权模式吧,配置区分开就好 |
||
std::string password; | ||
} auth_conf_; | ||
}; | ||
|
||
} // 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", | ||
], | ||
}), | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,26 @@ struct PrometheusConfig { | |
/// The default label attached to each RPC metrics data | ||
std::map<std::string, std::string> const_labels; | ||
|
||
std::map<std::string, std::string> auth_cfg; | ||
|
||
struct PushMode { | ||
bool enabled = false; | ||
std::string gateway_url; | ||
std::string job_name; | ||
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. job_name这个字段有什么用? |
||
int push_interval_seconds = 15; | ||
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_interval,单位为毫秒吧 |
||
} push_mode; | ||
|
||
void Display() const; | ||
}; | ||
|
||
} // namespace trpc | ||
|
||
namespace YAML { | ||
|
||
template <> | ||
struct convert<trpc::PrometheusConfig> { | ||
static YAML::Node encode(const trpc::PrometheusConfig& config); | ||
static bool decode(const YAML::Node& node, trpc::PrometheusConfig& config); | ||
}; | ||
|
||
} // namespace YAML |
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,这行可以删掉