Skip to content

Commit daaa592

Browse files
committed
clean up a little
appease jacoco
1 parent 6e5ce7c commit daaa592

File tree

7 files changed

+98
-152
lines changed

7 files changed

+98
-152
lines changed

dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/spanorigin/CodeOriginInfo.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ public static void entry(Method method) {
1919
}
2020
}
2121

22-
public static void entry(Class<?> type, Method method) {
23-
if (InstrumenterConfig.get().isCodeOriginEnabled()) {
24-
String signature =
25-
stream(method.getParameterTypes())
26-
.map(Class::getTypeName)
27-
.collect(Collectors.joining(", ", "(", ")"));
28-
captureCodeOrigin(signature);
29-
}
30-
}
31-
3222
public static void entry(String name, Class<?> target, String method, Class<?>[] types) {
3323
if (InstrumenterConfig.get().isCodeOriginEnabled()) {
3424
captureCodeOrigin(name, target, method, types, true);
Lines changed: 42 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.datadog.debugger.codeorigin;
22

33
import static com.datadog.debugger.agent.ConfigurationAcceptor.Source.CODE_ORIGIN;
4-
import static java.util.Arrays.asList;
54
import static java.util.Arrays.stream;
65

76
import com.datadog.debugger.agent.ConfigurationUpdater;
87
import com.datadog.debugger.exception.Fingerprinter;
98
import com.datadog.debugger.probe.CodeOriginProbe;
109
import com.datadog.debugger.probe.Where;
11-
import com.datadog.debugger.util.ClassNameFiltering;
1210
import datadog.trace.api.Config;
1311
import datadog.trace.bootstrap.debugger.CapturedContext;
1412
import datadog.trace.bootstrap.debugger.DebuggerContext;
@@ -18,19 +16,13 @@
1816
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1917
import datadog.trace.util.AgentTaskScheduler;
2018
import datadog.trace.util.stacktrace.StackWalkerFactory;
21-
import java.io.ByteArrayOutputStream;
22-
import java.io.IOException;
23-
import java.io.InputStream;
2419
import java.util.Collection;
2520
import java.util.Collections;
2621
import java.util.HashMap;
27-
import java.util.HashSet;
2822
import java.util.Map;
2923
import java.util.UUID;
3024
import java.util.concurrent.ConcurrentHashMap;
3125
import java.util.stream.Collectors;
32-
import org.objectweb.asm.ClassReader;
33-
import org.objectweb.asm.tree.ClassNode;
3426
import org.slf4j.Logger;
3527
import org.slf4j.LoggerFactory;
3628

@@ -43,92 +35,30 @@ public class DefaultCodeOriginRecorder implements CodeOriginRecorder {
4335

4436
private final Map<String, CodeOriginProbe> probes = new ConcurrentHashMap<>();
4537

46-
private final AgentTaskScheduler taskScheduler;
47-
4838
private final int maxUserFrames;
4939

50-
// this really should only be used for testing
51-
public DefaultCodeOriginRecorder() {
52-
maxUserFrames = 8;
53-
configurationUpdater = null;
54-
DebuggerContext.initClassNameFilter(
55-
new ClassNameFiltering(
56-
new HashSet<>(
57-
asList(
58-
"sun",
59-
"org.junit",
60-
"java.",
61-
"org.gradle",
62-
"com.sun",
63-
"worker.org.gradle",
64-
"datadog",
65-
"com.datadog.debugger.probe",
66-
"com.datadog.debugger.codeorigin"))));
67-
new ClassNameFiltering(
68-
new HashSet<>(
69-
asList(
70-
"sun",
71-
"org.junit",
72-
"java.",
73-
"org.gradle",
74-
"com.sun",
75-
"worker.org.gradle",
76-
"datadog",
77-
"com.datadog.debugger.probe",
78-
"com.datadog.debugger.codeorigin")));
79-
taskScheduler = AgentTaskScheduler.INSTANCE;
80-
}
81-
8240
public DefaultCodeOriginRecorder(Config config, ConfigurationUpdater configurationUpdater) {
8341
this.configurationUpdater = configurationUpdater;
8442
maxUserFrames = config.getDebuggerCodeOriginMaxUserFrames();
85-
taskScheduler = AgentTaskScheduler.INSTANCE;
86-
}
87-
88-
public DefaultCodeOriginRecorder(
89-
Config config, ConfigurationUpdater configurationUpdater, AgentTaskScheduler taskScheduler) {
90-
this.configurationUpdater = configurationUpdater;
91-
maxUserFrames = config.getDebuggerCodeOriginMaxUserFrames();
92-
this.taskScheduler = taskScheduler;
9343
}
9444

9545
@Override
9646
public String captureCodeOrigin(String signature) {
9747
StackTraceElement element = findPlaceInStack();
9848
String fingerprint = Fingerprinter.fingerprint(element);
99-
if (fingerprint == null) {
100-
LOG.debug("Unable to fingerprint stack trace");
101-
return null;
102-
}
10349
CodeOriginProbe probe;
10450

105-
AgentSpan span = AgentTracer.activeSpan();
106-
if (!isAlreadyInstrumented(fingerprint)) {
107-
Where where =
108-
Where.of(
109-
element.getClassName(),
110-
element.getMethodName(),
111-
signature,
112-
String.valueOf(element.getLineNumber()));
113-
114-
probe =
115-
new CodeOriginProbe(
116-
new ProbeId(UUID.randomUUID().toString(), 0),
117-
where.getSignature(),
118-
where,
119-
maxUserFrames);
120-
addFingerprint(fingerprint, probe);
121-
122-
installProbe(probe);
123-
if (span != null) {
124-
// committing here manually so that first run probe encounters decorate the span until the
125-
// instrumentation gets installed
126-
probe.commit(
127-
CapturedContext.EMPTY_CONTEXT, CapturedContext.EMPTY_CONTEXT, Collections.emptyList());
128-
}
129-
130-
} else {
51+
if (isAlreadyInstrumented(fingerprint)) {
13152
probe = fingerprints.get(fingerprint);
53+
} else {
54+
probe =
55+
createProbe(
56+
signature,
57+
Where.of(
58+
element.getClassName(),
59+
element.getMethodName(),
60+
signature,
61+
String.valueOf(element.getLineNumber())));
13262
}
13363

13464
return probe.getId();
@@ -142,30 +72,42 @@ public String captureCodeOrigin(
14272
if (isAlreadyInstrumented(name)) {
14373
probe = fingerprints.get(name);
14474
} else {
145-
Where where =
146-
Where.of(
147-
target.getName(),
148-
method,
149-
stream(types).map(Class::getTypeName).collect(Collectors.joining(", ", "(", ")")));
150-
15175
probe =
152-
new CodeOriginProbe(
153-
new ProbeId(UUID.randomUUID().toString(), 0),
154-
where.getSignature(),
155-
where,
156-
maxUserFrames);
157-
addFingerprint(name, probe);
158-
159-
installProbe(probe);
160-
// committing here manually so that first run probe encounters decorate the span until the
161-
// instrumentation gets installed
162-
probe.commit(
163-
CapturedContext.EMPTY_CONTEXT, CapturedContext.EMPTY_CONTEXT, Collections.emptyList());
76+
createProbe(
77+
name,
78+
Where.of(
79+
target.getName(),
80+
method,
81+
stream(types)
82+
.map(Class::getTypeName)
83+
.collect(Collectors.joining(", ", "(", ")"))));
16484
}
16585

16686
return probe.getId();
16787
}
16888

89+
private CodeOriginProbe createProbe(String fingerPrint, Where where) {
90+
CodeOriginProbe probe;
91+
AgentSpan span = AgentTracer.activeSpan();
92+
93+
probe =
94+
new CodeOriginProbe(
95+
new ProbeId(UUID.randomUUID().toString(), 0),
96+
where.getSignature(),
97+
where,
98+
maxUserFrames);
99+
addFingerprint(fingerPrint, probe);
100+
101+
installProbe(probe);
102+
// committing here manually so that first run probe encounters decorate the span until the
103+
// instrumentation gets installed
104+
if (span != null) {
105+
probe.commit(
106+
CapturedContext.EMPTY_CONTEXT, CapturedContext.EMPTY_CONTEXT, Collections.emptyList());
107+
}
108+
return probe;
109+
}
110+
169111
private StackTraceElement findPlaceInStack() {
170112
return StackWalkerFactory.INSTANCE.walk(
171113
stream ->
@@ -187,7 +129,8 @@ public String installProbe(CodeOriginProbe probe) {
187129
CodeOriginProbe installed = probes.putIfAbsent(probe.getId(), probe);
188130
if (installed == null) {
189131
if (configurationUpdater != null) {
190-
taskScheduler.execute(() -> configurationUpdater.accept(CODE_ORIGIN, getProbes()));
132+
AgentTaskScheduler.INSTANCE.execute(
133+
() -> configurationUpdater.accept(CODE_ORIGIN, getProbes()));
191134
}
192135
return probe.getId();
193136
}
@@ -201,24 +144,4 @@ public CodeOriginProbe getProbe(String probeId) {
201144
public Collection<CodeOriginProbe> getProbes() {
202145
return probes.values();
203146
}
204-
205-
private ClassNode parseClassFile(String className) {
206-
byte[] bytes = new byte[8192];
207-
try (InputStream inputStream =
208-
getClass()
209-
.getClassLoader()
210-
.getResourceAsStream(String.format("%s.class", className.replace('.', '/')))) {
211-
ByteArrayOutputStream bao = new ByteArrayOutputStream();
212-
int bytesRead;
213-
while ((bytesRead = inputStream.read(bytes)) != -1) {
214-
bao.write(bytes, 0, bytesRead);
215-
}
216-
ClassNode classNode = new ClassNode();
217-
new ClassReader(bao.toByteArray()).accept(classNode, ClassReader.SKIP_FRAMES);
218-
return classNode;
219-
} catch (IOException e) {
220-
LOG.error("Can't read class file information for {}", className);
221-
return null;
222-
}
223-
}
224147
}

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturingTestBase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.datadog.debugger.util.MoshiSnapshotHelper.NOT_CAPTURED_REASON;
44
import static com.datadog.debugger.util.MoshiSnapshotTestHelper.VALUE_ADAPTER;
55
import static com.datadog.debugger.util.TestHelper.setFieldInConfig;
6+
import static datadog.trace.util.AgentThreadFactory.AgentThread.TASK_SCHEDULER;
67
import static java.util.Arrays.asList;
78
import static org.junit.jupiter.api.Assertions.assertEquals;
89
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -30,6 +31,7 @@
3031
import datadog.trace.bootstrap.debugger.ProbeImplementation;
3132
import datadog.trace.bootstrap.debugger.ProbeRateLimiter;
3233
import datadog.trace.bootstrap.debugger.util.Redaction;
34+
import datadog.trace.util.AgentTaskScheduler;
3335
import java.io.File;
3436
import java.io.IOException;
3537
import java.lang.instrument.ClassFileTransformer;
@@ -351,6 +353,15 @@ public static LogProbe.Builder createProbeBuilder(
351353

352354
protected TestSnapshotListener installProbes(
353355
Configuration configuration, ProbeDefinition... probes) {
356+
357+
AgentTaskScheduler.INSTANCE =
358+
new AgentTaskScheduler(TASK_SCHEDULER) {
359+
@Override
360+
public void execute(final Runnable target) {
361+
target.run();
362+
}
363+
};
364+
354365
config = mock(Config.class);
355366
when(config.isDebuggerEnabled()).thenReturn(true);
356367
when(config.isDebuggerClassFileDumpEnabled()).thenReturn(true);

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/origin/CodeOriginTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,33 @@ public void testCaptureCodeOriginWithNullSignature() {
154154
assertFalse(probe.entrySpanProbe());
155155
}
156156

157+
@Test
158+
public void testCaptureCodeOriginWithExplicitInfo() throws IOException, URISyntaxException {
159+
final String CLASS_NAME = "com.datadog.debugger.CodeOrigin04";
160+
final Class<?> testClass = compileAndLoadClass(CLASS_NAME);
161+
installProbes();
162+
CodeOriginProbe probe =
163+
codeOriginRecorder.getProbe(
164+
codeOriginRecorder.captureCodeOrigin(
165+
"explicit", testClass, "main", new Class[] {int.class}, true));
166+
assertNotNull(probe, "The probe should have been created.");
167+
assertTrue(probe.entrySpanProbe(), "Should be an entry probe.");
168+
}
169+
170+
@Test
171+
public void testDuplicateInstrumentations() throws IOException, URISyntaxException {
172+
final String CLASS_NAME = "com.datadog.debugger.CodeOrigin04";
173+
final Class<?> testClass = compileAndLoadClass(CLASS_NAME);
174+
installProbes();
175+
String probe1 =
176+
codeOriginRecorder.captureCodeOrigin(
177+
"explicit", testClass, "main", new Class[] {int.class}, true);
178+
String probe2 =
179+
codeOriginRecorder.captureCodeOrigin(
180+
"explicit", testClass, "main", new Class[] {int.class}, true);
181+
assertEquals(probe1, probe2);
182+
}
183+
157184
@NotNull
158185
private List<LogProbe> codeOriginProbes(String type) {
159186
CodeOriginProbe entry =

dd-java-agent/instrumentation/grpc-1.5/src/main/java/datadog/trace/instrumentation/grpc/server/MethodHandlersInstrumentation.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,25 @@ public void methodAdvice(MethodTransformer transformer) {
4444
public static class BuildAdvice {
4545

4646
@Advice.OnMethodEnter(suppress = Throwable.class)
47-
public static void onEnter(@Advice.Argument(0) Object serviceImpl) {
47+
public static void onEnter(@Advice.Argument(0) Object serviceImpl) throws Exception {
4848
Class<?> serviceClass = serviceImpl.getClass();
4949
Class<?> superclass = serviceClass.getSuperclass();
5050
if (superclass != null) {
51-
try {
52-
Class<?> enclosingClass = superclass.getEnclosingClass();
53-
Field serviceNameField = enclosingClass.getDeclaredField("SERVICE_NAME");
54-
String serviceName = (String) serviceNameField.get(enclosingClass);
55-
for (Method method : superclass.getDeclaredMethods()) {
56-
try {
57-
Method declaredMethod =
58-
serviceClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
59-
CodeOriginInfo.entry(
60-
String.format("%s/%s", serviceName, method.getName()),
61-
serviceClass,
62-
declaredMethod.getName(),
63-
declaredMethod.getParameterTypes());
64-
} catch (NoSuchMethodException e) {
65-
// service method not override on the impl. skipping instrumentation
66-
}
51+
Class<?> enclosingClass = superclass.getEnclosingClass();
52+
Field serviceNameField = enclosingClass.getDeclaredField("SERVICE_NAME");
53+
String serviceName = (String) serviceNameField.get(enclosingClass);
54+
for (Method method : superclass.getDeclaredMethods()) {
55+
try {
56+
Method declaredMethod =
57+
serviceClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
58+
CodeOriginInfo.entry(
59+
String.format("%s/%s", serviceName, method.getName()),
60+
serviceClass,
61+
declaredMethod.getName(),
62+
declaredMethod.getParameterTypes());
63+
} catch (NoSuchMethodException e) {
64+
// service method not overridden on the impl. skipping instrumentation.
6765
}
68-
} catch (ReflectiveOperationException e) {
69-
// need to find a way to log this
70-
// LOG.debug("Member not found. Not instrumenting {}", serviceClass.getName(), e);
7166
}
7267
}
7368
}

dd-java-agent/instrumentation/grpc-1.5/src/test/groovy/GrpcCodeOriginTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,13 @@ abstract class GrpcCodeOriginTest extends VersionedNamingTestBase {
300300
DebuggerContext.initClassFilter(new DenyListHelper(null))
301301
DebuggerContext.initValueSerializer(new JsonSnapshotSerializer())
302302

303-
def scheduler = new AgentTaskScheduler(null) {
303+
AgentTaskScheduler.INSTANCE = new AgentTaskScheduler(null) {
304304
@Override
305305
void execute(Runnable target) {
306306
target.run()
307307
}
308308
}
309-
DebuggerContext.initCodeOrigin(new DefaultCodeOriginRecorder(config, configurationUpdater, scheduler))
309+
DebuggerContext.initCodeOrigin(new DefaultCodeOriginRecorder(config, configurationUpdater))
310310
}
311311
}
312312

internal-api/src/main/java/datadog/trace/util/AgentTaskScheduler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
public class AgentTaskScheduler implements Executor {
2323
private static final Logger log = LoggerFactory.getLogger(AgentTaskScheduler.class);
24-
public static final AgentTaskScheduler INSTANCE = new AgentTaskScheduler(TASK_SCHEDULER);
24+
public static AgentTaskScheduler INSTANCE = new AgentTaskScheduler(TASK_SCHEDULER);
2525

2626
private static final long SHUTDOWN_TIMEOUT = 5; // seconds
2727

0 commit comments

Comments
 (0)