Skip to content

Enable Gauge builders to take a subclass of Number #5601

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

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 @@ -53,7 +53,7 @@ static <T> Builder<T> builder(String name, @Nullable T obj, ToDoubleFunction<T>
* @since 1.1.0
*/
@Incubating(since = "1.1.0")
static Builder<Supplier<Number>> builder(String name, Supplier<Number> f) {
static <T extends Number> Builder<Supplier<T>> builder(String name, Supplier<T> f) {
return new Builder<>(name, f, f2 -> {
Number val = f2.get();
return val == null ? Double.NaN : val.doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static Row<Number> of(Tags uniqueTags, Number number) {
return new Row<>(uniqueTags, number, Number::doubleValue);
}

public static Row<Supplier<Number>> of(Tags uniqueTags, Supplier<Number> valueFunction) {
public static <T extends Number> Row<Supplier<T>> of(Tags uniqueTags, Supplier<T> valueFunction) {
return new Row<>(uniqueTags, valueFunction, f -> {
Number value = valueFunction.get();
return value == null ? Double.NaN : value.doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static <T> Builder<T> builder(String name, @Nullable T obj, TimeUnit fUnits, ToD
* @since 1.7.0
*/
@Incubating(since = "1.7.0")
static Builder<Supplier<Number>> builder(String name, Supplier<Number> f, TimeUnit fUnits) {
static <T extends Number> Builder<Supplier<T>> builder(String name, Supplier<T> f, TimeUnit fUnits) {
return new Builder<>(name, f, fUnits, f2 -> {
Number val = f2.get();
return val == null ? Double.NaN : val.doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -93,6 +94,14 @@ void rowGaugesHoldStrongReferences() {
assertThat(registry.get("colors").tag("color", "red").gauge().value()).isEqualTo(1);
}

@Test
void rowGaugesCanTakeSubClassOfNumberSuppliers() {
final Supplier<Long> supplier = () -> 1L;
colorGauges.register(Collections.singletonList(Row.of(Tags.of("color", "red"), supplier)));

assertThat(registry.get("colors").tag("color", "red").gauge().value()).isEqualTo(1);
}

@Test
void overwrite() {
testOverwrite();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@
import io.micrometer.core.instrument.step.StepFunctionCounter;
import io.micrometer.core.instrument.step.StepFunctionTimer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.math.BigInteger;
import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -165,6 +170,35 @@ void stringRepresentationOfMetersShouldBeOk() {
sample.stop();
}

@ParameterizedTest
@MethodSource("getSuppliers")
void newGaugeWhenSupplierProvidesSubClassOfNumberShouldReportCorrectly(Supplier<? extends Number> supplier) {
Gauge.builder("temperature", supplier).register(registry);
assertThat(registry.getMeters()).singleElement().satisfies(meter -> {
assertThat(meter.getId().getName()).isEqualTo("temperature");
assertThat(meter.measure()).singleElement()
.satisfies(measurement -> assertThat(measurement.getValue()).isEqualTo(70));
});
}

@ParameterizedTest
@MethodSource("getSuppliers")
void newTimeGaugeWhenSupplierProvidesSubClassOfNumberShouldReportCorrectly(Supplier<? extends Number> supplier) {
TimeGauge.builder("processing.time", supplier, TimeUnit.SECONDS).register(registry);
assertThat(registry.getMeters()).singleElement().satisfies(meter -> {
assertThat(meter.getId().getName()).isEqualTo("processing.time");
assertThat(meter.getId().getBaseUnit()).isEqualTo("seconds");
assertThat(meter.measure()).singleElement()
.satisfies(measurement -> assertThat(measurement.getValue()).isEqualTo(70));
});
}

private static Stream<Supplier<? extends Number>> getSuppliers() {
return Stream.of((Supplier<Integer>) () -> 70, (Supplier<Double>) () -> 70.0, (Supplier<Long>) () -> 70L,
(Supplier<AtomicInteger>) () -> new AtomicInteger(70),
(Supplier<BigInteger>) () -> new BigInteger("70"));
}

private SimpleMeterRegistry createRegistry(CountingMode mode) {
return new SimpleMeterRegistry(new SimpleConfig() {

Expand Down