15
15
import datadog .trace .bootstrap .instrumentation .api .AgentTracer ;
16
16
import datadog .trace .util .AgentTaskScheduler ;
17
17
import datadog .trace .util .stacktrace .StackWalkerFactory ;
18
+ import java .lang .reflect .Method ;
18
19
import java .util .Collection ;
19
20
import java .util .Collections ;
20
21
import java .util .HashMap ;
27
28
public class DefaultCodeOriginRecorder implements CodeOriginRecorder {
28
29
private static final Logger LOG = LoggerFactory .getLogger (DefaultCodeOriginRecorder .class );
29
30
30
- private final Config config ;
31
-
32
31
private final ConfigurationUpdater configurationUpdater ;
33
32
34
33
private final Map <String , CodeOriginProbe > fingerprints = new HashMap <>();
35
34
36
35
private final Map <String , CodeOriginProbe > probes = new ConcurrentHashMap <>();
37
36
38
- private final AgentTaskScheduler taskScheduler = AgentTaskScheduler . INSTANCE ;
37
+ private final int maxUserFrames ;
39
38
40
39
public DefaultCodeOriginRecorder (Config config , ConfigurationUpdater configurationUpdater ) {
41
- this .config = config ;
42
40
this .configurationUpdater = configurationUpdater ;
41
+ maxUserFrames = config .getDebuggerCodeOriginMaxUserFrames ();
43
42
}
44
43
45
44
@ Override
46
- public String captureCodeOrigin (String signature ) {
45
+ public String captureCodeOrigin (boolean entry ) {
47
46
StackTraceElement element = findPlaceInStack ();
48
47
String fingerprint = Fingerprinter .fingerprint (element );
49
- if (fingerprint == null ) {
50
- LOG .debug ("Unable to fingerprint stack trace" );
51
- return null ;
52
- }
53
48
CodeOriginProbe probe ;
54
49
55
- AgentSpan span = AgentTracer .activeSpan ();
56
- if (!isAlreadyInstrumented (fingerprint )) {
57
- Where where =
58
- Where .of (
59
- element .getClassName (),
60
- element .getMethodName (),
61
- signature ,
62
- String .valueOf (element .getLineNumber ()));
63
-
50
+ if (isAlreadyInstrumented (fingerprint )) {
51
+ probe = fingerprints .get (fingerprint );
52
+ } else {
64
53
probe =
65
- new CodeOriginProbe (
66
- new ProbeId (UUID .randomUUID ().toString (), 0 ),
67
- where .getSignature (),
68
- where ,
69
- config .getDebuggerCodeOriginMaxUserFrames ());
70
- addFingerprint (fingerprint , probe );
71
-
72
- installProbe (probe );
73
- if (span != null ) {
74
- // committing here manually so that first run probe encounters decorate the span until the
75
- // instrumentation gets installed
76
- probe .commit (
77
- CapturedContext .EMPTY_CONTEXT , CapturedContext .EMPTY_CONTEXT , Collections .emptyList ());
78
- }
54
+ createProbe (
55
+ fingerprint ,
56
+ entry ,
57
+ Where .of (
58
+ element .getClassName (),
59
+ element .getMethodName (),
60
+ null ,
61
+ String .valueOf (element .getLineNumber ())));
62
+ }
63
+
64
+ return probe .getId ();
65
+ }
79
66
67
+ @ Override
68
+ public String captureCodeOrigin (Method method , boolean entry ) {
69
+ CodeOriginProbe probe ;
70
+
71
+ String fingerPrint = method .toString ();
72
+ if (isAlreadyInstrumented (fingerPrint )) {
73
+ probe = fingerprints .get (fingerPrint );
80
74
} else {
81
- probe = fingerprints . get ( fingerprint );
75
+ probe = createProbe ( fingerPrint , entry , Where . of ( method ) );
82
76
}
83
77
84
78
return probe .getId ();
85
79
}
86
80
81
+ private CodeOriginProbe createProbe (String fingerPrint , boolean entry , Where where ) {
82
+ CodeOriginProbe probe ;
83
+ AgentSpan span = AgentTracer .activeSpan ();
84
+
85
+ probe =
86
+ new CodeOriginProbe (
87
+ new ProbeId (UUID .randomUUID ().toString (), 0 ), entry , where , maxUserFrames );
88
+ addFingerprint (fingerPrint , probe );
89
+
90
+ installProbe (probe );
91
+ // committing here manually so that first run probe encounters decorate the span until the
92
+ // instrumentation gets installed
93
+ if (span != null ) {
94
+ probe .commit (
95
+ CapturedContext .EMPTY_CONTEXT , CapturedContext .EMPTY_CONTEXT , Collections .emptyList ());
96
+ }
97
+ return probe ;
98
+ }
99
+
87
100
private StackTraceElement findPlaceInStack () {
88
101
return StackWalkerFactory .INSTANCE .walk (
89
102
stream ->
@@ -104,7 +117,8 @@ void addFingerprint(String fingerprint, CodeOriginProbe probe) {
104
117
public String installProbe (CodeOriginProbe probe ) {
105
118
CodeOriginProbe installed = probes .putIfAbsent (probe .getId (), probe );
106
119
if (installed == null ) {
107
- taskScheduler .execute (() -> configurationUpdater .accept (CODE_ORIGIN , getProbes ()));
120
+ AgentTaskScheduler .INSTANCE .execute (
121
+ () -> configurationUpdater .accept (CODE_ORIGIN , getProbes ()));
108
122
return probe .getId ();
109
123
}
110
124
return installed .getId ();
0 commit comments