Skip to content

Commit

Permalink
Allow specifying the used HighCardinalityTagsDetector instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Oct 8, 2024
1 parent cd137c2 commit f437ef1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeterRegistry, HighCardinalityTagsDetector>}
* 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<String> meterNameConsumer) {
return withHighCardinalityTagsDetector(
new HighCardinalityTagsDetector(MeterRegistry.this, threshold, delay, meterNameConsumer));
public Config withHighCardinalityTagsDetector(
Function<MeterRegistry, HighCardinalityTagsDetector> highCardinalityTagsDetectorFactory) {
return withHighCardinalityTagsDetector(highCardinalityTagsDetectorFactory.apply(MeterRegistry.this));
}

private Config withHighCardinalityTagsDetector(HighCardinalityTagsDetector newHighCardinalityTagsDetector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> {

@Nullable
private String name;
private volatile String name;

@Override
public void accept(String name) {
Expand All @@ -122,21 +120,4 @@ public String getName() {

}

private static class TestCustomMeterNameConsumer implements Consumer<String> {

@Nullable
private String name;

@Override
public void accept(String name) {
this.name = name + "_customized";
}

@Nullable
public String getName() {
return this.name;
}

}

}

0 comments on commit f437ef1

Please sign in to comment.