Skip to content

Commit 60d9bf8

Browse files
committed
Parse JSON
1 parent 4524f7b commit 60d9bf8

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/messaging/DatadogAttributeParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static void forEachProperty(AgentPropagation.KeyClassifier classifier, St
2626
acceptJsonProperty(classifier, json, "x-datadog-sampling-priority");
2727
}
2828
if (Config.get().isDataStreamsEnabled()) {
29+
System.out.println("DSM ENABLED!!!: " + json);
2930
acceptJsonProperty(classifier, json, "dd-pathway-ctx-base64");
3031
}
3132
} catch (Exception e) {

dd-java-agent/instrumentation/aws-java-sqs-1.0/src/main/java/datadog/trace/instrumentation/aws/v1/sqs/MessageExtractAdapter.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import com.amazonaws.services.sqs.model.Message;
44
import com.amazonaws.services.sqs.model.MessageAttributeValue;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
57
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
68
import datadog.trace.bootstrap.instrumentation.messaging.DatadogAttributeParser;
9+
10+
import java.nio.ByteBuffer;
11+
import java.io.IOException;
12+
import java.util.Base64;
713
import java.util.Map;
814
import org.slf4j.Logger;
915
import org.slf4j.LoggerFactory;
@@ -28,6 +34,32 @@ public void forEachKey(Message carrier, AgentPropagation.KeyClassifier classifie
2834
} else if ("Binary".equals(datadog.getDataType())) {
2935
DatadogAttributeParser.forEachProperty(classifier, datadog.getBinaryValue());
3036
}
37+
} else {
38+
try {
39+
this.forEachKeyInBody(carrier.getBody(), classifier);
40+
} catch (IOException e) {
41+
log.warn("Error extracting Datadog context from SQS message body", e);
42+
}
43+
}
44+
}
45+
46+
public void forEachKeyInBody(String body, AgentPropagation.KeyClassifier classifier) throws IOException {
47+
ObjectMapper objectMapper = new ObjectMapper();
48+
49+
// Parse the JSON string into a JsonNode
50+
JsonNode rootNode = objectMapper.readTree(body);
51+
52+
// Navigate to MessageAttributes._datadog
53+
JsonNode messageAttributes = rootNode.path("MessageAttributes").path("_datadog");
54+
55+
// Extract Value and Type
56+
String value = messageAttributes.path("Value").asText();
57+
String type = messageAttributes.path("Type").asText();
58+
if ("String".equals(type)) {
59+
DatadogAttributeParser.forEachProperty(classifier, value);
60+
} else if ("Binary".equals(type)) {
61+
ByteBuffer decodedValue = ByteBuffer.wrap(Base64.getDecoder().decode(value));
62+
DatadogAttributeParser.forEachProperty(classifier, decodedValue);
3163
}
3264
}
3365

0 commit comments

Comments
 (0)