Skip to content

Commit

Permalink
Appends units if not present in metric name
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Apr 29, 2023
1 parent bf33ae0 commit 0cd3aab
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -653,24 +653,27 @@ static Collection<? extends PointData> getPoints(MetricData metricData) {

// Visible for testing
static String metricName(MetricData rawMetric, PrometheusType type) {
String name = rawMetric.getName();
String name = NameSanitizer.INSTANCE.apply(rawMetric.getName());
String prometheusEquivalentUnit =
PrometheusUnitsHelper.getEquivalentPrometheusUnit(rawMetric.getUnit());
// append prometheus unit if not null or empty.
if (!StringUtils.isNullOrEmpty(prometheusEquivalentUnit)) {
if (!StringUtils.isNullOrEmpty(prometheusEquivalentUnit)
&& !name.contains(prometheusEquivalentUnit)) {
name = name + "_" + prometheusEquivalentUnit;
}

// special case - counter
if (type == PrometheusType.COUNTER) {
if (type == PrometheusType.COUNTER && !name.contains("total")) {
name = name + "_total";
}
// special case - gauge
if (rawMetric.getUnit().equals("1") && type == PrometheusType.GAUGE) {
if (rawMetric.getUnit().equals("1")
&& type == PrometheusType.GAUGE
&& !name.contains("ratio")) {
name = name + "_ratio";
}

return NameSanitizer.INSTANCE.apply(name);
return name;
}

private static double getExemplarValue(ExemplarData exemplar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ private static Stream<Arguments> provideRawMetricDataForTest() {
// special case for gauge
Arguments.of(
sampleMetricDataGenerator("sample", "1", PrometheusType.GAUGE), "sample_ratio"),
// special case for gauge with drop eligible unit
// special case for gauge with drop - metric unit should match "1"
Arguments.of(
sampleMetricDataGenerator("sample", "1{dropped}", PrometheusType.GAUGE), "sample"),
// Gauge without "1" as unit
Expand All @@ -496,9 +496,12 @@ private static Stream<Arguments> provideRawMetricDataForTest() {
Arguments.of(
sampleMetricDataGenerator("sample", "unit", PrometheusType.COUNTER),
"sample_unit_total"),
// special case unit "1", but no gauge
// special case unit "1", but no gauge - "1" is dropped
Arguments.of(
sampleMetricDataGenerator("sample", "1", PrometheusType.COUNTER), "sample_total"),
// units expressed as numbers other than 1 are retained
Arguments.of(
sampleMetricDataGenerator("sample", "2", PrometheusType.COUNTER), "sample_2_total"),
// metric name with unsupported characters
Arguments.of(
sampleMetricDataGenerator("s%%ple", "%/m", PrometheusType.SUMMARY),
Expand All @@ -510,6 +513,36 @@ private static Stream<Arguments> provideRawMetricDataForTest() {
// metric unit as a number other than 1 is not treated specially
Arguments.of(
sampleMetricDataGenerator("metric_name", "2", PrometheusType.SUMMARY), "metric_name_2"),
// metric unit is not appended if the name already contains the unit
Arguments.of(
sampleMetricDataGenerator("metric_name_total", "total", PrometheusType.COUNTER),
"metric_name_total"),
// metric unit is not appended if the name already contains the unit - special case for
// total with non-counter type
Arguments.of(
sampleMetricDataGenerator("metric_name_total", "total", PrometheusType.SUMMARY),
"metric_name_total"),
// metric unit not appended if present in metric name - special case for ratio
Arguments.of(
sampleMetricDataGenerator("metric_name_ratio", "1", PrometheusType.GAUGE),
"metric_name_ratio"),
// metric unit not appended if present in metric name - special case for ratio - unit not
// gauge
Arguments.of(
sampleMetricDataGenerator("metric_name_ratio", "1", PrometheusType.SUMMARY),
"metric_name_ratio"),
// metric unit is not appended if the name already contains the unit - unit can be anywhere
Arguments.of(
sampleMetricDataGenerator("metric_hertz", "hertz", PrometheusType.GAUGE),
"metric_hertz"),
// metric unit is not appended if the name already contains the unit - applies to every unit
Arguments.of(
sampleMetricDataGenerator("metric_hertz_total", "hertz_total", PrometheusType.COUNTER),
"metric_hertz_total"),
// metric unit is not appended if the name already contains the unit - order matters
Arguments.of(
sampleMetricDataGenerator("metric_total_hertz", "hertz_total", PrometheusType.COUNTER),
"metric_total_hertz_hertz_total"),
// metric name cannot start with a number
Arguments.of(
sampleMetricDataGenerator("2_metric_name", "By", PrometheusType.SUMMARY),
Expand Down

0 comments on commit 0cd3aab

Please sign in to comment.