From 4d1a35b97c14c231ec7500c35a2f608d1076976f Mon Sep 17 00:00:00 2001 From: mandarjog Date: Mon, 19 Aug 2019 12:11:27 -0700 Subject: [PATCH] Prometheus scraper: Opt out of automatic namespacing (#138) * Offer a way to opt out of automatic namespacing in prometheus Signed-off-by: Mandar U Jog --- source/server/http/admin.cc | 6 ++++++ test/server/http/admin_test.cc | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/source/server/http/admin.cc b/source/server/http/admin.cc index 5faacc1eb76d..f2389c73904d 100644 --- a/source/server/http/admin.cc +++ b/source/server/http/admin.cc @@ -833,6 +833,12 @@ std::string PrometheusStatsFormatter::formattedTags(const std::vector 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/ diff --git a/test/server/http/admin_test.cc b/test/server/http/admin_test.cc index 481a1bbfd533..b6c34ebf5001 100644 --- a/test/server/http/admin_test.cc +++ b/test/server/http/admin_test.cc @@ -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";