Skip to content

Commit 618f850

Browse files
committed
refactor code to address comments
1 parent 19d20cf commit 618f850

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

exporters/prometheus/src/exporter_utils.cc

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -500,40 +500,38 @@ std::string PrometheusExporterUtils::MapToPrometheusName(
500500
bool without_type_suffix)
501501
{
502502
auto sanitized_name = SanitizeNames(name);
503-
std::string prometheus_equivalent_unit;
504-
if (without_units)
503+
// append unit suffixes
504+
if (!without_units)
505505
{
506-
prometheus_equivalent_unit = "";
507-
}
508-
else
509-
{
510-
prometheus_equivalent_unit = GetEquivalentPrometheusUnit(unit);
511-
}
512-
513-
// Append prometheus unit if not null or empty.
514-
if (!prometheus_equivalent_unit.empty() &&
515-
sanitized_name.find(prometheus_equivalent_unit) == std::string::npos)
516-
{
517-
sanitized_name += "_" + prometheus_equivalent_unit;
518-
}
519-
520-
// Special case - counter
521-
if (prometheus_type == prometheus_client::MetricType::Counter && !without_type_suffix)
522-
{
523-
auto t_pos = sanitized_name.rfind("_total");
524-
bool ends_with_total = t_pos == sanitized_name.size() - 6;
525-
if (!ends_with_total)
506+
std::string prometheus_equivalent_unit = GetEquivalentPrometheusUnit(unit);
507+
// Append prometheus unit if not null or empty.
508+
if (!prometheus_equivalent_unit.empty() &&
509+
sanitized_name.find(prometheus_equivalent_unit) == std::string::npos)
510+
{
511+
sanitized_name += "_" + prometheus_equivalent_unit;
512+
}
513+
// Special case - gauge
514+
if (unit == "1" && prometheus_type == prometheus_client::MetricType::Gauge &&
515+
sanitized_name.find("ratio") == std::string::npos)
526516
{
527-
sanitized_name += "_total";
517+
// this is replacing the unit name
518+
sanitized_name += "_ratio";
528519
}
529520
}
530521

531-
// Special case - gauge
532-
if (unit == "1" && prometheus_type == prometheus_client::MetricType::Gauge &&
533-
sanitized_name.find("ratio") == std::string::npos && !without_units)
522+
// append type suffixes
523+
if (!without_type_suffix)
534524
{
535-
// this is replacing the unit name
536-
sanitized_name += "_ratio";
525+
// Special case - counter
526+
if (prometheus_type == prometheus_client::MetricType::Counter)
527+
{
528+
auto t_pos = sanitized_name.rfind("_total");
529+
bool ends_with_total = t_pos == sanitized_name.size() - 6;
530+
if (!ends_with_total)
531+
{
532+
sanitized_name += "_total";
533+
}
534+
}
537535
}
538536

539537
return CleanUpString(SanitizeNames(sanitized_name));

0 commit comments

Comments
 (0)