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

Fix context propagation in gRPC SerializingExecutor #4680

Merged
merged 2 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.propagate;
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.CLIENT_PATHWAY_EDGE_TAGS;
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.DECORATE;
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.GRPC_CLIENT;
import static datadog.trace.instrumentation.grpc.client.GrpcInjectAdapter.SETTER;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

Expand All @@ -16,7 +14,6 @@
import datadog.trace.bootstrap.InstrumentationContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import io.grpc.ClientCall;
import io.grpc.Metadata;
import io.grpc.Status;
Expand Down Expand Up @@ -45,17 +42,8 @@ public String instrumentedType() {

@Override
public void adviceTransformations(AdviceTransformation transformation) {
transformation.applyAdvice(isConstructor(), getClass().getName() + "$Capture");
transformation.applyAdvice(named("start").and(isMethod()), getClass().getName() + "$Start");
transformation.applyAdvice(named("cancel").and(isMethod()), getClass().getName() + "$Cancel");
transformation.applyAdvice(
named("request")
.and(isMethod())
.and(takesArguments(int.class))
.or(isMethod().and(named("halfClose").and(takesArguments(0)))),
getClass().getName() + "$ActivateSpan");
transformation.applyAdvice(
named("sendMessage").and(isMethod()), getClass().getName() + "$SendMessage");
transformation.applyAdvice(
named("closeObserver").and(takesArguments(3)), getClass().getName() + "$CloseObserver");
}
Expand All @@ -69,17 +57,6 @@ public String[] helperClassNames() {
};
}

public static final class Capture {
@Advice.OnMethodExit
public static void capture(@Advice.This io.grpc.ClientCall<?, ?> call) {
AgentSpan span = AgentTracer.activeSpan();
// embed the span in the call only if a grpc.client span is active
if (null != span && GRPC_CLIENT.equals(span.getOperationName())) {
InstrumentationContext.get(ClientCall.class, AgentSpan.class).put(call, span);
}
}
}

public static final class Start {
@Advice.OnMethodEnter
public static AgentScope before(
Expand Down Expand Up @@ -127,43 +104,6 @@ public static void before(
}
}

public static final class ActivateSpan {
@Advice.OnMethodEnter
public static AgentScope before(@Advice.This ClientCall<?, ?> call) {
AgentSpan span = InstrumentationContext.get(ClientCall.class, AgentSpan.class).get(call);
if (null != span) {
return activateSpan(span);
}
return null;
}

@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void after(@Advice.Enter AgentScope scope) {
if (null != scope) {
scope.close();
}
}
}

public static final class SendMessage {
@Advice.OnMethodEnter
public static AgentScope before(@Advice.This ClientCall<?, ?> call) {
// could create a message span here for the request
AgentSpan span = InstrumentationContext.get(ClientCall.class, AgentSpan.class).get(call);
if (span != null) {
return activateSpan(span);
}
return null;
}

@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void after(@Advice.Enter AgentScope scope) {
if (null != scope) {
scope.close();
}
}
}

public static final class CloseObserver {
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void closeObserver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.DECORATE;
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.GRPC_CLIENT;
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.GRPC_MESSAGE;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.InstrumentationContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils;
import datadog.trace.bootstrap.instrumentation.java.concurrent.State;
import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -52,17 +49,9 @@ public Map<String, String> contextStore() {

@Override
public void adviceTransformations(AdviceTransformation transformation) {
transformation.applyAdvice(isConstructor(), getClass().getName() + "$Capture");
transformation.applyAdvice(named("runInContext"), getClass().getName() + "$ReceiveMessages");
}

public static final class Capture {
@Advice.OnMethodExit
public static void capture(@Advice.This Runnable task) {
AdviceUtils.capture(InstrumentationContext.get(Runnable.class, State.class), task, true);
}
}

public static final class ReceiveMessages {
@Advice.OnMethodEnter
public static AgentScope before() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public AbstractExecutorInstrumentation(final String... additionalNames) {
"play.api.libs.streams.Execution$trampoline$",
"scala.concurrent.Future$InternalCallbackExecutor$",
"scala.concurrent.impl.ExecutionContextImpl",
"io.grpc.SynchronizationContext"
"io.grpc.SynchronizationContext",
"io.grpc.internal.SerializingExecutor"
};

final Set<String> executors = new HashSet<>(InstrumenterConfig.get().getTraceExecutors());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public String[] knownMatchingTypes() {
"net.sf.ehcache.store.disk.DiskStorageFactory",
"org.springframework.jms.listener.DefaultMessageListenerContainer",
"org.apache.activemq.broker.TransactionBroker",
"com.mongodb.internal.connection.DefaultConnectionPool$AsyncWorkManager"
"com.mongodb.internal.connection.DefaultConnectionPool$AsyncWorkManager",
"io.grpc.internal.SerializingExecutor"
};
}

Expand Down Expand Up @@ -170,6 +171,8 @@ public void adviceTransformations(AdviceTransformation transformation) {
advice);
transformation.applyAdvice(
isTypeInitializer().and(isDeclaredBy(REACTOR_DISABLED_TYPE_INITIALIZERS)), advice);
transformation.applyAdvice(
named("schedule").and(isDeclaredBy(named("io.grpc.internal.SerializingExecutor"))), advice);
}

public static class DisableAsyncAdvice {
Expand Down