Skip to content
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

Address deprecations in MP metrics for 3.x #4500

Merged
merged 9 commits into from
Jul 12, 2022
4 changes: 4 additions & 0 deletions microprofile/grpc/metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>io.helidon.microprofile.grpc</groupId>
<artifactId>helidon-microprofile-grpc-server</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-service-loader</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.metrics</groupId>
<artifactId>helidon-microprofile-metrics</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2022 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.grpc.metrics;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import io.helidon.common.LazyValue;
import io.helidon.microprofile.grpc.core.AnnotatedMethod;
import io.helidon.microprofile.grpc.core.GrpcMethod;
import io.helidon.microprofile.metrics.MetricAnnotationDiscovery;
import io.helidon.microprofile.metrics.spi.MetricAnnotationDiscoveryObserver;

import jakarta.enterprise.inject.spi.configurator.AnnotatedMethodConfigurator;

/**
* The gRPC implementation of {@link io.helidon.microprofile.metrics.spi.MetricAnnotationDiscoveryObserver}.
*/
class GrpcMetricAnnotationDiscoveryObserver implements MetricAnnotationDiscoveryObserver {

private static final LazyValue<GrpcMetricAnnotationDiscoveryObserver> INSTANCE =
LazyValue.create(GrpcMetricAnnotationDiscoveryObserver::new);

private final Map<Method,
Map<Class<? extends Annotation>, MetricAnnotationDiscovery.OfMethod>> discoveriesByMethod =
new HashMap<>();


private GrpcMetricAnnotationDiscoveryObserver() {
}


static GrpcMetricAnnotationDiscoveryObserver instance() {
return INSTANCE.get();
}

@Override
public void onDiscovery(MetricAnnotationDiscovery discovery) {
if (discovery instanceof MetricAnnotationDiscovery.OfMethod methodDiscovery
&& isRpcMethod(methodDiscovery.configurator(), discovery.annotation().annotationType())) {

if (!MetricsConfigurer.isServiceAnnotated(methodDiscovery.annotatedTypeConfigurator().getAnnotated().getJavaClass(),
methodDiscovery.configurator().getAnnotated().getJavaMember(),
discovery.annotation().annotationType())) {
// This endpoint should not give rise to a gRPC-inspired metric.
discovery.deactivate();
} else {
// This endpoint SHOULD give rise to a gRPC-inspired metric.
discovery.disableDefaultInterceptor();
discoveriesByMethod.computeIfAbsent(methodDiscovery.configurator().getAnnotated().getJavaMember(),
key -> new HashMap<>())
.putIfAbsent(discovery.annotation().annotationType(), methodDiscovery);
}
}
}

static Map<Class<? extends Annotation>, MetricAnnotationDiscovery.OfMethod> discoveries(Method method) {
return INSTANCE.get().discoveriesByMethod.get(method);
}

static boolean isDiscovered(Method method) {
return INSTANCE.get().discoveriesByMethod.containsKey(method);
}

/**
* Determine whether a method is annotated with both a metrics annotation
* and an annotation of type {@link io.helidon.microprofile.grpc.core.GrpcMethod}.
*
* @param configurator the {@link jakarta.enterprise.inject.spi.configurator.AnnotatedMethodConfigurator} representing
* the annotated method
*
* @return {@code true} if the method is a timed gRPC method
*/
private static boolean isRpcMethod(AnnotatedMethodConfigurator<?> configurator, Class<? extends Annotation> type) {
AnnotatedMethod method = AnnotatedMethod.create(configurator.getAnnotated().getJavaMember());
GrpcMethod rpcMethod = method.firstAnnotationOrMetaAnnotation(GrpcMethod.class);
if (rpcMethod != null) {
Annotation annotation = method.firstAnnotationOrMetaAnnotation(type);
return annotation != null;
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2022 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.grpc.metrics;

import java.util.HashMap;
import java.util.Map;

import io.helidon.common.LazyValue;
import io.helidon.microprofile.metrics.MetricAnnotationDiscovery;
import io.helidon.microprofile.metrics.spi.MetricRegistrationObserver;

import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.Metric;
import org.eclipse.microprofile.metrics.MetricID;

/**
* The gRPC implementation of {@link io.helidon.microprofile.metrics.spi.MetricRegistrationObserver} with a static factory method.
*/
public class GrpcMetricRegistrationObserver implements MetricRegistrationObserver {

private static final LazyValue<GrpcMetricRegistrationObserver> INSTANCE
= LazyValue.create(GrpcMetricRegistrationObserver::new);

private final Map<MetricAnnotationDiscovery, Metadata> metadataByDiscovery = new HashMap<>();

private GrpcMetricRegistrationObserver() {
}

static GrpcMetricRegistrationObserver instance() {
return INSTANCE.get();
}

@Override
public void onRegistration(MetricAnnotationDiscovery discovery,
Metadata metadata,
MetricID metricId,
Metric metric) {
metadataByDiscovery.put(discovery, metadata);
}

static Metadata metadata(MetricAnnotationDiscovery discovery) {
return INSTANCE.get().metadataByDiscovery.get(discovery);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2022 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,93 +16,24 @@

package io.helidon.microprofile.grpc.metrics;

import java.lang.annotation.Annotation;
import java.util.EnumMap;
import java.util.Map;
import io.helidon.microprofile.metrics.MetricsCdiExtension;

import io.helidon.microprofile.grpc.core.AnnotatedMethod;
import io.helidon.microprofile.grpc.core.Grpc;
import io.helidon.microprofile.grpc.core.GrpcMethod;

import jakarta.annotation.Priority;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
import jakarta.enterprise.inject.spi.WithAnnotations;
import jakarta.enterprise.inject.spi.configurator.AnnotatedMethodConfigurator;
import jakarta.interceptor.Interceptor;
import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.annotation.ConcurrentGauge;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Metered;
import org.eclipse.microprofile.metrics.annotation.SimplyTimed;
import org.eclipse.microprofile.metrics.annotation.Timed;

/**
* A CDI extension for gRPC metrics.
* <p>
* This extension will process annotated types that are gRPC methods and
* ensure that those methods re properly intercepted with a gRPC metrics
* {@link io.grpc.ServerInterceptor}.
* <p>
* If a method is discovered that is annotated with both a metrics annotation and a gRPC
* method type annotation they metrics annotation will be effectively removed from the CDI
* bean so that normal Helidon metrics interceptors do not also intercept that method.
* This extension instantiates and enrolls with the metrics CDI extension a metrics annotation discovery observer and a metrics
* registration observer. It records them for later use by the {@code MetricsConfigurer}.
*/
public class GrpcMetricsCdiExtension
implements Extension {

static final int OBSERVER_PRIORITY = Interceptor.Priority.APPLICATION;

static final EnumMap<MetricType, Class<? extends Annotation>> METRICS_ANNOTATIONS;

static {
Map<MetricType, Class<? extends Annotation>> map = Map.of(
MetricType.CONCURRENT_GAUGE, ConcurrentGauge.class,
MetricType.COUNTER, Counted.class,
MetricType.METERED, Metered.class,
MetricType.SIMPLE_TIMER, SimplyTimed.class,
MetricType.TIMER, Timed.class
);
METRICS_ANNOTATIONS = new EnumMap<>(map);
}


/**
* Observer {@link ProcessAnnotatedType} events and process any method
* annotated with a gRPC method annotation and a metric annotation.
*
* @param pat the {@link ProcessAnnotatedType} to observer
*/
private void registerMetrics(@Observes
@WithAnnotations({Counted.class, Timed.class, Metered.class, ConcurrentGauge.class,
SimplyTimed.class, Grpc.class})
@Priority(OBSERVER_PRIORITY)
ProcessAnnotatedType<?> pat) {
METRICS_ANNOTATIONS.values().forEach(type ->
pat.configureAnnotatedType()
.methods()
.stream()
.filter(method -> isRpcMethod(method, type))
.forEach(method -> method.remove(ann -> type.isAssignableFrom(ann.getClass()))));
}
public class GrpcMetricsCdiExtension implements Extension {

/**
* Determine whether a method is annotated with both a metrics annotation
* and an annotation of type {@link io.helidon.microprofile.grpc.core.GrpcMethod}.
*
* @param configurator the {@link AnnotatedMethodConfigurator} representing
* the annotated method
*
* @return {@code true} if the method is a timed gRPC method
*/
private boolean isRpcMethod(AnnotatedMethodConfigurator<?> configurator, Class<? extends Annotation> type) {
AnnotatedMethod method = AnnotatedMethod.create(configurator.getAnnotated().getJavaMember());
GrpcMethod rpcMethod = method.firstAnnotationOrMetaAnnotation(GrpcMethod.class);
if (rpcMethod != null) {
Annotation annotation = method.firstAnnotationOrMetaAnnotation(type);
return annotation != null;
}
return false;
private void before(@Observes BeforeBeanDiscovery bbd, BeanManager beanManager) {
MetricsCdiExtension metricsCdiExtension = beanManager.getExtension(MetricsCdiExtension.class);
metricsCdiExtension.enroll(GrpcMetricAnnotationDiscoveryObserver.instance());
metricsCdiExtension.enroll(GrpcMetricRegistrationObserver.instance());
}
}
Loading