1
1
package datadog .trace .instrumentation .aws .v2 .sfn ;
2
2
3
- import datadog .trace .bootstrap .JsonBuffer ;
3
+ import datadog .json .JsonMapper ;
4
+ import datadog .json .JsonWriter ;
4
5
import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
5
6
6
7
public class InputAttributeInjector {
7
8
public static String buildTraceContext (AgentSpan span ) {
8
- // Extract span tags
9
- JsonBuffer spanTagsJSON = new JsonBuffer ();
10
- spanTagsJSON .beginObject ();
11
- span .getTags ()
12
- .forEach ((tagKey , tagValue ) -> spanTagsJSON .name (tagKey ).value (tagValue .toString ()));
13
- spanTagsJSON .endObject ();
9
+ String tagsJson = JsonMapper .toJson (span .getTags ());
14
10
15
- // Build DD trace context object
16
- JsonBuffer ddTraceContextJSON = new JsonBuffer ();
17
- ddTraceContextJSON
18
- .beginObject ()
19
- .name ("_datadog" )
20
- .beginObject ()
21
- .name ("x-datadog-trace-id" )
22
- .value (span .getTraceId ().toString ())
23
- .name ("x-datadog-parent-id" )
24
- .value (String .valueOf (span .getSpanId ()))
25
- .name ("x-datadog-tags" )
26
- . value ( spanTagsJSON )
27
- .endObject ()
28
- .endObject ();
11
+ try {
12
+ JsonWriter ddTraceContextJSON = new JsonWriter ();
13
+ ddTraceContextJSON
14
+ .beginObject ()
15
+ .name ("_datadog" )
16
+ .beginObject ()
17
+ .name ("x-datadog-trace-id" )
18
+ .value (span .getTraceId ().toString ())
19
+ .name ("x-datadog-parent-id" )
20
+ .value (String .valueOf (span .getSpanId ()))
21
+ .name ("x-datadog-tags" )
22
+ . jsonValue ( tagsJson )
23
+ .endObject ()
24
+ .endObject ();
29
25
30
- return ddTraceContextJSON .toString ();
26
+ return ddTraceContextJSON .toString ();
27
+ } catch (Exception e ) {
28
+ return "{}" ;
29
+ }
31
30
}
32
31
33
32
public static String getModifiedInput (String request , String ddTraceContextJSON ) {
@@ -38,8 +37,8 @@ public static String getModifiedInput(String request, String ddTraceContextJSON)
38
37
if (inputContent .isEmpty ()) {
39
38
modifiedInput .insert (endPos , ddTraceContextJSON );
40
39
} else {
41
- modifiedInput . insert (
42
- endPos , "," . concat ( ddTraceContextJSON )); // prepend comma to existing input
40
+ // Prepend comma to separate from existing content
41
+ modifiedInput . insert ( endPos , "," + ddTraceContextJSON );
43
42
}
44
43
return modifiedInput .toString ();
45
44
}
0 commit comments