Skip to content

Commit

Permalink
Changes to metrics to work with MP metrics 2.2 (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjquinno authored Oct 29, 2019
1 parent da1c117 commit 759e1bf
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 43 deletions.
2 changes: 1 addition & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<version.lib.microprofile-health>2.1</version.lib.microprofile-health>
<version.lib.microprofile-jwt>1.1.1</version.lib.microprofile-jwt>
<version.lib.microprofile-metrics1-api>1.1</version.lib.microprofile-metrics1-api>
<version.lib.microprofile-metrics2-api>2.1.0</version.lib.microprofile-metrics2-api>
<version.lib.microprofile-metrics2-api>2.2</version.lib.microprofile-metrics2-api>
<version.lib.microprofile-openapi-api>1.1.2</version.lib.microprofile-openapi-api>
<version.lib.microprofile-fault-tolerance-api>2.0</version.lib.microprofile-fault-tolerance-api>
<version.lib.microprofile-tracing>1.3.1</version.lib.microprofile-tracing>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
/**
* Gauge implementation.
*/
final class HelidonGauge<T extends Number> extends MetricImpl implements Gauge<T> {
final class HelidonGauge<T /* extends Number */> extends MetricImpl implements Gauge<T> {
// TODO uncomment above once MP metrics enforces the Number restriction
private final Supplier<T> value;

private HelidonGauge(String registryType, Metadata metadata, Gauge<T> metric) {
Expand All @@ -45,8 +46,9 @@ private HelidonGauge(String registryType, Metadata metadata, Gauge<T> metric) {
value = metric::getValue;
}

static <S extends Number> HelidonGauge<S> create(String registryType, Metadata metadata,
static <S /* extends Number */> HelidonGauge<S> create(String registryType, Metadata metadata,
Gauge<S> metric) {
// TODO uncomment above once MP metrics enforces the Number restriction
return new HelidonGauge<>(registryType, metadata, metric);
}

Expand All @@ -67,40 +69,50 @@ public String prometheusValue() {

@Override
public void jsonData(JsonObjectBuilder builder, MetricID metricID) {
T value = getValue();
// TODO uncomment 'value' declaration below and remove 'untypedValue' once MP metrics enforces restriction
// T value = getValue();
T untypedValue = getValue();
String nameWithTags = jsonFullKey(metricID);

if (value instanceof AtomicInteger) {
builder.add(nameWithTags, value.doubleValue());
} else if (value instanceof AtomicLong) {
builder.add(nameWithTags, value.longValue());
} else if (value instanceof BigDecimal) {
builder.add(nameWithTags, (BigDecimal) value);
} else if (value instanceof BigInteger) {
builder.add(nameWithTags, (BigInteger) value);
} else if (value instanceof Byte) {
builder.add(nameWithTags, value.intValue());
} else if (value instanceof Double) {
builder.add(nameWithTags, (Double) value);
} else if (value instanceof DoubleAccumulator) {
builder.add(nameWithTags, value.doubleValue());
} else if (value instanceof DoubleAdder) {
builder.add(nameWithTags, value.doubleValue());
} else if (value instanceof Float) {
builder.add(nameWithTags, value.floatValue());
} else if (value instanceof Integer) {
builder.add(nameWithTags, (Integer) value);
} else if (value instanceof Long) {
builder.add(nameWithTags, (Long) value);
} else if (value instanceof LongAccumulator) {
builder.add(nameWithTags, value.longValue());
} else if (value instanceof LongAdder) {
builder.add(nameWithTags, value.longValue());
} else if (value instanceof Short) {
builder.add(nameWithTags, value.intValue());
// TODO remove following 'if' and 'value' assignment once MP metrics enforces restriction,
// promoting the nested 'if' one level.
if (untypedValue instanceof Number) {
Number value = (Number) untypedValue;
if (value instanceof AtomicInteger) {
builder.add(nameWithTags, value.doubleValue());
} else if (value instanceof AtomicLong) {
builder.add(nameWithTags, value.longValue());
} else if (value instanceof BigDecimal) {
builder.add(nameWithTags, (BigDecimal) value);
} else if (value instanceof BigInteger) {
builder.add(nameWithTags, (BigInteger) value);
} else if (value instanceof Byte) {
builder.add(nameWithTags, value.intValue());
} else if (value instanceof Double) {
builder.add(nameWithTags, (Double) value);
} else if (value instanceof DoubleAccumulator) {
builder.add(nameWithTags, value.doubleValue());
} else if (value instanceof DoubleAdder) {
builder.add(nameWithTags, value.doubleValue());
} else if (value instanceof Float) {
builder.add(nameWithTags, value.floatValue());
} else if (value instanceof Integer) {
builder.add(nameWithTags, (Integer) value);
} else if (value instanceof Long) {
builder.add(nameWithTags, (Long) value);
} else if (value instanceof LongAccumulator) {
builder.add(nameWithTags, value.longValue());
} else if (value instanceof LongAdder) {
builder.add(nameWithTags, value.longValue());
} else if (value instanceof Short) {
builder.add(nameWithTags, value.intValue());
} else {
// Might be a developer-provided class which extends Number.
builder.add(nameWithTags, value.doubleValue());
}
// TODO remove following 'else' and 'builder.add' once MP metrics enforces restriction
} else {
// Might be a developer-provided class which extends Number.
builder.add(nameWithTags, value.doubleValue());
builder.add(nameWithTags, String.valueOf(value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ static Metadata toMetadata(io.helidon.common.metrics.InternalBridge.Metadata met
final MetadataBuilder builder = new MetadataBuilder();
builder.withName(metadata.getName())
.withDisplayName(metadata.getDisplayName())
.withType(metadata.getTypeRaw())
.reusable(metadata.isReusable());

metadata.getDescription().ifPresent(builder::withDescription);
metadata.getUnit().ifPresent(builder::withUnit);
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
*
* @param <T> data type reported by the underlying {@code Gauge}
*/
class DelegatingGauge<T extends Number> implements Gauge<T> {
class DelegatingGauge<T /* extends Number */> implements Gauge<T> {
// TODO uncomment preceding clause once MP metrics enforces restriction

private final Method method;
private final Object obj;
Expand All @@ -50,8 +51,9 @@ private DelegatingGauge(Method method, Object obj, Class<T> clazz) {
* @param clazz type of the underlying gauge
* @return {@code DelegatingGauge}
*/
public static <S extends Number> DelegatingGauge<S> newInstance(Method method, Object obj,
public static <S /* extends Number */> DelegatingGauge<S> newInstance(Method method, Object obj,
Class<S> clazz) {
// TODO uncomment preceding clause once MP metrics enforces restriction
return new DelegatingGauge<>(method, obj, clazz);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ private ConcurrentGauge produceConcurrentGauge(MetricRegistry registry, Injectio
@Produces
@VendorDefined
@SuppressWarnings("unchecked")
private <T extends Number> Gauge<T> produceGauge(MetricRegistry registry, InjectionPoint ip) {
private <T /* extends Number */> Gauge<T> produceGauge(MetricRegistry registry, InjectionPoint ip) {
// TODO uncomment preceding clause once MP metrics enforces restriction
Metric metric = ip.getAnnotated().getAnnotation(Metric.class);
return (Gauge<T>) registry.getGauges().entrySet().stream()
.filter(entry -> entry.getKey().getName().equals(metric.name()))
Expand All @@ -207,8 +208,9 @@ private <T extends Number> Gauge<T> produceGauge(MetricRegistry registry, Inject
* @return requested gauge
*/
@Produces
private <T extends Number> Gauge<T> produceGaugeDefault(MetricRegistry registry,
private <T /* extends Number */> Gauge<T> produceGaugeDefault(MetricRegistry registry,
InjectionPoint ip) {
// TODO uncomment preceding clause once MP metrics enforces restrictions
return produceGauge(registry, ip);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ private void registerAnnotatedGauges(@Observes AfterDeploymentValidation adv, Be
MetricID gaugeID = gaugeSite.getKey();

AnnotatedMethodConfigurator<?> site = gaugeSite.getValue();
DelegatingGauge<? extends Number> dg;
// TODO uncomment following clause once MP metrics enforces restriction
DelegatingGauge<? /* extends Number */> dg;
try {
dg = buildDelegatingGauge(gaugeID.getName(), site,
bm);
Expand All @@ -417,20 +418,23 @@ private void registerAnnotatedGauges(@Observes AfterDeploymentValidation adv, Be
annotatedGaugeSites.clear();
}

private DelegatingGauge<? extends Number> buildDelegatingGauge(String gaugeName,
private DelegatingGauge<? /* extends Number */> buildDelegatingGauge(String gaugeName,
AnnotatedMethodConfigurator<?> site, BeanManager bm) {
// TODO uncomment preceding clause once MP metrics enforces restriction
Bean<?> bean = bm.getBeans(site.getAnnotated().getJavaMember().getDeclaringClass())
.stream()
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Cannot find bean for annotated gauge " + gaugeName));

Class<?> returnType = site.getAnnotated().getJavaMember().getReturnType();
Class<? extends Number> narrowedReturnType = typeToNumber(returnType);
// TODO uncomment following line once MP metrics enforces restriction
// Class<? extends Number> narrowedReturnType = typeToNumber(returnType);

return DelegatingGauge.newInstance(
site.getAnnotated().getJavaMember(),
getReference(bm, bean.getBeanClass(), bean),
narrowedReturnType);
// TODO use narrowedReturnType instead of returnType below once MP metrics enforces restriction
returnType);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

public class BadGaugeTest {

// TODO - remove following Disabled line once MP metrics enforces restriction
@org.junit.jupiter.api.Disabled
@Test
public void testBadBean() {
SeContainerInitializer initializer = SeContainerInitializer.newInstance();
Expand Down

0 comments on commit 759e1bf

Please sign in to comment.