Skip to content
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

Fixup mbvar convert prometheus metrics format issue (#2082) #2235

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/brpc/builtin/prometheus_metrics_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class PrometheusMetricsDumper : public bvar::Dumper {
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] == '"') {
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/brpc/builtin/prometheus_metrics_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions test/brpc_prometheus_metrics_service_unittest.cpp
Original file line number Diff line number Diff line change
@@ -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\"}"));
}

}