-
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.
Switch UpDownCounters to Micrometer Gauge, memoize MeterRegistry supp…
…lier, randomize measurements during tests
- Loading branch information
Showing
21 changed files
with
608 additions
and
200 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
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
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
44 changes: 44 additions & 0 deletions
44
...src/main/java/io/opentelemetry/contrib/metrics/micrometer/internal/MemoizingSupplier.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,44 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.metrics.micrometer.internal; | ||
|
||
import java.util.function.Supplier; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Delegating implementation of {@link Supplier Supplier<T>} that ensures that the {@link | ||
* Supplier#get()} method is called at most once and the result is memoized for subsequent | ||
* invocations. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public final class MemoizingSupplier<T> implements Supplier<T> { | ||
private final Supplier<T> delegate; | ||
private volatile boolean initialized; | ||
@Nullable private volatile T cachedResult; | ||
|
||
public MemoizingSupplier(Supplier<T> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("NullAway") | ||
public T get() { | ||
T result = cachedResult; | ||
if (!initialized) { | ||
synchronized (this) { | ||
if (!initialized) { | ||
result = delegate.get(); | ||
cachedResult = result; | ||
initialized = true; | ||
return result; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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
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
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
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
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
40 changes: 40 additions & 0 deletions
40
...test/java/io/opentelemetry/contrib/metrics/micrometer/internal/MemoizingSupplierTest.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,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.metrics.micrometer.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.function.Supplier; | ||
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 MemoizingSupplierTest { | ||
|
||
@Mock Supplier<String> supplier; | ||
|
||
@Test | ||
void callsGetOnlyOnce() { | ||
when(supplier.get()).thenReturn("RESULT"); | ||
|
||
Supplier<String> underTest = new MemoizingSupplier<>(supplier); | ||
|
||
verifyNoInteractions(supplier); | ||
|
||
assertThat(underTest.get()).isEqualTo("RESULT"); | ||
verify(supplier, times(1)).get(); | ||
|
||
assertThat(underTest.get()).isEqualTo("RESULT"); | ||
verifyNoMoreInteractions(supplier); | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
...a/io/opentelemetry/contrib/metrics/micrometer/internal/instruments/DoubleMeasurement.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
...ava/io/opentelemetry/contrib/metrics/micrometer/internal/instruments/LongMeasurement.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.