diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/AbstractMeterRegistry.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/AbstractMeterRegistry.java index 91ac7b0889..fa3208114d 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/AbstractMeterRegistry.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/AbstractMeterRegistry.java @@ -206,6 +206,7 @@ private class DistributionSummaryBuilder implements DistributionSummary.Builder private Histogram histogram; private final List tags = new ArrayList<>(); private String description; + private String baseUnit; private DistributionSummaryBuilder(String name) { this.name = name; @@ -233,10 +234,17 @@ public DistributionSummary.Builder description(String description) { return this; } + @Override + public DistributionSummary.Builder baseUnit(String unit) { + this.baseUnit = unit; + return this; + } + @Override public DistributionSummary create() { return registerMeterIfNecessary(DistributionSummary.class, name, tags, id -> - newDistributionSummary(id.getConventionName(Meter.Type.DistributionSummary), id.getTags(), description, quantiles, histogram)); + newDistributionSummary(id.getConventionName(Meter.Type.DistributionSummary, baseUnit), id.getTags(), + description, quantiles, histogram)); } } @@ -449,8 +457,12 @@ String getName() { /** * The formatted name matching this registry's naming convention */ + String getConventionName(Meter.Type type, String baseUnit) { + return namingConvention.name(name, type, baseUnit); + } + String getConventionName(Meter.Type type) { - return namingConvention.name(name, type); + return getConventionName(type, null); } /** diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/DistributionSummary.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/DistributionSummary.java index 280bc151a9..6cf48e10f8 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/DistributionSummary.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/DistributionSummary.java @@ -56,6 +56,8 @@ default Builder tags(String... tags) { Builder description(String description); + Builder baseUnit(String unit); + DistributionSummary create(); } diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/NamingConvention.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/NamingConvention.java index e987c60066..71d9a89c7d 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/NamingConvention.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/NamingConvention.java @@ -34,7 +34,7 @@ public interface NamingConvention { NamingConvention snakeCase = new NamingConvention() { @Override - public String name(String name, Meter.Type type) { + public String name(String name, Meter.Type type, String baseUnit) { return toSnakeCase(name); } @@ -50,7 +50,7 @@ private String toSnakeCase(String value) { NamingConvention camelCase = new NamingConvention() { @Override - public String name(String name, Meter.Type type) { + public String name(String name, Meter.Type type, String baseUnit) { return toCamelCase(name); } @@ -82,7 +82,11 @@ private String toCamelCase(String value) { } }; - String name(String name, Meter.Type type); + default String name(String name, Meter.Type type) { + return name(name, type, null); + } + + String name(String name, Meter.Type type, String baseUnit); default String tagKey(String key) { return key; } default String tagValue(String value) { return value; } diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/datadog/DatadogNamingConvention.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/datadog/DatadogNamingConvention.java index f8457fd231..48616a1cce 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/datadog/DatadogNamingConvention.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/datadog/DatadogNamingConvention.java @@ -29,8 +29,8 @@ public class DatadogNamingConvention implements NamingConvention { * all non-alphanumeric characters with '_'. */ @Override - public String name(String name, Meter.Type type) { - String sanitized = NamingConvention.camelCase.name(name, type); + public String name(String name, Meter.Type type, String baseUnit) { + String sanitized = NamingConvention.camelCase.name(name, type, baseUnit); // Metrics that don't start with a letter get dropped on the floor by the Datadog publish API, // so we will prepend them with 'm_'. diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/graphite/GraphiteNamingConvention.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/graphite/GraphiteNamingConvention.java index 6e524f0136..35b33438e1 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/graphite/GraphiteNamingConvention.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/graphite/GraphiteNamingConvention.java @@ -29,7 +29,7 @@ public class GraphiteNamingConvention implements NamingConvention { private static final Pattern blacklistedChars = Pattern.compile("[{}(),=\\[\\]/]"); @Override - public String name(String name, Meter.Type type) { + public String name(String name, Meter.Type type, String baseUnit) { return format(name); } diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/influx/InfluxNamingConvention.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/influx/InfluxNamingConvention.java index 39e0c2b34e..06203e51de 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/influx/InfluxNamingConvention.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/influx/InfluxNamingConvention.java @@ -23,7 +23,7 @@ */ public class InfluxNamingConvention implements NamingConvention { @Override - public String name(String name, Meter.Type type) { + public String name(String name, Meter.Type type, String baseUnit) { return format(name.replace("=", "_")); } diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/prometheus/PrometheusNamingConvention.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/prometheus/PrometheusNamingConvention.java index 5a4529b340..832d99c137 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/prometheus/PrometheusNamingConvention.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/prometheus/PrometheusNamingConvention.java @@ -38,8 +38,8 @@ public class PrometheusNamingConvention implements NamingConvention { * [a-zA-Z_:][a-zA-Z0-9_:]* */ @Override - public String name(String name, Meter.Type type) { - String conventionName = NamingConvention.snakeCase.name(name, type); + public String name(String name, Meter.Type type, String baseUnit) { + String conventionName = NamingConvention.snakeCase.name(name, type, baseUnit); switch (type) { case Counter: @@ -51,6 +51,10 @@ public String name(String name, Meter.Type type) { else if(!conventionName.endsWith("_seconds")) conventionName += "_duration_seconds"; break; + case DistributionSummary: + if(baseUnit != null && !conventionName.endsWith("_" + baseUnit)) + conventionName += "_" + baseUnit; + break; } String sanitized = nameChars.matcher(conventionName).replaceAll("_"); diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/stats/quantile/SlidingWindow.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/stats/quantile/SlidingWindow.java index 53be638132..c7bc2221ab 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/stats/quantile/SlidingWindow.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/stats/quantile/SlidingWindow.java @@ -52,7 +52,7 @@ public SlidingWindow(Integer windowSize) { windowSize *= -1; } this.windowSize = windowSize; - this.elements = new LinkedList>(); + this.elements = new LinkedList<>(); } /** @@ -63,7 +63,7 @@ public SlidingWindow(Integer windowSize) { * @param element */ public void add(T element) { - Element newElement = new Element(element, this.windowSize, 0); + Element newElement = new Element<>(element, this.windowSize, 0); this.elements.addFirst(newElement); } @@ -85,7 +85,7 @@ public void add(T element, Integer size) { throw new RuntimeException("The size of an element can't be a negative integer."); } - Element newElement = new Element(element, this.windowSize - size, size); + Element newElement = new Element<>(element, this.windowSize - size, size); this.elements.addFirst(newElement); } @@ -156,7 +156,7 @@ public T getNewestElement() { * @return a {@link Collection} of element of type T */ public Collection getAll() { - Collection elements = new LinkedList(); + Collection elements = new LinkedList<>(); for (int i = 0; i < this.elements.size(); i++) { elements.add(this.elements.get(i).getElement()); @@ -171,7 +171,7 @@ public Collection getAll() { * @return a {@link Collection} of element of type {@link Integer} */ public Collection getAllLifeTimes() { - Collection lifeTimes = new LinkedList(); + Collection lifeTimes = new LinkedList<>(); for (int i = 0; i < this.elements.size(); i++) { lifeTimes.add(this.elements.get(i).getTimeToLive()); diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/datadog/DatadogNamingConventionTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/datadog/DatadogNamingConventionTest.java index c0909db672..e1df5d56ca 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/datadog/DatadogNamingConventionTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/datadog/DatadogNamingConventionTest.java @@ -25,7 +25,7 @@ class DatadogNamingConventionTest { @Test void nameStartsWithLetter() { - assertThat(convention.name("123", Meter.Type.Gauge)).isEqualTo("m_123"); + assertThat(convention.name("123", Meter.Type.Gauge, null)).isEqualTo("m_123"); } @Test @@ -35,6 +35,6 @@ void tagKeyStartsWithLetter() { @Test void dotNotationIsConvertedToCamelCase() { - assertThat(convention.name("gauge.size", Meter.Type.Gauge)).isEqualTo("gaugeSize"); + assertThat(convention.name("gauge.size", Meter.Type.Gauge, null)).isEqualTo("gaugeSize"); } } diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/prometheus/PrometheusNamingConventionTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/prometheus/PrometheusNamingConventionTest.java index 91c8c64ca7..f55db7ada4 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/prometheus/PrometheusNamingConventionTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/prometheus/PrometheusNamingConventionTest.java @@ -41,6 +41,12 @@ void unitsAreAppendedToTimers() { assertThat(convention.name("timer", Meter.Type.Timer)).isEqualTo("timer_duration_seconds"); } + @Test + void unitsAreAppendedToDistributionSummaries() { + assertThat(convention.name("response.size", Meter.Type.DistributionSummary, "bytes")).isEqualTo("response_size_bytes"); + assertThat(convention.name("summary", Meter.Type.DistributionSummary)).isEqualTo("summary"); + } + @Test void dotNotationIsConvertedToSnakeCase() { assertThat(convention.name("gauge.size", Meter.Type.Gauge)).isEqualTo("gauge_size");