Skip to content

Commit 17dca46

Browse files
Fix schema tracking for nested messages (#7690)
* fix schema tracking bug with recursive messages * add generate script * fmt * add task for generating proto files * remove old gradle
1 parent d4ec19e commit 17dca46

File tree

11 files changed

+98
-1615
lines changed

11 files changed

+98
-1615
lines changed

dd-java-agent/instrumentation/protobuf/build.gradle

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
plugins {
2+
id "com.google.protobuf" version "0.9.4"
3+
}
4+
5+
apply from: "$rootDir/gradle/java.gradle"
6+
17
muzzle {
28
pass {
39
group = "com.google.protobuf"
@@ -7,9 +13,30 @@ muzzle {
713
}
814
}
915

10-
apply from: "$rootDir/gradle/java.gradle"
16+
protobuf {
17+
protoc {
18+
def os = System.getProperty("os.name").toLowerCase()
19+
def arch = System.getProperty("os.arch").toLowerCase()
20+
21+
// There is no m1 support for protoc 3.0.0, so require Rosetta
22+
if (os.contains("mac") && arch.contains("aarch64")) {
23+
artifact = "com.google.protobuf:protoc:3.0.0:osx-x86_64"
24+
} else {
25+
artifact = "com.google.protobuf:protoc:3.0.0"
26+
}
27+
}
28+
}
29+
1130

1231
dependencies {
1332
compileOnly group: 'com.google.protobuf', name: 'protobuf-java', version: '3.0.0'
1433
testImplementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.0.0'
1534
}
35+
36+
sourceSets {
37+
test {
38+
java {
39+
srcDir "$buildDir/generated/source/proto/test/java"
40+
}
41+
}
42+
}

dd-java-agent/instrumentation/protobuf/src/main/java/datadog/trace/instrumentation/protobuf_java/AbstractMessageInstrumentation.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.google.protobuf.AbstractMessage;
1111
import datadog.trace.agent.tooling.Instrumenter;
1212
import datadog.trace.agent.tooling.InstrumenterModule;
13+
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
1314
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1415
import java.util.concurrent.ExecutionException;
1516
import net.bytebuddy.asm.Advice;
@@ -58,11 +59,19 @@ public void methodAdvice(MethodTransformer transformer) {
5859
public static class WriteToAdvice {
5960
@Advice.OnMethodEnter(suppress = Throwable.class)
6061
public static void onEnter(@Advice.This AbstractMessage message) {
62+
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(AbstractMessage.class);
63+
if (callDepth > 0) {
64+
return;
65+
}
6166
SchemaExtractor.attachSchemaOnSpan(message, activeSpan(), SchemaExtractor.serialization);
6267
}
6368

6469
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6570
public static void stopSpan(@Advice.Thrown final Throwable throwable) {
71+
final int callDepth = CallDepthThreadLocalMap.decrementCallDepth(AbstractMessage.class);
72+
if (callDepth > 0) {
73+
return;
74+
}
6675
AgentSpan span = activeSpan();
6776
if (throwable != null) {
6877
span.addThrowable(

dd-java-agent/instrumentation/protobuf/src/main/java/datadog/trace/instrumentation/protobuf_java/AbstractParserInstrumentation.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
import com.google.auto.service.AutoService;
1111
import com.google.protobuf.AbstractMessage;
12+
import com.google.protobuf.AbstractParser;
1213
import com.google.protobuf.MessageLite;
1314
import datadog.trace.agent.tooling.Instrumenter;
1415
import datadog.trace.agent.tooling.InstrumenterModule;
16+
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
1517
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1618
import java.util.concurrent.ExecutionException;
1719
import net.bytebuddy.asm.Advice;
@@ -55,9 +57,19 @@ public void methodAdvice(MethodTransformer transformer) {
5557
}
5658

5759
public static class ParseFromAdvice {
60+
61+
@Advice.OnMethodEnter(suppress = Throwable.class)
62+
public static void trackDepth() {
63+
CallDepthThreadLocalMap.incrementCallDepth(AbstractParser.class);
64+
}
65+
5866
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
5967
public static void stopSpan(
6068
@Advice.Thrown final Throwable throwable, @Advice.Return MessageLite message) {
69+
final int callDepth = CallDepthThreadLocalMap.decrementCallDepth(AbstractParser.class);
70+
if (callDepth > 0) {
71+
return;
72+
}
6173
AgentSpan span = activeSpan();
6274
if (span == null) {
6375
return;

dd-java-agent/instrumentation/protobuf/src/main/java/datadog/trace/instrumentation/protobuf_java/DynamicMessageInstrumentation.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
import com.google.auto.service.AutoService;
1111
import com.google.protobuf.Descriptors.Descriptor;
12+
import com.google.protobuf.DynamicMessage;
1213
import datadog.trace.agent.tooling.Instrumenter;
1314
import datadog.trace.agent.tooling.InstrumenterModule;
15+
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
1416
import net.bytebuddy.asm.Advice;
1517

1618
@AutoService(InstrumenterModule.class)
@@ -50,7 +52,16 @@ public void methodAdvice(MethodTransformer transformer) {
5052
public static class ParseFromAdvice {
5153
@Advice.OnMethodEnter(suppress = Throwable.class)
5254
public static void onEnter(@Advice.Argument(0) final Descriptor descriptor) {
55+
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(DynamicMessage.class);
56+
if (callDepth > 0) {
57+
return;
58+
}
5359
SchemaExtractor.attachSchemaOnSpan(descriptor, activeSpan(), SchemaExtractor.deserialization);
5460
}
61+
62+
@Advice.OnMethodExit()
63+
public static void trackDepth() {
64+
CallDepthThreadLocalMap.decrementCallDepth(DynamicMessage.class);
65+
}
5566
}
5667
}

0 commit comments

Comments
 (0)