Skip to content

Commit 2aedd77

Browse files
committed
further simplify
1 parent 4db58ed commit 2aedd77

1 file changed

Lines changed: 13 additions & 48 deletions

File tree

datadog_lambda/tracing.py

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -547,31 +547,6 @@ def extract_context_from_step_functions(event, lambda_context):
547547
return extract_context_from_lambda_context(lambda_context)
548548

549549

550-
def _durable_operations(event):
551-
if not isinstance(event, dict):
552-
return []
553-
554-
operations = event.get("InitialExecutionState", {}).get("Operations")
555-
if isinstance(operations, list):
556-
return operations
557-
if not isinstance(operations, dict):
558-
return []
559-
560-
numeric_keys = []
561-
other_keys = []
562-
for key, value in operations.items():
563-
if not isinstance(value, dict):
564-
continue
565-
try:
566-
numeric_keys.append((int(key), value))
567-
except (TypeError, ValueError):
568-
other_keys.append((str(key), value))
569-
570-
numeric_keys.sort(key=lambda item: item[0])
571-
other_keys.sort(key=lambda item: item[0])
572-
return [value for _, value in numeric_keys + other_keys]
573-
574-
575550
def _extract_context_from_durable_checkpoint(operation):
576551
if not isinstance(operation, dict):
577552
return None
@@ -623,17 +598,14 @@ def _extract_context_from_durable_input_payload(operation):
623598

624599

625600
def extract_context_from_durable_execution(event):
626-
if not isinstance(event, dict):
627-
return None
628-
if not isinstance(event.get("DurableExecutionArn"), str):
629-
return None
630-
631-
operations = _durable_operations(event)
632-
if not operations:
601+
operations = event.get("InitialExecutionState", {}).get("Operations")
602+
if isinstance(operations, dict):
603+
operations = list(operations.values())
604+
if not isinstance(operations, list) or not operations:
633605
return None
634606

635-
best_context = None
636-
best_number = -1
607+
highest = -1
608+
best_operation = None
637609
for operation in operations:
638610
if not isinstance(operation, dict):
639611
continue
@@ -645,21 +617,14 @@ def extract_context_from_durable_execution(event):
645617
number = int(suffix)
646618
except (TypeError, ValueError):
647619
continue
648-
if number < best_number:
649-
continue
650-
context = _extract_context_from_durable_checkpoint(operation)
651-
if _is_context_complete(context):
652-
best_context = context
653-
best_number = number
620+
if number > highest:
621+
highest = number
622+
best_operation = operation
654623

655-
if best_context is not None:
656-
return best_context
624+
if best_operation is not None:
625+
return _extract_context_from_durable_checkpoint(best_operation)
657626

658-
upstream_context = _extract_context_from_durable_input_payload(operations[0])
659-
if _is_context_complete(upstream_context):
660-
return upstream_context
661-
662-
return None
627+
return _extract_context_from_durable_input_payload(operations[0])
663628

664629

665630
def extract_context_custom_extractor(extractor, event, lambda_context):
@@ -753,7 +718,7 @@ def extract_dd_trace_context(
753718

754719
if extractor is not None:
755720
context = extract_context_custom_extractor(extractor, event, lambda_context)
756-
elif isinstance(event, (set, dict)) and "DurableExecutionArn" in event:
721+
elif isinstance(event, dict) and "DurableExecutionArn" in event:
757722
context = extract_context_from_durable_execution(event)
758723
elif isinstance(event, (set, dict)) and "request" in event:
759724
context = extract_context_from_request_header_or_context(

0 commit comments

Comments
 (0)