Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying the meterNameConsumer for HighCardinalityTagsDetector #4028

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,22 @@ public Config withHighCardinalityTagsDetector(long threshold, Duration delay) {
new HighCardinalityTagsDetector(MeterRegistry.this, threshold, delay));
}

/**
* 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.14.0
*/
public Config withHighCardinalityTagsDetector(
Function<MeterRegistry, HighCardinalityTagsDetector> highCardinalityTagsDetectorFactory) {
return withHighCardinalityTagsDetector(highCardinalityTagsDetectorFactory.apply(MeterRegistry.this));
}

private Config withHighCardinalityTagsDetector(HighCardinalityTagsDetector newHighCardinalityTagsDetector) {
if (highCardinalityTagsDetector != null) {
highCardinalityTagsDetector.close();
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 @@ -90,10 +90,23 @@ void shouldNotDetectNoTags() {
assertThat(highCardinalityTagsDetector.findFirst()).isEmpty();
}

@Test
void shouldBeManagedThroughMeterRegistry() {
for (int i = 0; i < 4; i++) {
Counter.builder("test.counter").tag("index", String.valueOf(i)).register(registry).increment();
}

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 Down