-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feedback, switch to ScheduledExecutorService for registrar
- Loading branch information
Showing
7 changed files
with
219 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...src/main/java/io/opentelemetry/contrib/metrics/micrometer/ScheduledCallbackRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.metrics.micrometer; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* An implementation of {@link CallbackRegistrar} that uses a {@link ScheduledExecutorService} to | ||
* execute the registered callbacks on the specified schedule. | ||
*/ | ||
public final class ScheduledCallbackRegistrar implements CallbackRegistrar { | ||
private final ScheduledExecutorService scheduledExecutorService; | ||
private final long period; | ||
private final TimeUnit timeUnit; | ||
|
||
ScheduledCallbackRegistrar( | ||
ScheduledExecutorService scheduledExecutorService, long period, TimeUnit timeUnit) { | ||
this.scheduledExecutorService = scheduledExecutorService; | ||
this.period = period; | ||
this.timeUnit = timeUnit; | ||
} | ||
|
||
public static ScheduledCallbackRegistrarBuilder builder( | ||
ScheduledExecutorService scheduledExecutorService) { | ||
Objects.requireNonNull(scheduledExecutorService, "scheduledExecutorService"); | ||
return new ScheduledCallbackRegistrarBuilder(scheduledExecutorService); | ||
} | ||
|
||
@Override | ||
public CallbackRegistration registerCallback(Runnable callback) { | ||
if (callback != null) { | ||
ScheduledFuture<?> future = | ||
scheduledExecutorService.scheduleAtFixedRate(callback, period, period, timeUnit); | ||
return () -> future.cancel(false); | ||
} else { | ||
return () -> {}; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
scheduledExecutorService.shutdown(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...n/java/io/opentelemetry/contrib/metrics/micrometer/ScheduledCallbackRegistrarBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.metrics.micrometer; | ||
|
||
import java.time.Duration; | ||
import java.util.Objects; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** Builder utility class for creating instances of {@link ScheduledCallbackRegistrar}. */ | ||
public final class ScheduledCallbackRegistrarBuilder { | ||
private final ScheduledExecutorService scheduledExecutorService; | ||
private long period; | ||
private TimeUnit timeUnit; | ||
|
||
ScheduledCallbackRegistrarBuilder(ScheduledExecutorService scheduledExecutorService) { | ||
this.scheduledExecutorService = scheduledExecutorService; | ||
this.period = 1L; | ||
this.timeUnit = TimeUnit.SECONDS; | ||
} | ||
|
||
/** Sets the period between successive executions of each registered callback */ | ||
public ScheduledCallbackRegistrarBuilder setPeriod(long period, TimeUnit unit) { | ||
Objects.requireNonNull(unit, "unit"); | ||
this.period = period; | ||
this.timeUnit = unit; | ||
return this; | ||
} | ||
|
||
/** Sets the period between successive executions of each registered callback */ | ||
public ScheduledCallbackRegistrarBuilder setPeriod(Duration period) { | ||
Objects.requireNonNull(period, "period"); | ||
this.period = period.toMillis(); | ||
this.timeUnit = TimeUnit.MILLISECONDS; | ||
return this; | ||
} | ||
|
||
/** | ||
* Constructs a new instance of the {@link CallbackRegistrar} based on the builder's values. | ||
* | ||
* @return a new instance. | ||
*/ | ||
public CallbackRegistrar build() { | ||
return new ScheduledCallbackRegistrar(scheduledExecutorService, period, timeUnit); | ||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
...der/src/main/java/io/opentelemetry/contrib/metrics/micrometer/TimerCallbackRegistrar.java
This file was deleted.
Oops, something went wrong.
71 changes: 0 additions & 71 deletions
71
.../main/java/io/opentelemetry/contrib/metrics/micrometer/TimerCallbackRegistrarBuilder.java
This file was deleted.
Oops, something went wrong.
119 changes: 119 additions & 0 deletions
119
...test/java/io/opentelemetry/contrib/metrics/micrometer/ScheduledCallbackRegistrarTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.metrics.micrometer; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ScheduledCallbackRegistrarTest { | ||
|
||
@Mock ScheduledExecutorService scheduledExecutorService; | ||
|
||
@Mock Runnable callback; | ||
|
||
@Mock ScheduledFuture<?> scheduledFuture; | ||
|
||
@Test | ||
void schedulesCallback() { | ||
doReturn(scheduledFuture) | ||
.when(scheduledExecutorService) | ||
.scheduleAtFixedRate(any(), anyLong(), anyLong(), any()); | ||
|
||
try (CallbackRegistrar underTest = | ||
ScheduledCallbackRegistrar.builder(scheduledExecutorService).build()) { | ||
|
||
underTest.registerCallback(callback); | ||
|
||
verify(scheduledExecutorService).scheduleAtFixedRate(callback, 1L, 1L, TimeUnit.SECONDS); | ||
} | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("PreferJavaTimeOverload") | ||
void schedulesCallbackWithPeriod() { | ||
doReturn(scheduledFuture) | ||
.when(scheduledExecutorService) | ||
.scheduleAtFixedRate(any(), anyLong(), anyLong(), any()); | ||
|
||
try (CallbackRegistrar underTest = | ||
ScheduledCallbackRegistrar.builder(scheduledExecutorService) | ||
.setPeriod(10L, TimeUnit.SECONDS) | ||
.build()) { | ||
|
||
underTest.registerCallback(callback); | ||
|
||
verify(scheduledExecutorService).scheduleAtFixedRate(callback, 10L, 10L, TimeUnit.SECONDS); | ||
} | ||
} | ||
|
||
@Test | ||
void schedulesCallbackWithPeriodDuration() { | ||
doReturn(scheduledFuture) | ||
.when(scheduledExecutorService) | ||
.scheduleAtFixedRate(any(), anyLong(), anyLong(), any()); | ||
|
||
try (CallbackRegistrar underTest = | ||
ScheduledCallbackRegistrar.builder(scheduledExecutorService) | ||
.setPeriod(Duration.ofSeconds(10L)) | ||
.build()) { | ||
|
||
underTest.registerCallback(callback); | ||
|
||
verify(scheduledExecutorService) | ||
.scheduleAtFixedRate(callback, 10_000L, 10_000L, TimeUnit.MILLISECONDS); | ||
} | ||
} | ||
|
||
@Test | ||
void handlesNullCallback() { | ||
try (CallbackRegistrar underTest = | ||
ScheduledCallbackRegistrar.builder(scheduledExecutorService).build()) { | ||
|
||
underTest.registerCallback(null); | ||
|
||
verifyNoInteractions(scheduledExecutorService); | ||
} | ||
} | ||
|
||
@Test | ||
void closeCancelsScheduledFuture() { | ||
doReturn(scheduledFuture) | ||
.when(scheduledExecutorService) | ||
.scheduleAtFixedRate(any(), anyLong(), anyLong(), any()); | ||
|
||
try (CallbackRegistrar underTest = | ||
ScheduledCallbackRegistrar.builder(scheduledExecutorService).build()) { | ||
|
||
CallbackRegistration callbackRegistration = underTest.registerCallback(callback); | ||
verify(scheduledExecutorService).scheduleAtFixedRate(callback, 1L, 1L, TimeUnit.SECONDS); | ||
|
||
callbackRegistration.close(); | ||
verify(scheduledFuture).cancel(false); | ||
} | ||
} | ||
|
||
@Test | ||
void closeShutsDownScheduledExecutorService() { | ||
CallbackRegistrar underTest = | ||
ScheduledCallbackRegistrar.builder(scheduledExecutorService).build(); | ||
|
||
underTest.close(); | ||
verify(scheduledExecutorService).shutdown(); | ||
} | ||
} |
Oops, something went wrong.