Skip to content

Commit

Permalink
4.x Allow programmatic look-up of MetricRegistry via CDI without NPE (#…
Browse files Browse the repository at this point in the history
…8210)

* Allow programmatic look-up of MetricRegistry via CDI

Signed-off-by: Tim Quinn <tim.quinn@oracle.com>

* Review comments

---------

Signed-off-by: Tim Quinn <tim.quinn@oracle.com>
  • Loading branch information
tjquinno authored Jan 8, 2024
1 parent afa735d commit 1899ca1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 1899ca1

Please sign in to comment.