-
Notifications
You must be signed in to change notification settings - Fork 331
Add Sofa RPC instrumentation (Bolt, H2C, REST, Triple) #11135
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
Open
ValentinZakharov
wants to merge
8
commits into
master
Choose a base branch
from
vzakharov/sofa-rpc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8289a28
Add SOFA RPC instrumentation (Bolt, H2C, Triple protocols)
ValentinZakharov 2711638
Fix SOFA RPC instrumentation: protocol context, server error detectio…
ValentinZakharov 530087e
Add integration tests for SOFA RPC Bolt: happy path, distributed trac…
ValentinZakharov 4c54caf
Split sofarpc into sofarpc-5.0/sofarpc-5.14 modules, fix Triple+gRPC …
ValentinZakharov 90c9aef
Simplify sofarpc instrumentation: merge modules, add REST/Triple serv…
ValentinZakharov ea98882
Fix sofarpc-5.0 CI build: add gradle.lockfile
ValentinZakharov 1f553d7
Raise agent JAR size limit from 32 MB to 33 MB
ValentinZakharov 0555301
Register sofarpc configs in supported-configurations.json
ValentinZakharov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| apply from: "$rootDir/gradle/java.gradle" | ||
35 changes: 35 additions & 0 deletions
35
dd-java-agent/instrumentation/sofarpc/sofarpc-5.0/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| muzzle { | ||
| pass { | ||
| group = "com.alipay.sofa" | ||
| module = "sofa-rpc-all" | ||
| versions = "[5.0.0,)" | ||
| assertInverse = true | ||
| } | ||
| } | ||
|
|
||
| apply from: "$rootDir/gradle/java.gradle" | ||
|
|
||
| addTestSuiteForDir('latestDepTest', 'test') | ||
|
|
||
| configurations.testRuntimeClasspath { | ||
| resolutionStrategy.force "com.google.guava:guava:32.1.3-jre" | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly group: "com.alipay.sofa", name: "sofa-rpc-all", version: "5.6.0" | ||
|
|
||
| testImplementation group: "com.alipay.sofa", name: "sofa-rpc-all", version: "5.14.2" | ||
| // JAX-RS annotations required for the REST protocol test interface (@Path, @GET, etc.) | ||
| testImplementation group: "javax.ws.rs", name: "javax.ws.rs-api", version: "2.1.1" | ||
| // Required so that GrpcServerModule / GrpcClientModule are discovered via ServiceLoader | ||
| // in SofaRpcTripleWithGrpcForkedTest. | ||
| testImplementation project(':dd-java-agent:instrumentation:grpc-1.5') | ||
| testImplementation group: "io.grpc", name: "grpc-netty", version: "1.53.0" | ||
| testImplementation group: "io.grpc", name: "grpc-core", version: "1.53.0" | ||
| testImplementation group: "io.grpc", name: "grpc-stub", version: "1.53.0" | ||
| testImplementation group: "com.google.protobuf", name: "protobuf-java", version: "3.25.3" | ||
| testImplementation group: "com.alibaba", name: "fastjson", version: "1.2.83" | ||
| testImplementation group: "com.google.guava", name: "guava", version: "32.1.3-jre" | ||
|
|
||
| latestDepTestImplementation group: "com.alipay.sofa", name: "sofa-rpc-all", version: "+" | ||
| } |
243 changes: 243 additions & 0 deletions
243
dd-java-agent/instrumentation/sofarpc/sofarpc-5.0/gradle.lockfile
Large diffs are not rendered by default.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
...0/src/main/java/datadog/trace/instrumentation/sofarpc/AbstractClusterInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package datadog.trace.instrumentation.sofarpc; | ||
|
|
||
| import static datadog.context.propagation.Propagators.defaultPropagator; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
| import static datadog.trace.instrumentation.sofarpc.SofaRpcClientDecorator.DECORATE; | ||
| import static datadog.trace.instrumentation.sofarpc.SofaRpcClientDecorator.SOFA_RPC_CLIENT; | ||
| import static datadog.trace.instrumentation.sofarpc.SofaRpcInjectAdapter.SETTER; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import com.alipay.sofa.rpc.client.AbstractCluster; | ||
| import com.alipay.sofa.rpc.config.ConsumerConfig; | ||
| import com.alipay.sofa.rpc.core.request.SofaRequest; | ||
| import com.alipay.sofa.rpc.core.response.SofaResponse; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class AbstractClusterInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "com.alipay.sofa.rpc.client.AbstractCluster"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(named("invoke")) | ||
| .and(takesArguments(1)) | ||
| .and(takesArgument(0, named("com.alipay.sofa.rpc.core.request.SofaRequest"))), | ||
| getClass().getName() + "$InvokeAdvice"); | ||
| } | ||
|
|
||
| public static class InvokeAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static AgentScope enter( | ||
| @Advice.This AbstractCluster self, @Advice.Argument(0) SofaRequest request) { | ||
| ConsumerConfig config = self.getConsumerConfig(); | ||
| String protocol = config != null ? config.getProtocol() : null; | ||
| AgentSpan span = startSpan(SOFA_RPC_CLIENT); | ||
| DECORATE.afterStart(span); | ||
| DECORATE.onRequest(span, request); | ||
| if (protocol != null) { | ||
| span.setTag("sofarpc.protocol", protocol); | ||
| } | ||
| AgentScope scope = activateSpan(span); | ||
| defaultPropagator().inject(span, request, SETTER); | ||
| return scope; | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void exit( | ||
| @Advice.Enter AgentScope scope, | ||
| @Advice.Return SofaResponse response, | ||
| @Advice.Thrown Throwable throwable) { | ||
| if (scope == null) { | ||
| return; | ||
| } | ||
| AgentSpan span = scope.span(); | ||
| DECORATE.onResponse(span, response); | ||
| DECORATE.onError(span, throwable); | ||
| DECORATE.beforeFinish(span); | ||
| scope.close(); | ||
| span.finish(); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
...c/main/java/datadog/trace/instrumentation/sofarpc/BoltServerProcessorInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package datadog.trace.instrumentation.sofarpc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class BoltServerProcessorInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "com.alipay.sofa.rpc.server.bolt.BoltServerProcessor"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(named("handleRequest")) | ||
| .and(takesArguments(3)) | ||
| .and(takesArgument(2, named("com.alipay.sofa.rpc.core.request.SofaRequest"))), | ||
| getClass().getName() + "$HandleRequestAdvice"); | ||
| } | ||
|
|
||
| public static class HandleRequestAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void enter() { | ||
| SofaRpcProtocolContext.set("bolt"); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void exit() { | ||
| SofaRpcProtocolContext.clear(); | ||
| } | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...5.0/src/main/java/datadog/trace/instrumentation/sofarpc/H2cServerTaskInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package datadog.trace.instrumentation.sofarpc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; | ||
|
|
||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class H2cServerTaskInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "com.alipay.sofa.rpc.server.http.AbstractHttpServerTask"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod().and(named("run")).and(takesNoArguments()), getClass().getName() + "$RunAdvice"); | ||
| } | ||
|
|
||
| public static class RunAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void enter() { | ||
| SofaRpcProtocolContext.set("h2c"); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void exit() { | ||
| SofaRpcProtocolContext.clear(); | ||
| } | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
.../main/java/datadog/trace/instrumentation/sofarpc/ProviderProxyInvokerInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package datadog.trace.instrumentation.sofarpc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.extractContextAndGetSpanContext; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
| import static datadog.trace.instrumentation.sofarpc.SofaRpcExtractAdapter.GETTER; | ||
| import static datadog.trace.instrumentation.sofarpc.SofaRpcServerDecorator.DECORATE; | ||
| import static datadog.trace.instrumentation.sofarpc.SofaRpcServerDecorator.SOFA_RPC_SERVER; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import com.alipay.sofa.rpc.core.request.SofaRequest; | ||
| import com.alipay.sofa.rpc.core.response.SofaResponse; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class ProviderProxyInvokerInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "com.alipay.sofa.rpc.server.ProviderProxyInvoker"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(named("invoke")) | ||
| .and(takesArguments(1)) | ||
| .and(takesArgument(0, named("com.alipay.sofa.rpc.core.request.SofaRequest"))), | ||
| getClass().getName() + "$InvokeAdvice"); | ||
| } | ||
|
|
||
| public static class InvokeAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static AgentScope enter(@Advice.Argument(0) SofaRequest request) { | ||
| // Protocol is set in thread-local by transport-specific instrumentation before this call. | ||
| // If null, the transport is not instrumented — skip. | ||
| String protocol = SofaRpcProtocolContext.get(); | ||
| if (protocol == null) { | ||
| return null; | ||
| } | ||
| // For Bolt and H2C the client injects trace context into SofaRequest.requestProps; | ||
| // extract it here. For Triple, parentContext will be null and startSpan() without an | ||
| // explicit parent naturally attaches to the active grpc.server span. For REST, | ||
| // parentContext will also be null and the active netty.request span becomes the parent. | ||
| AgentSpanContext parentContext = extractContextAndGetSpanContext(request, GETTER); | ||
| AgentSpan span = | ||
| parentContext != null | ||
| ? startSpan(SOFA_RPC_SERVER, parentContext) | ||
| : startSpan(SOFA_RPC_SERVER); | ||
| DECORATE.afterStart(span); | ||
| DECORATE.onRequest(span, request); | ||
| span.setTag("sofarpc.protocol", protocol); | ||
| return activateSpan(span); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void exit( | ||
| @Advice.Enter AgentScope scope, | ||
| @Advice.Return SofaResponse response, | ||
| @Advice.Thrown Throwable throwable) { | ||
| if (scope == null) { | ||
| return; | ||
| } | ||
| AgentSpan span = scope.span(); | ||
| DECORATE.onResponse(span, response); | ||
| DECORATE.onError(span, throwable); | ||
| DECORATE.beforeFinish(span); | ||
| scope.close(); | ||
| span.finish(); | ||
| } | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
...src/main/java/datadog/trace/instrumentation/sofarpc/RestServerHandlerInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package datadog.trace.instrumentation.sofarpc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class RestServerHandlerInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "com.alipay.sofa.rpc.server.rest.SofaRestRequestHandler"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod().and(named("channelRead0")).and(takesArguments(2)), | ||
| getClass().getName() + "$ChannelRead0Advice"); | ||
| } | ||
|
|
||
| public static class ChannelRead0Advice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void enter() { | ||
| SofaRpcProtocolContext.set("rest"); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void exit() { | ||
| SofaRpcProtocolContext.clear(); | ||
| } | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
...farpc-5.0/src/main/java/datadog/trace/instrumentation/sofarpc/SofaRpcClientDecorator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package datadog.trace.instrumentation.sofarpc; | ||
|
|
||
| import com.alipay.sofa.rpc.core.request.SofaRequest; | ||
| import com.alipay.sofa.rpc.core.response.SofaResponse; | ||
| import datadog.trace.api.naming.SpanNaming; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.InternalSpanTypes; | ||
| import datadog.trace.bootstrap.instrumentation.api.Tags; | ||
| import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString; | ||
| import datadog.trace.bootstrap.instrumentation.decorator.ClientDecorator; | ||
|
|
||
| public class SofaRpcClientDecorator extends ClientDecorator { | ||
|
|
||
| public static final CharSequence SOFA_RPC_CLIENT = | ||
| UTF8BytesString.create( | ||
| SpanNaming.instance().namingSchema().client().operationForProtocol("sofarpc")); | ||
|
|
||
| private static final CharSequence COMPONENT_NAME = UTF8BytesString.create("sofarpc-client"); | ||
|
|
||
| public static final SofaRpcClientDecorator DECORATE = new SofaRpcClientDecorator(); | ||
|
|
||
| @Override | ||
| protected String[] instrumentationNames() { | ||
| return new String[] {"sofarpc"}; | ||
| } | ||
|
|
||
| @Override | ||
| protected CharSequence component() { | ||
| return COMPONENT_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| protected CharSequence spanType() { | ||
| return InternalSpanTypes.RPC; | ||
| } | ||
|
|
||
| @Override | ||
| protected String service() { | ||
| return null; | ||
| } | ||
|
|
||
| public AgentSpan onRequest(AgentSpan span, SofaRequest request) { | ||
| span.setTag("rpc.system", "sofarpc"); | ||
| if (request == null) { | ||
| return span; | ||
| } | ||
| String serviceName = request.getTargetServiceUniqueName(); | ||
| String methodName = request.getMethodName(); | ||
| span.setTag(Tags.RPC_SERVICE, serviceName); | ||
| span.setTag("rpc.method", methodName); | ||
| // peer.service is derived automatically by PeerServiceCalculator from rpc.service. | ||
| if (serviceName != null && methodName != null) { | ||
| span.setResourceName(serviceName + "/" + methodName); | ||
| } else if (methodName != null) { | ||
| span.setResourceName(methodName); | ||
| } | ||
| return span; | ||
| } | ||
|
|
||
| public AgentSpan onResponse(AgentSpan span, SofaResponse response) { | ||
| if (response != null && response.isError()) { | ||
| span.setError(true); | ||
| span.setTag("error.message", response.getErrorMsg()); | ||
| } | ||
| return span; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note you can delete
dd-java-agent/instrumentation/sofarpc/build.gradleand omit the":dd-java-agent:instrumentation:sofarpc",entry fromsettings.gradleif this parent directory doesn't contain any source / resources.