From 5a669dead5b0303d5a7b637002aa82785e035c45 Mon Sep 17 00:00:00 2001 From: snehavats1404 Date: Mon, 13 Jan 2025 12:49:47 +0530 Subject: [PATCH 1/5] fixing --- src/bvar/variable.cpp | 60 ++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/src/bvar/variable.cpp b/src/bvar/variable.cpp index e4f093555e..12ad34f963 100644 --- a/src/bvar/variable.cpp +++ b/src/bvar/variable.cpp @@ -539,29 +539,43 @@ int Variable::dump_exposed(Dumper* dumper, const DumpOptions* poptions) { // ============= export to files ============== std::string read_command_name() { - std::ifstream fin("/proc/self/stat"); - if (!fin.is_open()) { - return std::string(); - } - int pid = 0; - std::string command_name; - fin >> pid >> command_name; - if (!fin.good()) { - return std::string(); - } - // Although the man page says the command name is in parenthesis, for - // safety we normalize the name. - std::string s; - if (command_name.size() >= 2UL && command_name[0] == '(' && - butil::back_char(command_name) == ')') { - // remove parenthesis. - to_underscored_name(&s, - butil::StringPiece(command_name.data() + 1, - command_name.size() - 2UL)); - } else { - to_underscored_name(&s, command_name); - } - return s; + std::ifstream fin("/proc/self/cmdline", std::ios::in | std::ios::binary); + if (!fin.is_open()) + { + return std::string(); + } + int pid = 0; + // read entire content into a buffer + std::ostringstream oss; + oss << fin.rdbuf(); + std::string cmdline = oss.str(); + if (cmdline.empty()) + { + return std::string(); + // return empty string + } + + // extract the first null-terminated string + std::string::size_type pos = cmdline.find('\0'); + std::string command_name = (pos != std::string::npos) ? cmdline.substr(0, pos) : cmdline; + std::string s; + + // Although the man page says the command name is in parenthesis, for + // safety we normalize the name. + std::string s; + if (command_name.size() >= 2UL && command_name[0] == '(' && + butil::back_char(command_name) == ')') + { + // remove parenthesis. + to_underscored_name(&s, + butil::StringPiece(command_name.data() + 1, + command_name.size() - 2UL)); + } + else + { + to_underscored_name(&s, command_name); + } + return s; } class FileDumper : public Dumper { From cd87b7ee8e7a5fb49c20978c2af61e39b4745c7e Mon Sep 17 00:00:00 2001 From: snehavats1404 Date: Mon, 13 Jan 2025 14:18:40 +0530 Subject: [PATCH 2/5] fixing emission-tracking --- .../builtin/prometheus_metrics_service.cpp | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/brpc/builtin/prometheus_metrics_service.cpp b/src/brpc/builtin/prometheus_metrics_service.cpp index 88f675bb81..d903c86248 100644 --- a/src/brpc/builtin/prometheus_metrics_service.cpp +++ b/src/brpc/builtin/prometheus_metrics_service.cpp @@ -80,6 +80,7 @@ class PrometheusMetricsDumper : public bvar::Dumper { butil::IOBufBuilder* _os; const std::string _server_prefix; std::map _m; + std::set emitted_metrics;//added to track emitted metric names }; butil::StringPiece GetMetricsName(const std::string& name) { @@ -102,9 +103,13 @@ bool PrometheusMetricsDumper::dump(const std::string& name, auto metrics_name = GetMetricsName(name); - *_os << "# HELP " << metrics_name << '\n' - << "# TYPE " << metrics_name << " gauge" << '\n' - << name << " " << desc << '\n'; + if(emitted_metrics.find(metrics_name.as_string()) == emitted_metrics.end()){ + *_os<< "# HELP " << metrics_name <<'\n' + << "# TYPE " << metrics_name <<" gauge\n"; + emitted_metrics.insert(metrics_name.as_string()); + } + + *_os << name << " " << desc << '\n'; return true; } @@ -164,9 +169,12 @@ bool PrometheusMetricsDumper::DumpLatencyRecorderSuffix( if (!si->IsComplete()) { return true; } - *_os << "# HELP " << si->metric_name << '\n' - << "# TYPE " << si->metric_name << " summary\n" - << si->metric_name << "{quantile=\"" + if(emitted_metrics.find(si->metric_name) == emitted_metrics.end()){ + *_os << "# HELP " << si->metric_name << '\n' + << "# TYPE " << si->metric_name << " summary\n"; + emitted_metrics.insert(si->metric_name); + } + *_os << si->metric_name << "{quantile=\"" << (double)(bvar::FLAGS_bvar_latency_p1) / 100 << "\"} " << si->latency_percentiles[0] << '\n' << si->metric_name << "{quantile=\"" From 5a1707fe99bc1934063a7eb448295c798127f647 Mon Sep 17 00:00:00 2001 From: snehavats1404 Date: Mon, 13 Jan 2025 14:28:42 +0530 Subject: [PATCH 3/5] fixing emission-tracking --- src/bvar/variable.cpp | 60 +++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/src/bvar/variable.cpp b/src/bvar/variable.cpp index 12ad34f963..4a34a8b7b2 100644 --- a/src/bvar/variable.cpp +++ b/src/bvar/variable.cpp @@ -539,43 +539,29 @@ int Variable::dump_exposed(Dumper* dumper, const DumpOptions* poptions) { // ============= export to files ============== std::string read_command_name() { - std::ifstream fin("/proc/self/cmdline", std::ios::in | std::ios::binary); - if (!fin.is_open()) - { - return std::string(); - } - int pid = 0; - // read entire content into a buffer - std::ostringstream oss; - oss << fin.rdbuf(); - std::string cmdline = oss.str(); - if (cmdline.empty()) - { - return std::string(); - // return empty string - } - - // extract the first null-terminated string - std::string::size_type pos = cmdline.find('\0'); - std::string command_name = (pos != std::string::npos) ? cmdline.substr(0, pos) : cmdline; - std::string s; - - // Although the man page says the command name is in parenthesis, for - // safety we normalize the name. - std::string s; - if (command_name.size() >= 2UL && command_name[0] == '(' && - butil::back_char(command_name) == ')') - { - // remove parenthesis. - to_underscored_name(&s, - butil::StringPiece(command_name.data() + 1, - command_name.size() - 2UL)); - } - else - { - to_underscored_name(&s, command_name); - } - return s; + std::ifstream fin("/proc/self/stat"); + if (!fin.is_open()) { + return std::string(); + } + int pid = 0; + std::string command_name; + fin >> pid >> command_name; + if (!fin.good()) { + return std::string(); + } + // Although the man page says the command name is in parenthesis, for + // safety we normalize the name. + std::string s; + if (command_name.size() >= 2UL && command_name[0] == '(' && + butil::back_char(command_name) == ')') { + // remove parenthesis. + to_underscored_name(&s, + butil::StringPiece(command_name.data() + 1, + command_name.size() - 2UL)); + } else { + to_underscored_name(&s, command_name); + } + return s } class FileDumper : public Dumper { From bb7363d8a1eab559ef96a8370f124239c412dbf1 Mon Sep 17 00:00:00 2001 From: snehavats1404 Date: Mon, 13 Jan 2025 14:31:05 +0530 Subject: [PATCH 4/5] fixing emission-tracking --- src/bvar/variable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bvar/variable.cpp b/src/bvar/variable.cpp index 4a34a8b7b2..e4f093555e 100644 --- a/src/bvar/variable.cpp +++ b/src/bvar/variable.cpp @@ -561,7 +561,7 @@ std::string read_command_name() { } else { to_underscored_name(&s, command_name); } - return s + return s; } class FileDumper : public Dumper { From a0507d6c9cfa62ac8c7f196788d7a2d6e6dcf9ea Mon Sep 17 00:00:00 2001 From: snehavats1404 Date: Mon, 13 Jan 2025 23:56:32 +0530 Subject: [PATCH 5/5] fixing spaces based on review --- src/brpc/builtin/prometheus_metrics_service.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/brpc/builtin/prometheus_metrics_service.cpp b/src/brpc/builtin/prometheus_metrics_service.cpp index d903c86248..27b89fa717 100644 --- a/src/brpc/builtin/prometheus_metrics_service.cpp +++ b/src/brpc/builtin/prometheus_metrics_service.cpp @@ -103,10 +103,10 @@ bool PrometheusMetricsDumper::dump(const std::string& name, auto metrics_name = GetMetricsName(name); - if(emitted_metrics.find(metrics_name.as_string()) == emitted_metrics.end()){ - *_os<< "# HELP " << metrics_name <<'\n' - << "# TYPE " << metrics_name <<" gauge\n"; - emitted_metrics.insert(metrics_name.as_string()); + if (emitted_metrics.find(metrics_name.as_string()) == emitted_metrics.end()) { + *_os << "# HELP " << metrics_name << '\n' + << "# TYPE " << metrics_name << " gauge\n"; + emitted_metrics.insert(metrics_name.as_string()); } *_os << name << " " << desc << '\n'; @@ -169,7 +169,7 @@ bool PrometheusMetricsDumper::DumpLatencyRecorderSuffix( if (!si->IsComplete()) { return true; } - if(emitted_metrics.find(si->metric_name) == emitted_metrics.end()){ + if (emitted_metrics.find(si->metric_name) == emitted_metrics.end()) { *_os << "# HELP " << si->metric_name << '\n' << "# TYPE " << si->metric_name << " summary\n"; emitted_metrics.insert(si->metric_name);