2323import datadog .trace .util .stacktrace .StackWalkerFactory ;
2424import java .util .Collection ;
2525import java .util .Collections ;
26- import java .util .HashMap ;
2726import java .util .List ;
2827import java .util .Map ;
2928import 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