5
5
import static datadog .trace .api .DDTags .DD_CODE_ORIGIN_TYPE ;
6
6
import static java .lang .String .format ;
7
7
import static java .util .Arrays .asList ;
8
+ import static java .util .Collections .emptyList ;
8
9
import static java .util .Collections .singletonList ;
9
10
10
11
import com .datadog .debugger .agent .DebuggerAgent ;
@@ -31,6 +32,8 @@ public class CodeOriginProbe extends LogProbe implements ForceMethodInstrumentat
31
32
32
33
private final boolean entrySpanProbe ;
33
34
35
+ private List <StackTraceElement > stackTraceElements ;
36
+
34
37
public CodeOriginProbe (ProbeId probeId , boolean entry , Where where , int maxFrames ) {
35
38
super (LANGUAGE , probeId , null , where , MethodLocation .EXIT , null , null , true , null , null , null );
36
39
this .entrySpanProbe = entry ;
@@ -82,8 +85,25 @@ public void commit(
82
85
span .getLocalRootSpan ().setTag (getId (), (String ) null ); // clear possible span reference
83
86
}
84
87
88
+ private List <StackTraceElement > findLocation () {
89
+ if (entrySpanProbe && stackTraceElements == null ) {
90
+ ProbeLocation probeLocation = getLocation ();
91
+ List <String > lines = probeLocation .getLines ();
92
+ int line = lines == null ? -1 : Integer .parseInt (lines .get (0 ));
93
+ stackTraceElements =
94
+ singletonList (
95
+ new StackTraceElement (
96
+ probeLocation .getType (),
97
+ probeLocation .getMethod (),
98
+ probeLocation .getFile (),
99
+ line ));
100
+ }
101
+ return stackTraceElements != null ? stackTraceElements : emptyList ();
102
+ }
103
+
85
104
private void applySpanOriginTags (AgentSpan span , String snapshotId ) {
86
- List <StackTraceElement > entries = getUserStackFrames ();
105
+ List <StackTraceElement > entries =
106
+ stackTraceElements != null ? stackTraceElements : getUserStackFrames ();
87
107
List <AgentSpan > agentSpans =
88
108
entrySpanProbe ? asList (span , span .getLocalRootSpan ()) : singletonList (span );
89
109
@@ -92,7 +112,8 @@ private void applySpanOriginTags(AgentSpan span, String snapshotId) {
92
112
93
113
for (int i = 0 ; i < entries .size (); i ++) {
94
114
StackTraceElement info = entries .get (i );
95
- s .setTag (format (DD_CODE_ORIGIN_FRAME , i , "file" ), info .getFileName ());
115
+ String fileName = info .getFileName ();
116
+ s .setTag (format (DD_CODE_ORIGIN_FRAME , i , "file" ), fileName );
96
117
s .setTag (format (DD_CODE_ORIGIN_FRAME , i , "method" ), info .getMethodName ());
97
118
s .setTag (format (DD_CODE_ORIGIN_FRAME , i , "line" ), info .getLineNumber ());
98
119
s .setTag (format (DD_CODE_ORIGIN_FRAME , i , "type" ), info .getClassName ());
@@ -127,12 +148,25 @@ private AgentSpan findSpan(AgentSpan candidate) {
127
148
public void buildLocation (InstrumentationResult result ) {
128
149
String type = where .getTypeName ();
129
150
String method = where .getMethodName ();
151
+ List <String > lines = null ;
152
+
153
+ String file = where .getSourceFile ();
154
+
130
155
if (result != null ) {
131
156
type = result .getTypeName ();
132
157
method = result .getMethodName ();
158
+ if (result .getMethodStart () != -1 ) {
159
+ lines = singletonList (String .valueOf (result .getMethodStart ()));
160
+ }
161
+ if (file == null ) {
162
+ file = result .getSourceFileName ();
163
+ }
164
+ if (entrySpanProbe ) {
165
+ stackTraceElements =
166
+ singletonList (new StackTraceElement (type , method , file , result .getMethodStart ()));
167
+ }
133
168
}
134
- // drop line number for code origin probe
135
- this .location = new ProbeLocation (type , method , where .getSourceFile (), null );
169
+ this .location = new ProbeLocation (type , method , file , lines );
136
170
}
137
171
138
172
private List <StackTraceElement > getUserStackFrames () {
0 commit comments