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 829fd47 commit de32399
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ public final class RatpackClientTelemetryBuilder {

private final DefaultHttpClientInstrumenterBuilder<RequestSpec, HttpResponse> builder;

static {
Experimental.setSetEmitExperimentalClientTelemetry(
(builder, emit) -> builder.builder.setEmitExperimentalHttpClientMetrics(emit));
}

RatpackClientTelemetryBuilder(OpenTelemetry openTelemetry) {
builder = RatpackClientInstrumenterBuilderFactory.create(INSTRUMENTATION_NAME, openTelemetry);
}

@CanIgnoreReturnValue
public RatpackClientTelemetryBuilder addAttributesExtractor(
AttributesExtractor<? super RequestSpec, ? super HttpResponse> attributesExtractor) {
builder.addAttributeExtractor(attributesExtractor);
builder.addAttributesExtractor(attributesExtractor);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public final class RatpackServerTelemetryBuilder {

private final DefaultHttpServerInstrumenterBuilder<Request, Response> builder;

static {
Experimental.setSetEmitExperimentalServerTelemetry(
(builder, emit) -> builder.builder.setEmitExperimentalHttpServerMetrics(emit));
}

RatpackServerTelemetryBuilder(OpenTelemetry openTelemetry) {
builder = RatpackServerInstrumenterBuilderFactory.create(INSTRUMENTATION_NAME, openTelemetry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@

package io.opentelemetry.instrumentation.ratpack.v1_7.internal;

import static java.util.logging.Level.FINE;

import io.opentelemetry.instrumentation.ratpack.v1_7.RatpackClientTelemetryBuilder;
import io.opentelemetry.instrumentation.ratpack.v1_7.RatpackServerTelemetryBuilder;
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,65 +17,35 @@
*/
public class Experimental {

private static final Logger logger = Logger.getLogger(Experimental.class.getName());

@Nullable
private static final Method emitExperimentalClientTelemetryMethod =
getEmitExperimentalClientTelemetryMethod();
private static BiConsumer<RatpackClientTelemetryBuilder, Boolean>
setEmitExperimentalClientTelemetry;

@Nullable
private static final Method emitExperimentalServerTelemetryMethod =
getEmitExperimentalServerTelemetryMethod();
private static BiConsumer<RatpackServerTelemetryBuilder, Boolean>
setEmitExperimentalServerTelemetry;

public void setEmitExperimentalTelemetry(
RatpackClientTelemetryBuilder 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(
RatpackServerTelemetryBuilder builder, boolean emitExperimentalTelemetry) {

if (emitExperimentalServerTelemetryMethod != null) {
try {
emitExperimentalServerTelemetryMethod.invoke(builder, emitExperimentalTelemetry);
} catch (IllegalAccessException | InvocationTargetException e) {
logger.log(FINE, e.getMessage(), e);
}
if (setEmitExperimentalServerTelemetry != null) {
setEmitExperimentalServerTelemetry.accept(builder, emitExperimentalTelemetry);
}
}

@Nullable
private static Method getEmitExperimentalClientTelemetryMethod() {
try {
Method method =
RatpackClientTelemetryBuilder.class.getDeclaredMethod(
"setEmitExperimentalHttpClientMetrics", boolean.class);
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
logger.log(FINE, e.getMessage(), e);
return null;
}
public static void setSetEmitExperimentalClientTelemetry(
BiConsumer<RatpackClientTelemetryBuilder, Boolean> setEmitExperimentalClientTelemetry) {
Experimental.setEmitExperimentalClientTelemetry = setEmitExperimentalClientTelemetry;
}

@Nullable
private static Method getEmitExperimentalServerTelemetryMethod() {
try {
Method method =
RatpackServerTelemetryBuilder.class.getDeclaredMethod(
"setEmitExperimentalHttpServerMetrics", boolean.class);
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
logger.log(FINE, e.getMessage(), e);
return null;
}
public static void setSetEmitExperimentalServerTelemetry(
BiConsumer<RatpackServerTelemetryBuilder, Boolean> setEmitExperimentalServerTelemetry) {
Experimental.setEmitExperimentalServerTelemetry = setEmitExperimentalServerTelemetry;
}
}

0 comments on commit de32399

Please sign in to comment.