Skip to content

Commit ea7a9c7

Browse files
authored
Make Code Origin fingerprint map concurrent (#10043)
Code Origin probe creation can be called multiple threads/requests need to be thread safe. Adjust the code to be also atomic using this concurrent map to create only one probe
1 parent 4d1a38c commit ea7a9c7

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/codeorigin/DefaultCodeOriginRecorder.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import datadog.trace.util.stacktrace.StackWalkerFactory;
2424
import java.util.Collection;
2525
import java.util.Collections;
26-
import java.util.HashMap;
2726
import java.util.List;
2827
import java.util.Map;
2928
import java.util.concurrent.ConcurrentHashMap;
@@ -38,7 +37,7 @@ public class DefaultCodeOriginRecorder implements CodeOriginRecorder {
3837

3938
private final ConfigurationUpdater configurationUpdater;
4039

41-
private final Map<String, CodeOriginProbe> probesByFingerprint = new HashMap<>();
40+
private final Map<String, CodeOriginProbe> probesByFingerprint = new ConcurrentHashMap<>();
4241

4342
private final Map<String, CodeOriginProbe> probes = new ConcurrentHashMap<>();
4443
private final Map<String, LogProbe> logProbes = new ConcurrentHashMap<>();
@@ -75,8 +74,6 @@ public String captureCodeOrigin(boolean entry) {
7574
null,
7675
String.valueOf(element.getLineNumber()));
7776
probe = createProbe(fingerprint, entry, where);
78-
79-
LOG.debug("Creating probe for location {}", where);
8077
}
8178
return probe.getId();
8279
}
@@ -110,11 +107,13 @@ public void registerLogProbe(CodeOriginProbe probe) {
110107
}
111108

112109
private CodeOriginProbe createProbe(String fingerPrint, boolean entry, Where where) {
113-
CodeOriginProbe probe;
114-
AgentSpan span = AgentTracer.activeSpan();
115-
116-
probe = new CodeOriginProbe(ProbeId.newId(), entry, where);
117-
addFingerprint(fingerPrint, probe);
110+
CodeOriginProbe probe = new CodeOriginProbe(ProbeId.newId(), entry, where);
111+
CodeOriginProbe existing;
112+
if ((existing = probesByFingerprint.putIfAbsent(fingerPrint, probe)) != null) {
113+
// concurrent calls, considered the probe already installed
114+
return existing;
115+
}
116+
LOG.debug("Creating probe for location {}", where);
118117
CodeOriginProbe installed = probes.putIfAbsent(probe.getId(), probe);
119118

120119
// i think this check is unnecessary at this point time but leaving for now to be safe
@@ -126,6 +125,7 @@ private CodeOriginProbe createProbe(String fingerPrint, boolean entry, Where whe
126125
}
127126
// committing here manually so that first run probe encounters decorate the span until the
128127
// instrumentation gets installed
128+
AgentSpan span = AgentTracer.activeSpan();
129129
if (span != null) {
130130
probe.commit(
131131
CapturedContext.EMPTY_CONTEXT, CapturedContext.EMPTY_CONTEXT, Collections.emptyList());
@@ -142,10 +142,6 @@ private StackTraceElement findPlaceInStack() {
142142
.orElse(null));
143143
}
144144

145-
void addFingerprint(String fingerprint, CodeOriginProbe probe) {
146-
probesByFingerprint.putIfAbsent(fingerprint, probe);
147-
}
148-
149145
public void installProbes() {
150146
scheduler.execute(() -> configurationUpdater.accept(CODE_ORIGIN, getProbes()));
151147
}

0 commit comments

Comments
 (0)