Skip to content

Commit

Permalink
Use different name for StepMeterRegistry thread that polls to roll over
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Jun 26, 2023
1 parent 9415e76 commit 1af154e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.micrometer.core.instrument.internal.DefaultLongTaskTimer;
import io.micrometer.core.instrument.internal.DefaultMeter;
import io.micrometer.core.instrument.push.PushMeterRegistry;
import io.micrometer.core.instrument.util.NamedThreadFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -119,7 +120,12 @@ public void start(ThreadFactory threadFactory) {
super.start(threadFactory);

if (config.enabled()) {
this.meterPollingService = Executors.newSingleThreadScheduledExecutor(threadFactory);
ThreadFactory pollerThreadFactory = threadFactory;
if (threadFactory.getClass() == NamedThreadFactory.class) {
pollerThreadFactory = new NamedThreadFactory(
"step-meter-registry-poller-for-" + getClass().getSimpleName());
}
this.meterPollingService = Executors.newSingleThreadScheduledExecutor(pollerThreadFactory);

this.meterPollingService.scheduleAtFixedRate(this::pollMetersToRollover, getInitialDelay(),
config.step().toMillis(), TimeUnit.MILLISECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.micrometer.common.lang.Nullable;
import io.micrometer.core.Issue;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.util.NamedThreadFactory;
import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.com.google.common.util.concurrent.AtomicDouble;

Expand Down Expand Up @@ -450,6 +451,71 @@ public String get(String key) {
assertThat(publishes.get()).isEqualTo(publishCountBeforeClose);
}

@Test
void startWithNamedThreadFactoryShouldUseNamedThreadFactoryForPoller() {
StepMeterRegistry registry = new CustomStepMeterRegistry();
String publisherThreadName = "custom-metrics-publisher";
registry.start(new NamedThreadFactory(publisherThreadName));
List<String> threadNames = Thread.getAllStackTraces()
.keySet()
.stream()
.map((thread) -> thread.getName())
.collect(Collectors.toList());
assertThat(threadNames).contains(publisherThreadName)
.doesNotContain(publisherThreadName + "-2")
.contains("step-meter-registry-poller-for-CustomStepMeterRegistry");
}

@Test
void startWithCustomThreadFactoryShouldUseCustomThreadFactoryForPoller() {
StepMeterRegistry registry = new CustomStepMeterRegistry();
String publisherThreadName = "custom-metrics-publisher";
registry.start(new CustomThreadFactory(publisherThreadName));
List<String> threadNames = Thread.getAllStackTraces()
.keySet()
.stream()
.map((thread) -> thread.getName())
.collect(Collectors.toList());
assertThat(threadNames).contains(publisherThreadName)
.contains(publisherThreadName + "-2")
.doesNotContain("step-meter-registry-poller-for-CustomStepMeterRegistry");
}

static class CustomStepMeterRegistry extends StepMeterRegistry {

CustomStepMeterRegistry() {
super(new StepRegistryConfig() {
@Override
public String prefix() {
return null;
}

@Override
public String get(String key) {
return null;
}
}, new MockClock());
}

@Override
protected void publish() {
}

@Override
protected TimeUnit getBaseTimeUnit() {
return TimeUnit.SECONDS;
}

}

static class CustomThreadFactory extends NamedThreadFactory {

CustomThreadFactory(String prefix) {
super(prefix);
}

}

private class MyStepMeterRegistry extends StepMeterRegistry {

Deque<Double> publishedCounterCounts = new ArrayDeque<>();
Expand Down

0 comments on commit 1af154e

Please sign in to comment.