Skip to content

Commit 0e6e9dc

Browse files
authored
Cover string/b64 msg attribute trace extraction (#211)
* Cover string/b64 msg attribute trace extraction * fix lint * double quote to single and try to fix install crossbuild deps ci error * try apt get update before * revert to old install crossbuild deps step * apt-get * bring back whitespace in build.yml * remove space but keep empty line * b64decode right after pulling binaryValue/Value, add comment, and exception if message attributes are not string or binary * remove unessessary tests, fix failing test by adding dataType to msgAttribute, and try to get BinaryValue vs binaryValue depending on how casing off comes in * remove TitleCase checks and new snapshots * edit snapshots * bump ddtrace and update poetry.lock * remove newline from logs * update snapshots * force gh actions * revert change * edit snapshots * snapshots * gh action * revert force github action * make exception a logger.warn * force gh action * revert change * warning->debug to stop integration test failure * format and lint
1 parent fc1c286 commit 0e6e9dc

16 files changed

+387
-215
lines changed

datadog_lambda/tracing.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,25 @@ def extract_context_from_sqs_or_sns_event_or_context(event, lambda_context):
223223
first_record.get("Sns", {}).get("MessageAttributes", {}),
224224
)
225225
dd_payload = msg_attributes.get("_datadog", {})
226-
dd_json_data = dd_payload.get("stringValue", dd_payload.get("Value", r"{}"))
226+
# SQS uses dataType and binaryValue/stringValue
227+
# SNS uses Type and Value
228+
dd_json_data_type = dd_payload.get("Type", dd_payload.get("dataType", ""))
229+
if dd_json_data_type == "Binary":
230+
dd_json_data = dd_payload.get(
231+
"binaryValue",
232+
dd_payload.get("Value", r"{}"),
233+
)
234+
dd_json_data = base64.b64decode(dd_json_data)
235+
elif dd_json_data_type == "String":
236+
dd_json_data = dd_payload.get(
237+
"stringValue",
238+
dd_payload.get("Value", r"{}"),
239+
)
240+
else:
241+
logger.debug(
242+
"Datadog Lambda Python only supports extracting trace"
243+
"context from String or Binary SQS/SNS message attributes"
244+
)
227245
dd_data = json.loads(dd_json_data)
228246
trace_id = dd_data.get(TraceHeader.TRACE_ID)
229247
parent_id = dd_data.get(TraceHeader.PARENT_ID)

poetry.lock

Lines changed: 263 additions & 196 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ classifiers = [
2626
python = ">=3.6.0,<4"
2727
datadog = "^0.41.0"
2828
wrapt = "^1.11.2"
29-
ddtrace = "^0.58.1"
29+
ddtrace = "^0.59.2"
3030
importlib_metadata = {version = "^1.0", python = "<3.8"}
3131
boto3 = { version = "^1.10.33", optional = true }
3232
typing_extensions = {version = "^4.0", python = "<3.8"}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:sa-east-1:601427279990:serverlessTracingTopicPy:224b60ba-befc-4830-ad96-f1f0ac94eb04",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "87056a47-f506-5d77-908b-303605d3b197",
10+
"TopicArn": "arn:aws:sns:sa-east-1:601427279990:serverlessTracingTopicPy",
11+
"Subject": null,
12+
"Message": "Asynchronously invoking a Lambda function with SNS.",
13+
"Timestamp": "2022-01-31T14:13:41.637Z",
14+
"SignatureVersion": "1",
15+
"Signature": "BmwnJb0Ku2KgQef9QOgaSSTwLyUsbkRq90lzD5Vn4mAcRUOq2ForfMOYbxMB6idljWIWy9t/jK4AIMxPGk/eOGiRcENx3BvAcGcoDayBRFY13+xUGaPn5Lfoht/ZJ7/hmCgFWKRa8ooATZL+AwGAw6Id8qzf0R3M3k2asy5Vxa4ODKiFW9OzWY/zFgsYJhddR3JrQl9YOMRyIobNNHT96o1TwjGsSUTEemrxA6jQtb3QbardEKO+2SuataLEZki7gE2D2sA300WqZecumI339q7la+OIj6VDGDwFoppE2sh8hzJYXAH7oo11giwltE0V3/eLFCVhsE8Y1KD/yDPPsA==",
16+
"SigningCertUrl": "https://sns.sa-east-1.amazonaws.com/SimpleNotificationService-7ff5318490ec183fbaddaa2a969abfda.pem",
17+
"UnsubscribeUrl": "https://sns.sa-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:sa-east-1:601427279990:serverlessTracingTopicPy:224b60ba-befc-4830-ad96-f1f0ac94eb04",
18+
"MessageAttributes": {
19+
"_datadog": {
20+
"Type": "Binary",
21+
"Value": "eyJ4LWRhdGFkb2ctdHJhY2UtaWQiOiI0OTQ4Mzc3MzE2MzU3MjkxNDIxIiwieC1kYXRhZG9nLXBhcmVudC1pZCI6IjY3NDY5OTgwMTUwMzc0Mjk1MTIiLCJ4LWRhdGFkb2ctc2FtcGxpbmctcHJpb3JpdHkiOiIxIn0="
22+
}
23+
}
24+
}
25+
}
26+
]
27+
}

tests/integration/snapshots/logs/async-metrics_python36.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ HTTP GET https://www.datadoghq.com/ Headers: ["Accept-Encoding:gzip, deflate", "
11141114
"_inferred_span.tag_source": "self"
11151115
},
11161116
"metrics": {
1117+
"_dd.agent_psr": 1,
11171118
"system.pid": "XXXX",
11181119
"_sampling_priority_v1": 1,
11191120
"_dd.top_level": 1

tests/integration/snapshots/logs/async-metrics_python37.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ HTTP GET https://www.datadoghq.com/ Headers: ["Accept-Encoding:gzip, deflate", "
11141114
"_inferred_span.tag_source": "self"
11151115
},
11161116
"metrics": {
1117+
"_dd.agent_psr": 1,
11171118
"system.pid": "XXXX",
11181119
"_sampling_priority_v1": 1,
11191120
"_dd.top_level": 1

tests/integration/snapshots/logs/async-metrics_python38.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ HTTP GET https://www.datadoghq.com/ Headers: ["Accept-Encoding:gzip, deflate", "
11141114
"_inferred_span.tag_source": "self"
11151115
},
11161116
"metrics": {
1117+
"_dd.agent_psr": 1,
11171118
"system.pid": "XXXX",
11181119
"_sampling_priority_v1": 1,
11191120
"_dd.top_level": 1

tests/integration/snapshots/logs/async-metrics_python39.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ HTTP GET https://www.datadoghq.com/ Headers: ["Accept-Encoding:gzip, deflate", "
11141114
"_inferred_span.tag_source": "self"
11151115
},
11161116
"metrics": {
1117+
"_dd.agent_psr": 1,
11171118
"system.pid": "XXXX",
11181119
"_sampling_priority_v1": 1,
11191120
"_dd.top_level": 1

0 commit comments

Comments
 (0)