11package datadog .trace .instrumentation .aws .v2 .sfn ;
22
3- import datadog .trace .bootstrap .JsonBuffer ;
3+ import datadog .json .JsonMapper ;
4+ import datadog .json .JsonWriter ;
45import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
56
67public class InputAttributeInjector {
78 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 ());
1410
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 ();
2925
30- return ddTraceContextJSON .toString ();
26+ return ddTraceContextJSON .toString ();
27+ } catch (Exception e ) {
28+ return "{}" ;
29+ }
3130 }
3231
3332 public static String getModifiedInput (String request , String ddTraceContextJSON ) {
@@ -38,8 +37,8 @@ public static String getModifiedInput(String request, String ddTraceContextJSON)
3837 if (inputContent .isEmpty ()) {
3938 modifiedInput .insert (endPos , ddTraceContextJSON );
4039 } 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 );
4342 }
4443 return modifiedInput .toString ();
4544 }
0 commit comments