diff --git a/src/brpc/builtin/prometheus_metrics_service.cpp b/src/brpc/builtin/prometheus_metrics_service.cpp index 7bf8bbf359..88f675bb81 100644 --- a/src/brpc/builtin/prometheus_metrics_service.cpp +++ b/src/brpc/builtin/prometheus_metrics_service.cpp @@ -82,6 +82,12 @@ class PrometheusMetricsDumper : public bvar::Dumper { std::map _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 c844e1e7a0..541b395c82 100644 --- a/src/brpc/builtin/prometheus_metrics_service.h +++ b/src/brpc/builtin/prometheus_metrics_service.h @@ -31,6 +31,7 @@ class PrometheusMetricsService : public brpc_metrics { ::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 0000000000..b5b0bc10d5 --- /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 + +#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\"}")); +} + +}