@@ -102,13 +102,15 @@ std::string SanitizeLabel(std::string label_key)
102102 * Helper function to convert OpenTelemetry metrics data collection
103103 * to Prometheus metrics data collection
104104 *
105- * @param records a collection of metrics in OpenTelemetry
105+ * @param data a collection of metrics in OpenTelemetry
106106 * @return a collection of translated metrics that is acceptable by Prometheus
107107 */
108108std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateToPrometheus (
109109 const sdk::metrics::ResourceMetrics &data,
110110 bool populate_target_info,
111- bool without_otel_scope)
111+ bool without_otel_scope,
112+ bool without_units,
113+ bool without_type_suffix)
112114{
113115
114116 // initialize output vector
@@ -150,7 +152,9 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
150152 }
151153 const prometheus_client::MetricType type = TranslateType (kind, is_monotonic);
152154 metric_family.name = MapToPrometheusName (metric_data.instrument_descriptor .name_ ,
153- metric_data.instrument_descriptor .unit_ , type);
155+ metric_data.instrument_descriptor .unit_ , type,
156+ without_units, without_type_suffix);
157+ // TODO (psx95): Add tests to check compliance
154158 metric_family.type = type;
155159 const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope =
156160 without_otel_scope ? nullptr : instrumentation_info.scope_ ;
@@ -492,10 +496,19 @@ std::string PrometheusExporterUtils::CleanUpString(const std::string &str)
492496std::string PrometheusExporterUtils::MapToPrometheusName (
493497 const std::string &name,
494498 const std::string &unit,
495- prometheus_client::MetricType prometheus_type)
499+ prometheus_client::MetricType prometheus_type,
500+ bool without_units,
501+ bool without_type_suffix)
496502{
497503 auto sanitized_name = SanitizeNames (name);
498- std::string prometheus_equivalent_unit = GetEquivalentPrometheusUnit (unit);
504+ std::string prometheus_equivalent_unit;
505+ if (without_units)
506+ {
507+ prometheus_equivalent_unit = " " ;
508+ } else
509+ {
510+ prometheus_equivalent_unit = GetEquivalentPrometheusUnit (unit);
511+ }
499512
500513 // Append prometheus unit if not null or empty.
501514 if (!prometheus_equivalent_unit.empty () &&
@@ -505,7 +518,7 @@ std::string PrometheusExporterUtils::MapToPrometheusName(
505518 }
506519
507520 // Special case - counter
508- if (prometheus_type == prometheus_client::MetricType::Counter)
521+ if (prometheus_type == prometheus_client::MetricType::Counter && !without_type_suffix )
509522 {
510523 auto t_pos = sanitized_name.rfind (" _total" );
511524 bool ends_with_total = t_pos == sanitized_name.size () - 6 ;
@@ -517,7 +530,7 @@ std::string PrometheusExporterUtils::MapToPrometheusName(
517530
518531 // Special case - gauge
519532 if (unit == " 1" && prometheus_type == prometheus_client::MetricType::Gauge &&
520- sanitized_name.find (" ratio" ) == std::string::npos)
533+ sanitized_name.find (" ratio" ) == std::string::npos && !without_type_suffix )
521534 {
522535 sanitized_name += " _ratio" ;
523536 }
0 commit comments