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
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions examples/quickstarts/helidon-quickstart-mp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
tjquinno marked this conversation as resolved.
Show resolved Hide resolved
<groupId>io.helidon.microprofile.grpc</groupId>
<artifactId>helidon-microprofile-grpc-metrics</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
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,107 @@
/*
* 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.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}.
*
* <p>
* This implementation
* </p>
*/
public class GrpcMetricAnnotationDiscoveryObserverImpl implements MetricAnnotationDiscoveryObserver {
tjquinno marked this conversation as resolved.
Show resolved Hide resolved

private static GrpcMetricAnnotationDiscoveryObserverImpl instance;
tjquinno marked this conversation as resolved.
Show resolved Hide resolved

static GrpcMetricAnnotationDiscoveryObserverImpl instance() {
tjquinno marked this conversation as resolved.
Show resolved Hide resolved
return instance;
}

private static void instance(GrpcMetricAnnotationDiscoveryObserverImpl value) {
instance = value;
}

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

/**
* Creates a new instance.
*/
public GrpcMetricAnnotationDiscoveryObserverImpl() {
instance(this);
tjquinno marked this conversation as resolved.
Show resolved Hide resolved
}

@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);
}
}
}

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

boolean isDiscovered(Method method) {
return 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,63 @@
/*
* 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.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 GrpcMetricRegistrationObserverImpl implements MetricRegistrationObserver {
tjquinno marked this conversation as resolved.
Show resolved Hide resolved

private static GrpcMetricRegistrationObserverImpl instance;

static GrpcMetricRegistrationObserverImpl instance() {
tjquinno marked this conversation as resolved.
Show resolved Hide resolved
return instance;
}

private static void instance(GrpcMetricRegistrationObserverImpl value) {
instance = value;
}

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

/**
* Creates a new instance of the observer.
*/
public GrpcMetricRegistrationObserverImpl() {
instance(this);
}

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

Metadata metadata(MetricAnnotationDiscovery discovery) {
return metadataByDiscovery.get(discovery);
}
}

This file was deleted.

Loading