This is an automated email from the ASF dual-hosted git repository. wwbmmm pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brpc.git
The following commit(s) were added to refs/heads/master by this push: new 6de560b8 Fixup mbvar convert prometheus metrics format issue (#2082) (#2235) 6de560b8 is described below commit 6de560b84fb4cc37461bc6698ea2effd64678465 Author: dylan <451809...@qq.com> AuthorDate: Tue Nov 7 14:05:37 2023 +0800 Fixup mbvar convert prometheus metrics format issue (#2082) (#2235) --- src/brpc/builtin/prometheus_metrics_service.cpp | 13 ++++++++-- src/brpc/builtin/prometheus_metrics_service.h | 1 + .../brpc_prometheus_metrics_service_unittest.cpp | 30 ++++++++++++---------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/brpc/builtin/prometheus_metrics_service.cpp b/src/brpc/builtin/prometheus_metrics_service.cpp index 7bf8bbf3..88f675bb 100644 --- a/src/brpc/builtin/prometheus_metrics_service.cpp +++ b/src/brpc/builtin/prometheus_metrics_service.cpp @@ -82,6 +82,12 @@ private: std::map<std::string, SummaryItems> _m; }; +butil::StringPiece GetMetricsName(const std::string& name) { + auto pos = name.find_first_of('{'); + int size = (pos == std::string::npos) ? name.size() : pos; + return butil::StringPiece(name.data(), size); +} + bool PrometheusMetricsDumper::dump(const std::string& name, const butil::StringPiece& desc) { if (!desc.empty() && desc[0] == '"') { @@ -93,8 +99,11 @@ bool PrometheusMetricsDumper::dump(const std::string& name, // Leave it to DumpLatencyRecorderSuffix to output Summary. return true; } - *_os << "# HELP " << name << '\n' - << "# TYPE " << name << " gauge" << '\n' + + auto metrics_name = GetMetricsName(name); + + *_os << "# HELP " << metrics_name << '\n' + << "# TYPE " << metrics_name << " gauge" << '\n' << name << " " << desc << '\n'; return true; } diff --git a/src/brpc/builtin/prometheus_metrics_service.h b/src/brpc/builtin/prometheus_metrics_service.h index c844e1e7..541b395c 100644 --- a/src/brpc/builtin/prometheus_metrics_service.h +++ b/src/brpc/builtin/prometheus_metrics_service.h @@ -31,6 +31,7 @@ public: ::google::protobuf::Closure* done) override; }; +butil::StringPiece GetMetricsName(const std::string& name); int DumpPrometheusMetricsToIOBuf(butil::IOBuf* output); } // namepace brpc diff --git a/src/brpc/builtin/prometheus_metrics_service.h b/test/brpc_prometheus_metrics_service_unittest.cpp similarity index 58% copy from src/brpc/builtin/prometheus_metrics_service.h copy to test/brpc_prometheus_metrics_service_unittest.cpp index c844e1e7..b5b0bc10 100644 --- a/src/brpc/builtin/prometheus_metrics_service.h +++ b/test/brpc_prometheus_metrics_service_unittest.cpp @@ -15,24 +15,28 @@ // specific language governing permissions and limitations // under the License. +// Date: 2023/05/06 15:10:00 -#ifndef BRPC_PROMETHEUS_METRICS_SERVICE_H -#define BRPC_PROMETHEUS_METRICS_SERVICE_H +#include <gtest/gtest.h> -#include "brpc/builtin_service.pb.h" +#include "butil/strings/string_piece.h" +#include "butil/iobuf.h" +#include "brpc/builtin/prometheus_metrics_service.h" -namespace brpc { +namespace { -class PrometheusMetricsService : public brpc_metrics { -public: - void default_method(::google::protobuf::RpcController* cntl_base, - const ::brpc::MetricsRequest* request, - ::brpc::MetricsResponse* response, - ::google::protobuf::Closure* done) override; +class PrometheusMetricsDumperTest : public testing::Test { +protected: + void SetUp() {} + void TearDown() {} }; -int DumpPrometheusMetricsToIOBuf(butil::IOBuf* output); +TEST_F(PrometheusMetricsDumperTest, GetMetricsName) { + EXPECT_EQ("", brpc::GetMetricsName("")); -} // namepace brpc + EXPECT_EQ("commit_count", brpc::GetMetricsName("commit_count")); -#endif // BRPC_PROMETHEUS_METRICS_SERVICE_H + EXPECT_EQ("commit_count", brpc::GetMetricsName("commit_count{region=\"1000\"}")); +} + +} --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org