Skip to content

Commit

Permalink
Merge branch '1.12.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
shakuzen committed Dec 19, 2023
2 parents 019f97b + 3eec8ba commit 352433c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
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 @@ -122,8 +123,8 @@ public void start(ThreadFactory threadFactory) {
super.start(threadFactory);

if (config.enabled()) {
this.meterPollingService = Executors.newSingleThreadScheduledExecutor(threadFactory);

this.meterPollingService = Executors.newSingleThreadScheduledExecutor(
new NamedThreadFactory("step-meter-registry-poller-for-" + getClass().getSimpleName()));
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 @@ -567,6 +568,48 @@ void publishOnceWhenClosedWithinFirstStep() {
assertThat(stepMeterRegistry.publishCount.get()).isEqualTo(1);
}

@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");
}

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;
}

}

private class MyStepMeterRegistry extends StepMeterRegistry {

private final AtomicInteger publishCount = new AtomicInteger();
Expand Down

0 comments on commit 352433c

Please sign in to comment.