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

reduce number of scope activations in gRPC client instrumentation #5470

Merged
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,7 +5,6 @@
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.OPERATION_NAME;
import static datadog.trace.instrumentation.grpc.client.GrpcInjectAdapter.SETTER;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand All @@ -16,9 +15,9 @@
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.MethodDescriptor;
import io.grpc.Status;
import io.grpc.StatusException;
import java.util.Collections;
Expand Down Expand Up @@ -48,6 +47,14 @@ 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 @@ -63,10 +70,11 @@ 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 && OPERATION_NAME.equals(span.getOperationName())) {
public static void capture(
@Advice.This io.grpc.ClientCall<?, ?> call,
@Advice.Argument(0) MethodDescriptor<?, ?> method) {
AgentSpan span = DECORATE.startCall(method);
if (null != span) {
InstrumentationContext.get(ClientCall.class, AgentSpan.class).put(call, span);
}
}
Expand Down Expand Up @@ -105,6 +113,43 @@ public static void after(
}
}

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 Cancel {
@Advice.OnMethodEnter
public static void before(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.DECORATE;
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.GRPC_MESSAGE;
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.OPERATION_NAME;
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 @@ -49,9 +52,17 @@ 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 @@ -43,9 +43,7 @@ public String[] knownMatchingTypes() {
"kotlinx.coroutines.scheduling.CoroutineScheduler",
"play.api.libs.streams.Execution$trampoline$",
"scala.concurrent.Future$InternalCallbackExecutor$",
"scala.concurrent.impl.ExecutionContextImpl",
"io.grpc.SynchronizationContext",
"io.grpc.internal.SerializingExecutor"
"scala.concurrent.impl.ExecutionContextImpl"
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ 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",
"io.grpc.internal.SerializingExecutor"
"com.mongodb.internal.connection.DefaultConnectionPool$AsyncWorkManager"
};
}

Expand Down Expand Up @@ -171,8 +170,6 @@ 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