From cd137c26c123fbbb70377eb227a56a67dddcb1b8 Mon Sep 17 00:00:00 2001 From: Sean Heller Date: Fri, 11 Aug 2023 11:02:42 -0400 Subject: [PATCH 1/2] Allow specifying the meterNameConsumer for HighCardinalityTagsDetector --- .../core/instrument/MeterRegistry.java | 18 +++++++++++ .../HighCardinalityTagsDetectorTests.java | 32 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java index 8cf2c91a6d..5afd1056dd 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java @@ -963,6 +963,24 @@ public Config withHighCardinalityTagsDetector(long threshold, Duration delay) { new HighCardinalityTagsDetector(MeterRegistry.this, threshold, delay)); } + /** + * Creates and starts a new {@link HighCardinalityTagsDetector} for this registry. + * @param threshold The threshold to use to detect high cardinality tags (if the + * number of Meters with the same name is higher than this value, that's a high + * cardinality tag). + * @param delay The delay between the termination of one check and the + * commencement of the next. + * @param meterNameConsumer A consumer that will be called for each high + * cardinality tag detected. + * @return This configuration instance. + * @since 1.12.0 + */ + public Config withHighCardinalityTagsDetector(long threshold, Duration delay, + Consumer meterNameConsumer) { + return withHighCardinalityTagsDetector( + new HighCardinalityTagsDetector(MeterRegistry.this, threshold, delay, meterNameConsumer)); + } + private Config withHighCardinalityTagsDetector(HighCardinalityTagsDetector newHighCardinalityTagsDetector) { if (highCardinalityTagsDetector != null) { highCardinalityTagsDetector.close(); diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/HighCardinalityTagsDetectorTests.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/HighCardinalityTagsDetectorTests.java index 97c6b45a1b..3c9e758f41 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/HighCardinalityTagsDetectorTests.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/HighCardinalityTagsDetectorTests.java @@ -90,6 +90,21 @@ void shouldNotDetectNoTags() { assertThat(highCardinalityTagsDetector.findFirst()).isEmpty(); } + @Test + void addsCustomMeterNameConsumer() { + TestCustomMeterNameConsumer customMeterNameConsumer = new TestCustomMeterNameConsumer(); + this.highCardinalityTagsDetector = new HighCardinalityTagsDetector(registry, 3, Duration.ofMinutes(1), + customMeterNameConsumer); + + for (int i = 0; i < 4; i++) { + Counter.builder("test.counter").tag("index", String.valueOf(i)).register(registry).increment(); + } + highCardinalityTagsDetector.start(); + + await().atMost(Duration.ofSeconds(1)) + .until(() -> "test.counter_customized".equals(customMeterNameConsumer.getName())); + } + private static class TestMeterNameConsumer implements Consumer { @Nullable @@ -107,4 +122,21 @@ public String getName() { } + private static class TestCustomMeterNameConsumer implements Consumer { + + @Nullable + private String name; + + @Override + public void accept(String name) { + this.name = name + "_customized"; + } + + @Nullable + public String getName() { + return this.name; + } + + } + } From f437ef1ccca3c602a6200eb1b4874dfc8490bf10 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Tue, 8 Oct 2024 14:35:13 -0700 Subject: [PATCH 2/2] Allow specifying the used HighCardinalityTagsDetector instance --- .../core/instrument/MeterRegistry.java | 24 ++++++------ .../HighCardinalityTagsDetectorTests.java | 39 +++++-------------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java index 5afd1056dd..c4a6db3ef6 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java @@ -964,21 +964,19 @@ public Config withHighCardinalityTagsDetector(long threshold, Duration delay) { } /** - * Creates and starts a new {@link HighCardinalityTagsDetector} for this registry. - * @param threshold The threshold to use to detect high cardinality tags (if the - * number of Meters with the same name is higher than this value, that's a high - * cardinality tag). - * @param delay The delay between the termination of one check and the - * commencement of the next. - * @param meterNameConsumer A consumer that will be called for each high - * cardinality tag detected. + * Uses the supplied {@code Function} + * to create a new {@link HighCardinalityTagsDetector} for this registry. After + * the {@link HighCardinalityTagsDetector} is created, it also starts it. The + * implementation of the factory {@code Function} must pass the registry instance + * to one of the constructors of {@link HighCardinalityTagsDetector}. + * @param highCardinalityTagsDetectorFactory The {@code Function} that creates the + * {@link HighCardinalityTagsDetector} instance * @return This configuration instance. - * @since 1.12.0 + * @since 1.14.0 */ - public Config withHighCardinalityTagsDetector(long threshold, Duration delay, - Consumer meterNameConsumer) { - return withHighCardinalityTagsDetector( - new HighCardinalityTagsDetector(MeterRegistry.this, threshold, delay, meterNameConsumer)); + public Config withHighCardinalityTagsDetector( + Function highCardinalityTagsDetectorFactory) { + return withHighCardinalityTagsDetector(highCardinalityTagsDetectorFactory.apply(MeterRegistry.this)); } private Config withHighCardinalityTagsDetector(HighCardinalityTagsDetector newHighCardinalityTagsDetector) { diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/HighCardinalityTagsDetectorTests.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/HighCardinalityTagsDetectorTests.java index 3c9e758f41..c36b2e7ebe 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/HighCardinalityTagsDetectorTests.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/HighCardinalityTagsDetectorTests.java @@ -15,15 +15,15 @@ */ package io.micrometer.core.instrument; -import java.time.Duration; -import java.util.function.Consumer; - import io.micrometer.common.lang.Nullable; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.time.Duration; +import java.util.function.Consumer; + import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; @@ -91,24 +91,22 @@ void shouldNotDetectNoTags() { } @Test - void addsCustomMeterNameConsumer() { - TestCustomMeterNameConsumer customMeterNameConsumer = new TestCustomMeterNameConsumer(); - this.highCardinalityTagsDetector = new HighCardinalityTagsDetector(registry, 3, Duration.ofMinutes(1), - customMeterNameConsumer); - + void shouldBeManagedThroughMeterRegistry() { for (int i = 0; i < 4; i++) { Counter.builder("test.counter").tag("index", String.valueOf(i)).register(registry).increment(); } - highCardinalityTagsDetector.start(); - await().atMost(Duration.ofSeconds(1)) - .until(() -> "test.counter_customized".equals(customMeterNameConsumer.getName())); + registry.config() + .withHighCardinalityTagsDetector( + r -> new HighCardinalityTagsDetector(r, 3, Duration.ofMinutes(1), testMeterNameConsumer)); + + await().atMost(Duration.ofSeconds(1)).until(() -> "test.counter".equals(testMeterNameConsumer.getName())); } private static class TestMeterNameConsumer implements Consumer { @Nullable - private String name; + private volatile String name; @Override public void accept(String name) { @@ -122,21 +120,4 @@ public String getName() { } - private static class TestCustomMeterNameConsumer implements Consumer { - - @Nullable - private String name; - - @Override - public void accept(String name) { - this.name = name + "_customized"; - } - - @Nullable - public String getName() { - return this.name; - } - - } - }