Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Oct 16, 2024
1 parent 66c9c73 commit d2747a0
Showing 1 changed file with 92 additions and 2 deletions.
94 changes: 92 additions & 2 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ class SanitizeNameTester
}
static std::string mapToPrometheusName(const std::string &name,
const std::string &unit,
prometheus_client::MetricType prometheus_type)
prometheus_client::MetricType prometheus_type,
bool without_units = false,
bool without_type_suffix = false)
{
return PrometheusExporterUtils::MapToPrometheusName(name, unit, prometheus_type, false, false);
return PrometheusExporterUtils::MapToPrometheusName(name, unit, prometheus_type, without_units,
without_type_suffix);
}
};
} // namespace metrics
Expand Down Expand Up @@ -419,6 +422,93 @@ TEST(PrometheusExporterUtils, ConvertRateExpressedToPrometheusUnit)
"_per_minute");
}

TEST(PromentheusExporterUtils, PrometheusNameMapping)
{
// General test cases on unit expansions and name sanitization
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric___name", "g", prometheus::MetricType::Counter),
"sample_metric_name_grams_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "s", prometheus::MetricType::Counter),
"sample_metric_name_seconds_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "s", prometheus::MetricType::Gauge),
"sample_metric_name_seconds");
// Test without_units & without_type_suffix with Counters and unit = 1
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Counter),
"sample_metric_name_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Counter, true, false),
"sample_metric_name_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Counter, false, true),
"sample_metric_name");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Counter, true, true),
"sample_metric_name");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Counter, true, true),
"sample_metric_name");
// Test without_units & without_type_suffix with Counters and non-special units
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "%", prometheus::MetricType::Counter),
"sample_metric_name_percent_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "m", prometheus::MetricType::Counter, true, false),
"sample_metric_name_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "By", prometheus::MetricType::Counter, false, true),
"sample_metric_name_bytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "s", prometheus::MetricType::Counter, true, true),
"sample_metric_name");
// Special case Gauges & ratio
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Gauge),
"sample_metric_name_ratio");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Gauge, false, true),
"sample_metric_name_ratio");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Gauge, true, false),
"sample_metric_name");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "1", prometheus::MetricType::Gauge, true, true),
"sample_metric_name");
// Test without_type_suffix affects only counters
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Counter),
"sample_metric_name_hertz_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Counter, false, true),
"sample_metric_name_hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Gauge),
"sample_metric_name_hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Gauge, false, true),
"sample_metric_name_hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Histogram),
"sample_metric_name_hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Histogram, false, true),
"sample_metric_name_hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Summary),
"sample_metric_name_hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Summary, false, true),
"sample_metric_name_hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Info),
"sample_metric_name_hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"sample_metric_name", "Hz", prometheus::MetricType::Info, false, true),
"sample_metric_name_hertz");
}

TEST_F(AttributeCollisionTest, JoinsCollidingKeys)
{
CheckTranslation({{"foo.a", "value1"}, {"foo_a", "value2"}}, {{"foo_a", "value1;value2"},
Expand Down

0 comments on commit d2747a0

Please sign in to comment.