Skip to content

Commit

Permalink
remove reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Dec 11, 2024
1 parent 1e35fa5 commit b7c02d3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -51,7 +53,7 @@ public ArmeriaClientTelemetryBuilder setStatusExtractor(
@CanIgnoreReturnValue
public ArmeriaClientTelemetryBuilder addAttributesExtractor(
AttributesExtractor<? super ClientRequestContext, ? super RequestLog> attributesExtractor) {
builder.addAttributeExtractor(attributesExtractor);
builder.addAttributesExtractor(attributesExtractor);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<ArmeriaClientTelemetryBuilder, Boolean>
setEmitExperimentalClientTelemetry;

@Nullable
private static final Method emitExperimentalHttpServerMetricsMethod =
getEmitExperimentalHttpServerMetricsMethod();
private static BiConsumer<ArmeriaServerTelemetryBuilder, Boolean>
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<ArmeriaClientTelemetryBuilder, Boolean> 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<ArmeriaServerTelemetryBuilder, Boolean> setEmitExperimentalServerTelemetry) {
Experimental.setEmitExperimentalServerTelemetry = setEmitExperimentalServerTelemetry;
}
}

0 comments on commit b7c02d3

Please sign in to comment.