Skip to content

Commit

Permalink
Fix prometheus metric format (apache#2825)
Browse files Browse the repository at this point in the history
To meet specification of prometheus metric, there should be only
one TYPE field for the same metric name
  • Loading branch information
William-Zhu committed Feb 23, 2025
1 parent f642491 commit 6861f5a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/brpc/builtin/prometheus_metrics_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class PrometheusMetricsDumper : public bvar::Dumper {
butil::IOBufBuilder* _os;
const std::string _server_prefix;
std::map<std::string, SummaryItems> _m;
std::string _last_metric_name;
};

butil::StringPiece GetMetricsName(const std::string& name) {
Expand All @@ -102,9 +103,14 @@ 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';
// To meet specification of prometheus metric, there should be only
// one TYPE field for the same metric name.
if (metrics_name.as_string() != _last_metric_name) {
*_os << "# HELP " << metrics_name << '\n'
<< "# TYPE " << metrics_name << " gauge" << '\n';
}
*_os << name << " " << desc << '\n';
_last_metric_name = metrics_name.as_string();
return true;
}

Expand Down

0 comments on commit 6861f5a

Please sign in to comment.