From 1899ca13b460c19895eb69fca4cf435191b42d52 Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Mon, 8 Jan 2024 15:05:39 -0600 Subject: [PATCH] 4.x Allow programmatic look-up of MetricRegistry via CDI without NPE (#8210) * Allow programmatic look-up of MetricRegistry via CDI Signed-off-by: Tim Quinn * Review comments --------- Signed-off-by: Tim Quinn --- .../helidon/microprofile/metrics/RegistryProducer.java | 6 ++++-- .../io/helidon/microprofile/metrics/ProducerTest.java | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/microprofile/metrics/src/main/java/io/helidon/microprofile/metrics/RegistryProducer.java b/microprofile/metrics/src/main/java/io/helidon/microprofile/metrics/RegistryProducer.java index 83a04dbe97c..b69c8f19f21 100644 --- a/microprofile/metrics/src/main/java/io/helidon/microprofile/metrics/RegistryProducer.java +++ b/microprofile/metrics/src/main/java/io/helidon/microprofile/metrics/RegistryProducer.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2018, 2023 Oracle and/or its affiliates. + * Copyright (c) 2018, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Default; import jakarta.enterprise.inject.Produces; +import jakarta.enterprise.inject.spi.Annotated; import jakarta.enterprise.inject.spi.InjectionPoint; import org.eclipse.microprofile.metrics.MetricRegistry; import org.eclipse.microprofile.metrics.MetricRegistry.Type; @@ -40,7 +41,8 @@ private RegistryProducer() { @Produces @Default public static org.eclipse.microprofile.metrics.MetricRegistry getScopedRegistry(InjectionPoint injectionPoint) { - RegistryScope scope = injectionPoint.getAnnotated().getAnnotation(RegistryScope.class); + Annotated annotated = (injectionPoint == null) ? null : injectionPoint.getAnnotated(); + RegistryScope scope = (annotated == null) ? null : annotated.getAnnotation(RegistryScope.class); return scope == null ? getApplicationRegistry() : RegistryFactory.getInstance().getRegistry(scope.scope()); diff --git a/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/ProducerTest.java b/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/ProducerTest.java index 092c10e65c2..78bf0630327 100644 --- a/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/ProducerTest.java +++ b/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/ProducerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2023 Oracle and/or its affiliates. + * Copyright (c) 2018, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package io.helidon.microprofile.metrics; +import jakarta.enterprise.inject.spi.CDI; import jakarta.inject.Inject; import org.eclipse.microprofile.metrics.Counter; import org.eclipse.microprofile.metrics.MetricID; @@ -89,4 +90,10 @@ void testRegistryFactoryProducer() { MetricRegistry customRegistry = registryFactory.getRegistry("myCustomScope"); assertThat("Custom scoped MetricRegistry", customRegistry, notNullValue()); } + + @Test + void testDirectMetricRegistryLookup() { + MetricRegistry appRegistry = CDI.current().select(MetricRegistry.class).get(); + assertThat("Directly-looked-up app registry", appRegistry, notNullValue()); + } }