Skip to content

Commit

Permalink
Prometheus scraper: Opt out of automatic namespacing (envoyproxy#138)
Browse files Browse the repository at this point in the history
* Offer a way to opt out of automatic namespacing in prometheus

Signed-off-by: Mandar U Jog <[email protected]>
  • Loading branch information
mandarjog authored and jplevyak committed Aug 19, 2019
1 parent d75578d commit 4d1a35b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions source/server/http/admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,12 @@ std::string PrometheusStatsFormatter::formattedTags(const std::vector<Stats::Tag
}

std::string PrometheusStatsFormatter::metricName(const std::string& extractedName) {
// Offer a way to opt out of automatic namespacing.
// If metric name starts with "_" it will be trimmed but not namespaced.
// It is the responsibility of the metric creator to ensure proper namespacing.
if (extractedName.size() > 1 && extractedName[0] == '_') {
return sanitizeName(extractedName.substr(1));
}
// Add namespacing prefix to avoid conflicts, as per best practice:
// https://prometheus.io/docs/practices/naming/#metric-names
// Also, naming conventions on https://prometheus.io/docs/concepts/data_model/
Expand Down
7 changes: 7 additions & 0 deletions test/server/http/admin_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,13 @@ TEST_F(PrometheusStatsFormatterTest, MetricName) {
EXPECT_EQ(expected, actual);
}

TEST_F(PrometheusStatsFormatterTest, MetricNameOptOut) {
std::string raw = "_vulture.eats-liver";
std::string expected = "vulture_eats_liver";
auto actual = PrometheusStatsFormatter::metricName(raw);
EXPECT_EQ(expected, actual);
}

TEST_F(PrometheusStatsFormatterTest, SanitizeMetricName) {
std::string raw = "An.artist.plays-violin@019street";
std::string expected = "envoy_An_artist_plays_violin_019street";
Expand Down

0 comments on commit 4d1a35b

Please sign in to comment.