Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo committed Jul 2, 2023
1 parent 049a165 commit 940fed8
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
2 changes: 1 addition & 1 deletion exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ std::string PrometheusExporterUtils::MapToPrometheusName(
sanitized_name += "_ratio";
}

return sanitized_name;
return CleanUpString(SanitizeNames(sanitized_name));
}

metric_sdk::AggregationType PrometheusExporterUtils::getAggregationType(
Expand Down
159 changes: 159 additions & 0 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ class SanitizeNameTester
{
return PrometheusExporterUtils::SanitizeNames(name);
}
static std::string getPrometheusUnit(const std::string &unit_abbreviation)
{
return PrometheusExporterUtils::GetPrometheusUnit(unit_abbreviation);
}
static std::string getPrometheusPerUnit(const std::string &per_unit_abbreviation)
{
return PrometheusExporterUtils::GetPrometheusPerUnit(per_unit_abbreviation);
}
static std::string removeUnitPortionInBraces(const std::string &unit)
{
return PrometheusExporterUtils::RemoveUnitPortionInBraces(unit);
}
static std::string convertRateExpressedToPrometheusUnit(const std::string &rate_expressed_unit)
{
return PrometheusExporterUtils::ConvertRateExpressedToPrometheusUnit(rate_expressed_unit);
}
static std::string cleanUpString(const std::string &str)
{
return PrometheusExporterUtils::CleanUpString(str);
}
static std::string getEquivalentPrometheusUnit(const std::string &raw_metric_unit_name)
{
return PrometheusExporterUtils::GetEquivalentPrometheusUnit(raw_metric_unit_name);
}
static std::string mapToPrometheusName(const std::string &name,
const std::string &unit,
prometheus_client::MetricType prometheus_type)
{
return PrometheusExporterUtils::MapToPrometheusName(name, unit, prometheus_type);
}
};
} // namespace metrics
} // namespace exporter
Expand Down Expand Up @@ -155,4 +185,133 @@ TEST(PrometheusExporterUtils, SanitizeName)
ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name?__name:"), "name_name:");
}

TEST(PrometheusExporterUtils, PrometheusUnit)
{
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("d"), "days");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("h"), "hours");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("min"), "minutes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("s"), "seconds");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("ms"), "milliseconds");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("us"), "microseconds");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("ns"), "nanoseconds");

ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("By"), "bytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("KiBy"), "kibibytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("MiBy"), "mebibytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("GiBy"), "gibibytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("TiBy"), "tibibytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("KBy"), "kilobytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("MBy"), "megabytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("GBy"), "gigabytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("TBy"), "terabytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("B"), "bytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("KB"), "kilobytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("MB"), "megabytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("GB"), "gigabytes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("TB"), "terabytes");

ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("m"), "meters");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("V"), "volts");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("A"), "amperes");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("J"), "joules");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("W"), "watts");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("g"), "grams");

ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("Cel"), "celsius");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("Hz"), "hertz");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("1"), "");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("%"), "percent");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("$"), "dollars");
}

TEST(PrometheusExporterUtils, PrometheusPerUnit)
{
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusPerUnit("s"), "second");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusPerUnit("m"), "minute");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusPerUnit("h"), "hour");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusPerUnit("d"), "day");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusPerUnit("w"), "week");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusPerUnit("mo"), "month");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusPerUnit("y"), "year");
}

TEST(PrometheusExporterUtils, RemoveUnitPortionInBraces)
{
ASSERT_EQ(exporter::metrics::SanitizeNameTester::removeUnitPortionInBraces("{unit}"), "");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::removeUnitPortionInBraces("unit{unit}"), "unit");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::removeUnitPortionInBraces("unit_{unit}"),
"unit_");
}

TEST(PrometheusExporterUtils, ConvertRateExpressedToPrometheusUnit)
{
ASSERT_EQ(exporter::metrics::SanitizeNameTester::convertRateExpressedToPrometheusUnit("unit/d"),
"unit_per_day");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::convertRateExpressedToPrometheusUnit("unit/s"),
"unit_per_second");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::convertRateExpressedToPrometheusUnit("unit/"),
"unit/");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::convertRateExpressedToPrometheusUnit("unit"),
"unit");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::convertRateExpressedToPrometheusUnit("unit/m"),
"unit_per_minute");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::convertRateExpressedToPrometheusUnit("/m"),
"_per_minute");
}

TEST(PrometheusExporterUtils, CleanUpString)
{
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("unit/d"), "unit_d");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("unit/d_"), "unit_d");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("unit_/d_"), "unit_d");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("_unit_/d_"), "unit_d");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("_unit_d_"), "unit_d");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("_unit[]_d_"), "unit_d");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("_unit[67]_d_"), "unit_67_d");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("_unit[67_]_d_"), "unit_67_d");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::cleanUpString("_unit[_67_]_d_"), "unit_67_d");
}

TEST(PrometheusExporterUtils, GetEquivalentPrometheusUnit)
{
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getEquivalentPrometheusUnit("unit/d"),
"unit_per_day");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getEquivalentPrometheusUnit("unit/m"),
"unit_per_minute");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getEquivalentPrometheusUnit("unit"), "unit");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getEquivalentPrometheusUnit("unit_"), "unit");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getEquivalentPrometheusUnit("_unit_"), "unit");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getEquivalentPrometheusUnit(""), "");
}

TEST(PrometheusExporterUtils, MapToPrometheusName)
{
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"name", "unit", prometheus_client::MetricType::Counter),
"name_unit_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"name", "unit", prometheus_client::MetricType::Gauge),
"name_unit");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"name", "1", prometheus_client::MetricType::Gauge),
"name_ratio");

ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"_name_", "unit/s", prometheus_client::MetricType::Counter),
"name_unit_per_second_total");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"_name_", "unit/s", prometheus_client::MetricType::Gauge),
"name_unit_per_second");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"_name_", "1", prometheus_client::MetricType::Gauge),
"name_ratio");

ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"_name_", "unit/s", prometheus_client::MetricType::Histogram),
"name_unit_per_second");
ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName(
"_name_", "unit/s", prometheus_client::MetricType::Untyped),
"name_unit_per_second");
}

OPENTELEMETRY_END_NAMESPACE

0 comments on commit 940fed8

Please sign in to comment.