Skip to content

Commit

Permalink
(bug) apply exec controller classes using instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrengelman committed Nov 7, 2024
1 parent b70e529 commit cf59135
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.common.collect.ImmutableList;
import io.opentelemetry.instrumentation.ratpack.v1_7.OpenTelemetryExecInitializer;
import io.opentelemetry.instrumentation.ratpack.v1_7.OpenTelemetryExecInterceptor;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import ratpack.exec.ExecInitializer;
import ratpack.exec.ExecInterceptor;

public class DefaultExecControllerInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("ratpack.exec.internal.DefaultExecController");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
named("setInitializers")
.and(takesArgument(0, named("com.google.common.collect.ImmutableList"))),
DefaultExecControllerInstrumentation.class.getName() + "$SetInitializersAdvice");

transformer.applyAdviceToMethod(
named("setInterceptors")
.and(takesArgument(0, named("com.google.common.collect.ImmutableList"))),
DefaultExecControllerInstrumentation.class.getName() + "$SetInterceptorsAdvice");

transformer.applyAdviceToMethod(
isConstructor(),
DefaultExecControllerInstrumentation.class.getName() + "$ConstructorAdvice");
}

public static class SetInitializersAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void enter(
@Advice.Argument(value = 0, readOnly = false)
ImmutableList<? extends ExecInitializer> initializers) {
initializers =
ImmutableList.<ExecInitializer>builder()
.addAll(initializers)
.add(OpenTelemetryExecInitializer.INSTANCE)
.build();
}
}

public static class SetInterceptorsAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void enter(
@Advice.Argument(value = 0, readOnly = false)
ImmutableList<? extends ExecInterceptor> interceptors) {
interceptors =
ImmutableList.<ExecInterceptor>builder()
.addAll(interceptors)
.add(OpenTelemetryExecInterceptor.INSTANCE)
.build();
}
}

public static class ConstructorAdvice {

@SuppressWarnings("UnusedVariable")
@Advice.OnMethodExit(suppress = Throwable.class)
public static void exit(
@Advice.FieldValue(value = "initializers", readOnly = false)
ImmutableList<? extends ExecInitializer> initializers,
@Advice.FieldValue(value = "interceptors", readOnly = false)
ImmutableList<? extends ExecInterceptor> interceptors) {
initializers = ImmutableList.of(OpenTelemetryExecInitializer.INSTANCE);
interceptors = ImmutableList.of(OpenTelemetryExecInterceptor.INSTANCE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
@Override
public List<TypeInstrumentation> typeInstrumentations() {
return asList(
new DefaultExecControllerInstrumentation(),
new ServerRegistryInstrumentation(),
new HttpClientInstrumentation(),
new RequestActionSupportInstrumentation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import ratpack.handling.HandlerDecorator;
import ratpack.registry.Registry;

public class ServerRegistryInstrumentation implements TypeInstrumentation {
Expand All @@ -34,9 +35,12 @@ public void transform(TypeTransformer transformer) {
public static class BuildAdvice {

@Advice.OnMethodExit(suppress = Throwable.class)
public static void injectTracing(@Advice.Return(readOnly = false) Registry registry)
throws Exception {
registry = registry.join(Registry.of(RatpackSingletons.telemetry()::configureServerRegistry));
public static void injectTracing(@Advice.Return(readOnly = false) Registry registry) {
registry =
registry.join(
Registry.single(
HandlerDecorator.prepend(
RatpackSingletons.telemetry().getOpenTelemetryServerHandler())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,25 @@

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

import com.google.common.collect.ImmutableList;
import io.netty.channel.ConnectTimeoutException;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.ratpack.client.AbstractRatpackForkedHttpClientTest;
import io.opentelemetry.instrumentation.ratpack.v1_7.OpenTelemetryExecInitializer;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import io.opentelemetry.semconv.NetworkAttributes;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.RegisterExtension;
import ratpack.exec.internal.DefaultExecController;
import ratpack.http.client.HttpClientReadTimeoutException;

class RatpackForkedHttpClientTest extends AbstractRatpackForkedHttpClientTest {

@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();

@BeforeAll
@Override
protected void setUpClient() throws Exception {
exec.run(
unused -> {
((DefaultExecController) exec.getController())
.setInitializers(ImmutableList.of(OpenTelemetryExecInitializer.INSTANCE));
client = buildHttpClient();
singleConnectionClient = buildHttpClient(spec -> spec.poolSize(1));
});
}

@Override
protected Set<AttributeKey<?>> computeHttpAttributes(URI uri) {
Set<AttributeKey<?>> attributes = new HashSet<>(super.computeHttpAttributes(uri));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,25 @@

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

import com.google.common.collect.ImmutableList;
import io.netty.channel.ConnectTimeoutException;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.ratpack.client.AbstractRatpackHttpClientTest;
import io.opentelemetry.instrumentation.ratpack.v1_7.OpenTelemetryExecInitializer;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import io.opentelemetry.semconv.NetworkAttributes;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.RegisterExtension;
import ratpack.exec.internal.DefaultExecController;
import ratpack.http.client.HttpClientReadTimeoutException;

class RatpackHttpClientTest extends AbstractRatpackHttpClientTest {

@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();

@BeforeAll
@Override
protected void setUpClient() throws Exception {
exec.run(
unused -> {
((DefaultExecController) exec.getController())
.setInitializers(ImmutableList.of(OpenTelemetryExecInitializer.INSTANCE));
client = buildHttpClient();
singleConnectionClient = buildHttpClient(spec -> spec.poolSize(1));
});
}

@Override
protected Set<AttributeKey<?>> computeHttpAttributes(URI uri) {
Set<AttributeKey<?>> attributes = new HashSet<>(super.computeHttpAttributes(uri));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,25 @@

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

import com.google.common.collect.ImmutableList;
import io.netty.channel.ConnectTimeoutException;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.ratpack.client.AbstractRatpackPooledHttpClientTest;
import io.opentelemetry.instrumentation.ratpack.v1_7.OpenTelemetryExecInitializer;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import io.opentelemetry.semconv.NetworkAttributes;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.RegisterExtension;
import ratpack.exec.internal.DefaultExecController;
import ratpack.http.client.HttpClientReadTimeoutException;

class RatpackPooledHttpClientTest extends AbstractRatpackPooledHttpClientTest {

@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();

@BeforeAll
@Override
protected void setUpClient() throws Exception {
exec.run(
unused -> {
((DefaultExecController) exec.getController())
.setInitializers(ImmutableList.of(OpenTelemetryExecInitializer.INSTANCE));
client = buildHttpClient();
singleConnectionClient = buildHttpClient(spec -> spec.poolSize(1));
});
}

@Override
protected Set<AttributeKey<?>> computeHttpAttributes(URI uri) {
Set<AttributeKey<?>> attributes = new HashSet<>(super.computeHttpAttributes(uri));
Expand Down

0 comments on commit cf59135

Please sign in to comment.