From 5d042b8e99dfc066d978d5c8e117f17822a03d25 Mon Sep 17 00:00:00 2001 From: Christoph Lorenz Date: Tue, 10 Jul 2018 16:40:18 +0200 Subject: [PATCH] Allow custom key names instead of fixed 'type' key name --- dc-commons-feign/pom.xml | 2 +- dc-commons-jdbi/pom.xml | 2 +- .../pom.xml | 2 +- .../dc-commons-prosemirror-model/pom.xml | 2 +- dc-commons-prosemirror/pom.xml | 2 +- dc-commons-server/pom.xml | 2 +- dc-commons-springaop/pom.xml | 2 +- dc-commons-springboot/pom.xml | 2 +- .../springboot/metrics/MetricsService.java | 38 ++++++++++++------- .../metrics/MetricsServiceTest.java | 9 +++++ dc-commons-springdata/pom.xml | 2 +- dc-commons-springmvc/pom.xml | 2 +- dc-commons-springsecurity/pom.xml | 2 +- dc-commons-validation/pom.xml | 2 +- dc-commons-xml/pom.xml | 2 +- dc-commons-yaml/pom.xml | 2 +- pom.xml | 2 +- 17 files changed, 49 insertions(+), 28 deletions(-) diff --git a/dc-commons-feign/pom.xml b/dc-commons-feign/pom.xml index d2a0c6a8..46f51c60 100644 --- a/dc-commons-feign/pom.xml +++ b/dc-commons-feign/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons REST Feign diff --git a/dc-commons-jdbi/pom.xml b/dc-commons-jdbi/pom.xml index 784bc37c..844cb426 100644 --- a/dc-commons-jdbi/pom.xml +++ b/dc-commons-jdbi/pom.xml @@ -6,7 +6,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons JDBI diff --git a/dc-commons-prosemirror/dc-commons-prosemirror-model-jackson/pom.xml b/dc-commons-prosemirror/dc-commons-prosemirror-model-jackson/pom.xml index a1c4f73c..5cd77d00 100644 --- a/dc-commons-prosemirror/dc-commons-prosemirror-model-jackson/pom.xml +++ b/dc-commons-prosemirror/dc-commons-prosemirror-model-jackson/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons-prosemirror - 1.4.0 + 1.4.1 DigitalCollections: Commons ProseMirror Model Jackson diff --git a/dc-commons-prosemirror/dc-commons-prosemirror-model/pom.xml b/dc-commons-prosemirror/dc-commons-prosemirror-model/pom.xml index 474eaad3..e081e12f 100644 --- a/dc-commons-prosemirror/dc-commons-prosemirror-model/pom.xml +++ b/dc-commons-prosemirror/dc-commons-prosemirror-model/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons-prosemirror - 1.4.0 + 1.4.1 DigitalCollections: Commons ProseMirror Model diff --git a/dc-commons-prosemirror/pom.xml b/dc-commons-prosemirror/pom.xml index 7b62e26b..be94bb0a 100644 --- a/dc-commons-prosemirror/pom.xml +++ b/dc-commons-prosemirror/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons ProseMirror (Parent) diff --git a/dc-commons-server/pom.xml b/dc-commons-server/pom.xml index ecfea061..33ee78f3 100644 --- a/dc-commons-server/pom.xml +++ b/dc-commons-server/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons Server diff --git a/dc-commons-springaop/pom.xml b/dc-commons-springaop/pom.xml index 71057403..4b83b443 100644 --- a/dc-commons-springaop/pom.xml +++ b/dc-commons-springaop/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons Spring AOP diff --git a/dc-commons-springboot/pom.xml b/dc-commons-springboot/pom.xml index d4da9a02..9e55a65c 100644 --- a/dc-commons-springboot/pom.xml +++ b/dc-commons-springboot/pom.xml @@ -6,7 +6,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons Spring Boot (Spring Boot 2) diff --git a/dc-commons-springboot/src/main/java/de/digitalcollections/commons/springboot/metrics/MetricsService.java b/dc-commons-springboot/src/main/java/de/digitalcollections/commons/springboot/metrics/MetricsService.java index cb63d400..66588b99 100644 --- a/dc-commons-springboot/src/main/java/de/digitalcollections/commons/springboot/metrics/MetricsService.java +++ b/dc-commons-springboot/src/main/java/de/digitalcollections/commons/springboot/metrics/MetricsService.java @@ -30,17 +30,29 @@ public MetricsService(MeterRegistry meterRegistry) { * @param value Value of the gauge */ public void setGauge(String name, long value) { - handleCounter(name, null, null, value, null, false); + handleCounter(name, null,null, null, value, null, false); } /** * Sets the value of a gauge * @param name Name of the gauge, postfixed with .amount - * @param tag Name of the tag + * @param tag Name of the tag (key is type) * @param value Value of the gauge */ public void setGauge(String name, String tag, long value) { - handleCounter(name, tag, null, value, null, false); + handleCounter(name, "type", tag, null, value, null, false); + } + + + /** + * Sets the value of a gauge + * @param name Name of the gauge, postfixed with .amount + * @param tagKey Name of the tag + * @param tagValue Value of the tag + * @param value Value of the gauge + */ + public void setGauge(String name, String tagKey, String tagValue, long value) { + handleCounter(name, tagKey, tagValue, null, value, null, false); } /** @@ -49,7 +61,7 @@ public void setGauge(String name, String tag, long value) { * @param tag Name of the tag */ public void increaseCounter(String name, String tag) { - handleCounter(name, tag, 1L, null, null, false); + handleCounter(name, "type", tag, 1L, null, null, false); } /** @@ -59,7 +71,7 @@ public void increaseCounter(String name, String tag) { * @param increment Increment value */ public void increaseCounter(String name, String tag, long increment) { - handleCounter(name, tag, increment, null,null, false); + handleCounter(name, "type", tag, increment, null,null, false); } /** @@ -69,7 +81,7 @@ public void increaseCounter(String name, String tag, long increment) { * @param durationMillis Duration in milliseconds */ public void increaseCounterWithDuration(String name, String tag, Long durationMillis) { - handleCounter(name, tag, 1L, null, durationMillis, false); + handleCounter(name, "type", tag, 1L, null, durationMillis, false); } /** @@ -79,11 +91,11 @@ public void increaseCounterWithDuration(String name, String tag, Long durationMi * @param durationMillis Duration in milliseconds */ public void increaseCounterWithDurationAndPercentiles(String name, String tag, Long durationMillis) { - handleCounter(name, tag, 1L, null, durationMillis, true); + handleCounter(name, "type", tag, 1L, null, durationMillis, true); } - private void handleCounter(String name, String tag, Long increment, Long absoluteValue, Long durationMillis, Boolean publishPercentiles) { - String key = name + ( tag != null ? "." + tag : ""); + private void handleCounter(String name, String tagKey, String tagValue, Long increment, Long absoluteValue, Long durationMillis, Boolean publishPercentiles) { + String key = name + ( ( tagKey != null && tagValue != null ) ? "." + tagValue : ""); if ( increment != null ) { // Increase counter value @@ -95,19 +107,19 @@ private void handleCounter(String name, String tag, Long increment, Long absolut // Register counter, if it doesn't exist yet if (counterTags.get(key) == null) { counterTags.put(key, new HashSet<>()); - if ( tag != null ) { - counterTags.get(key).add(new ImmutableTag("type", tag)); + if ( tagKey != null && tagValue != null ) { + counterTags.get(key).add(new ImmutableTag(tagKey, tagValue)); meterRegistry.gauge(name + ".amount", counterTags.get(key), key, counters::get); } else { meterRegistry.gauge(name + ".amount", counters.get(key)); } } - if (durationMillis != null && tag != null) { + if (durationMillis != null && tagKey != null && tagValue != null) { // Register Timer if (timers.get(key) == null) { Timer.Builder timerBuilder = Timer.builder(name + ".duration") - .tag("type", tag); + .tag(tagKey, tagValue); if ( publishPercentiles ) { timerBuilder = timerBuilder.publishPercentiles(0.5, 0.95) diff --git a/dc-commons-springboot/src/test/java/de/digitalcollections/commons/springboot/metrics/MetricsServiceTest.java b/dc-commons-springboot/src/test/java/de/digitalcollections/commons/springboot/metrics/MetricsServiceTest.java index c5e23ac6..eac339e8 100644 --- a/dc-commons-springboot/src/test/java/de/digitalcollections/commons/springboot/metrics/MetricsServiceTest.java +++ b/dc-commons-springboot/src/test/java/de/digitalcollections/commons/springboot/metrics/MetricsServiceTest.java @@ -83,6 +83,15 @@ void multipleValuesOfAGauge() { assertThat(metricsService.getCounters().get("foo.bar")).isEqualTo(42); } + @Test + @DisplayName("Gauges with different tag keys") + void gaugesWithDifferentTagKeys() { + metricsService.setGauge("foo", "bar", "blub", 2); + assertThat(metricsService.getCounters().get("foo.blub")).isEqualTo(2); + metricsService.setGauge("foo", "two", 42); + assertThat(metricsService.getCounters().get("foo.two")).isEqualTo(42); + } + @Test @DisplayName("Gauges without tag") void gaugeWithoutTag() { diff --git a/dc-commons-springdata/pom.xml b/dc-commons-springdata/pom.xml index 93ebec1b..1aa76b79 100644 --- a/dc-commons-springdata/pom.xml +++ b/dc-commons-springdata/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons Spring Data diff --git a/dc-commons-springmvc/pom.xml b/dc-commons-springmvc/pom.xml index 980c6179..0d72deff 100644 --- a/dc-commons-springmvc/pom.xml +++ b/dc-commons-springmvc/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons Spring MVC diff --git a/dc-commons-springsecurity/pom.xml b/dc-commons-springsecurity/pom.xml index 941f56d2..080b6090 100644 --- a/dc-commons-springsecurity/pom.xml +++ b/dc-commons-springsecurity/pom.xml @@ -6,7 +6,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons Spring Security (Spring Security 5) diff --git a/dc-commons-validation/pom.xml b/dc-commons-validation/pom.xml index e654a50f..8b548127 100644 --- a/dc-commons-validation/pom.xml +++ b/dc-commons-validation/pom.xml @@ -7,7 +7,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons Validation diff --git a/dc-commons-xml/pom.xml b/dc-commons-xml/pom.xml index dab31613..ac1d670a 100644 --- a/dc-commons-xml/pom.xml +++ b/dc-commons-xml/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons XML diff --git a/dc-commons-yaml/pom.xml b/dc-commons-yaml/pom.xml index c67fc506..feb54a2a 100644 --- a/dc-commons-yaml/pom.xml +++ b/dc-commons-yaml/pom.xml @@ -5,7 +5,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 DigitalCollections: Commons YAML diff --git a/pom.xml b/pom.xml index 05500f72..7163aafd 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ de.digitalcollections.commons dc-commons - 1.4.0 + 1.4.1 pom