Skip to content

Commit 6c2fbbd

Browse files
Suggested loop structure
1 parent 103ffa1 commit 6c2fbbd

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

datadog_lambda/tracing.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,32 +247,33 @@ def extract_context_from_sqs_or_sns_event_or_context(
247247
except Exception:
248248
logger.debug("Failed extracting context as EventBridge to SQS.")
249249

250-
apm_context = None
251-
for idx, record in enumerate(event.get("Records", [])):
250+
apm_context: Context = None
251+
for record in event.get("Records", []):
252252
source_arn = (
253253
record.get("eventSourceARN")
254254
if event_type == "sqs"
255255
else record.get("Sns", {}).get("TopicArn")
256256
)
257-
dd_data = None
257+
dd_ctx = None
258258
try:
259-
dd_data = _extract_context_from_sqs_or_sns_record(record)
260-
if idx == 0:
261-
if dd_data and is_step_function_event(dd_data):
259+
dd_ctx = _extract_context_from_sqs_or_sns_record(record)
260+
if apm_context is None:
261+
if not dd_ctx:
262+
apm_context = _extract_context_from_xray(record)
263+
264+
elif dd_ctx and is_step_function_event(dd_ctx):
262265
try:
263-
return extract_context_from_step_functions(dd_data, None)
266+
return extract_context_from_step_functions(dd_ctx, None)
264267
except Exception:
265268
logger.debug(
266269
"Failed to extract Step Functions context from SQS/SNS event."
267270
)
268-
elif not dd_data:
269-
apm_context = _extract_context_from_xray(record)
270271
else:
271-
apm_context = propagator.extract(dd_data)
272+
apm_context = propagator.extract(dd_ctx)
272273
except Exception as e:
273274
logger.debug("The trace extractor returned with error %s", e)
274275
if config.data_streams_enabled:
275-
_dsm_set_checkpoint(dd_data, event_type, source_arn)
276+
_dsm_set_checkpoint(dd_ctx, event_type, source_arn)
276277
if not config.data_streams_enabled:
277278
break
278279

@@ -303,7 +304,6 @@ def _extract_context_from_sqs_or_sns_record(record):
303304
if dd_payload:
304305
# SQS uses dataType and binaryValue/stringValue
305306
# SNS uses Type and Value
306-
# fmt: off
307307
dd_json_data = None
308308
dd_json_data_type = dd_payload.get("Type") or dd_payload.get("dataType")
309309
if dd_json_data_type == "Binary":
@@ -314,7 +314,6 @@ def _extract_context_from_sqs_or_sns_record(record):
314314
dd_json_data = base64.b64decode(dd_json_data)
315315
elif dd_json_data_type == "String":
316316
dd_json_data = dd_payload.get("stringValue") or dd_payload.get("Value")
317-
# fmt: on
318317
else:
319318
logger.debug(
320319
"Datadog Lambda Python only supports extracting trace"

0 commit comments

Comments
 (0)