From b7c02d30e9555504c10e83a56112797343189196 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Tue, 10 Dec 2024 18:36:18 -0800 Subject: [PATCH] remove reflection --- .../v1_3/ArmeriaClientTelemetryBuilder.java | 4 +- .../v1_3/ArmeriaServerTelemetryBuilder.java | 2 + .../armeria/v1_3/internal/Experimental.java | 87 ++++--------------- 3 files changed, 20 insertions(+), 73 deletions(-) diff --git a/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaClientTelemetryBuilder.java b/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaClientTelemetryBuilder.java index 57d6f5ce5d64..7da48e24ed3e 100644 --- a/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaClientTelemetryBuilder.java +++ b/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaClientTelemetryBuilder.java @@ -27,6 +27,8 @@ public final class ArmeriaClientTelemetryBuilder { static { ArmeriaInstrumenterBuilderUtil.setClientBuilderExtractor(builder -> builder.builder); + Experimental.setSetEmitExperimentalClientTelemetry( + (builder, emit) -> builder.builder.setEmitExperimentalHttpClientMetrics(emit)); } ArmeriaClientTelemetryBuilder(OpenTelemetry openTelemetry) { @@ -51,7 +53,7 @@ public ArmeriaClientTelemetryBuilder setStatusExtractor( @CanIgnoreReturnValue public ArmeriaClientTelemetryBuilder addAttributesExtractor( AttributesExtractor attributesExtractor) { - builder.addAttributeExtractor(attributesExtractor); + builder.addAttributesExtractor(attributesExtractor); return this; } diff --git a/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaServerTelemetryBuilder.java b/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaServerTelemetryBuilder.java index 11fca59e7dcd..ca5748c4bbc4 100644 --- a/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaServerTelemetryBuilder.java +++ b/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/ArmeriaServerTelemetryBuilder.java @@ -27,6 +27,8 @@ public final class ArmeriaServerTelemetryBuilder { static { ArmeriaInstrumenterBuilderUtil.setServerBuilderExtractor(builder -> builder.builder); + Experimental.setSetEmitExperimentalServerTelemetry( + (builder, emit) -> builder.builder.setEmitExperimentalHttpServerMetrics(emit)); } ArmeriaServerTelemetryBuilder(OpenTelemetry openTelemetry) { diff --git a/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/internal/Experimental.java b/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/internal/Experimental.java index 606065990bd7..b2bbda010ece 100644 --- a/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/internal/Experimental.java +++ b/instrumentation/armeria/armeria-1.3/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_3/internal/Experimental.java @@ -5,13 +5,9 @@ package io.opentelemetry.instrumentation.armeria.v1_3.internal; -import static java.util.logging.Level.FINE; - import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaClientTelemetryBuilder; import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaServerTelemetryBuilder; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.logging.Logger; +import java.util.function.BiConsumer; import javax.annotation.Nullable; /** @@ -21,88 +17,35 @@ */ public class Experimental { - private static final Logger logger = Logger.getLogger(Experimental.class.getName()); - @Nullable - private static final Method emitExperimentalClientTelemetryMethod = - getEmitExperimentalClientTelemetryMethod(); - - @Nullable private static final Method clientPeerServiceMethod = getClientPeerServiceMethod(); + private static BiConsumer + setEmitExperimentalClientTelemetry; @Nullable - private static final Method emitExperimentalHttpServerMetricsMethod = - getEmitExperimentalHttpServerMetricsMethod(); + private static BiConsumer + setEmitExperimentalServerTelemetry; public void setEmitExperimentalTelemetry( ArmeriaClientTelemetryBuilder builder, boolean emitExperimentalTelemetry) { - - if (emitExperimentalClientTelemetryMethod != null) { - try { - emitExperimentalClientTelemetryMethod.invoke(builder, emitExperimentalTelemetry); - } catch (IllegalAccessException | InvocationTargetException e) { - logger.log(FINE, e.getMessage(), e); - } + if (setEmitExperimentalClientTelemetry != null) { + setEmitExperimentalClientTelemetry.accept(builder, emitExperimentalTelemetry); } } public void setEmitExperimentalTelemetry( ArmeriaServerTelemetryBuilder builder, boolean emitExperimentalTelemetry) { - - if (emitExperimentalHttpServerMetricsMethod != null) { - try { - emitExperimentalHttpServerMetricsMethod.invoke(builder, emitExperimentalTelemetry); - } catch (IllegalAccessException | InvocationTargetException e) { - logger.log(FINE, e.getMessage(), e); - } + if (setEmitExperimentalServerTelemetry != null) { + setEmitExperimentalServerTelemetry.accept(builder, emitExperimentalTelemetry); } } - public void setClientPeerService(ArmeriaClientTelemetryBuilder builder, String peerService) { - - if (clientPeerServiceMethod != null) { - try { - clientPeerServiceMethod.invoke(builder, peerService); - } catch (IllegalAccessException | InvocationTargetException e) { - logger.log(FINE, e.getMessage(), e); - } - } + public static void setSetEmitExperimentalClientTelemetry( + BiConsumer setEmitExperimentalClientTelemetry) { + Experimental.setEmitExperimentalClientTelemetry = setEmitExperimentalClientTelemetry; } - @Nullable - private static Method getEmitExperimentalClientTelemetryMethod() { - try { - Method method = - ArmeriaClientTelemetryBuilder.class.getDeclaredMethod( - "setEmitExperimentalHttpClientMetrics", boolean.class); - method.setAccessible(true); - return method; - } catch (NoSuchMethodException e) { - logger.log(FINE, e.getMessage(), e); - return null; - } - } - - @Nullable - private static Method getClientPeerServiceMethod() { - try { - Method method = - ArmeriaClientTelemetryBuilder.class.getDeclaredMethod("setPeerService", String.class); - method.setAccessible(true); - return method; - } catch (NoSuchMethodException e) { - logger.log(FINE, e.getMessage(), e); - return null; - } - } - - @Nullable - private static Method getEmitExperimentalHttpServerMetricsMethod() { - try { - return ArmeriaServerTelemetryBuilder.class.getMethod( - "setEmitExperimentalHttpServerMetrics", boolean.class); - } catch (NoSuchMethodException e) { - logger.log(FINE, e.getMessage(), e); - return null; - } + public static void setSetEmitExperimentalServerTelemetry( + BiConsumer setEmitExperimentalServerTelemetry) { + Experimental.setEmitExperimentalServerTelemetry = setEmitExperimentalServerTelemetry; } }