| From 6de560b84fb4cc37461bc6698ea2effd64678465 Mon Sep 17 00:00:00 2001 |
| From: dylan <451809218@qq.com> |
| Date: Tue, 7 Nov 2023 14:05:37 +0800 |
| Subject: [PATCH] Fixup mbvar convert prometheus metrics format issue (#2082) |
| (#2235) |
| |
| --- |
| .../builtin/prometheus_metrics_service.cpp | 13 +++++- |
| src/brpc/builtin/prometheus_metrics_service.h | 1 + |
| ...pc_prometheus_metrics_service_unittest.cpp | 42 +++++++++++++++++++ |
| 3 files changed, 54 insertions(+), 2 deletions(-) |
| create mode 100644 test/brpc_prometheus_metrics_service_unittest.cpp |
| |
| 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/test/brpc_prometheus_metrics_service_unittest.cpp b/test/brpc_prometheus_metrics_service_unittest.cpp |
| new file mode 100644 |
| index 00000000..b5b0bc10 |
| --- /dev/null |
| +++ b/test/brpc_prometheus_metrics_service_unittest.cpp |
| @@ -0,0 +1,42 @@ |
| +// Licensed to the Apache Software Foundation (ASF) under one |
| +// or more contributor license agreements. See the NOTICE file |
| +// distributed with this work for additional information |
| +// regarding copyright ownership. The ASF licenses this file |
| +// to you under the Apache License, Version 2.0 (the |
| +// "License"); you may not use this file except in compliance |
| +// with the License. You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, |
| +// software distributed under the License is distributed on an |
| +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| +// KIND, either express or implied. See the License for the |
| +// specific language governing permissions and limitations |
| +// under the License. |
| + |
| +// Date: 2023/05/06 15:10:00 |
| + |
| +#include <gtest/gtest.h> |
| + |
| +#include "butil/strings/string_piece.h" |
| +#include "butil/iobuf.h" |
| +#include "brpc/builtin/prometheus_metrics_service.h" |
| + |
| +namespace { |
| + |
| +class PrometheusMetricsDumperTest : public testing::Test { |
| +protected: |
| + void SetUp() {} |
| + void TearDown() {} |
| +}; |
| + |
| +TEST_F(PrometheusMetricsDumperTest, GetMetricsName) { |
| + EXPECT_EQ("", brpc::GetMetricsName("")); |
| + |
| + EXPECT_EQ("commit_count", brpc::GetMetricsName("commit_count")); |
| + |
| + EXPECT_EQ("commit_count", brpc::GetMetricsName("commit_count{region=\"1000\"}")); |
| +} |
| + |
| +} |
| -- |
| 2.39.3 |
| |