-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_execution_plugin.py
More file actions
1005 lines (831 loc) · 35.5 KB
/
Copy pathtest_execution_plugin.py
File metadata and controls
1005 lines (831 loc) · 35.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for the execution-view OpenTelemetry plugin (Workflow-rooted trace)."""
from __future__ import annotations
import threading
from concurrent.futures import ThreadPoolExecutor
from datetime import UTC, datetime
from types import SimpleNamespace
import opentelemetry.context as otel_context
import pytest
from aws_durable_execution_sdk_python.lambda_service import (
ErrorObject,
OperationStatus,
OperationSubType,
)
from aws_durable_execution_sdk_python.plugin import (
InvocationEndInfo,
InvocationStatus,
InvocationStartInfo,
OperationEndInfo,
OperationStartInfo,
OperationType,
UserFunctionEndInfo,
UserFunctionIncompleteInfo,
UserFunctionOutcome,
UserFunctionStartInfo,
)
from opentelemetry import trace
from opentelemetry.context import Context
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from aws_durable_execution_sdk_python_otel.deterministic_id_generator import (
derive_workflow_span_id,
operation_id_to_span_id,
)
from aws_durable_execution_sdk_python_otel.execution_plugin import ExecutionOtelPlugin
from aws_durable_execution_sdk_python_otel.otel_plugin_config import OtelPluginConfig
START_TIME = datetime(2024, 1, 2, 3, 4, 5, tzinfo=UTC)
END_TIME = datetime(2024, 1, 2, 3, 4, 6, tzinfo=UTC)
EXECUTION_ARN = "arn:aws:lambda:us-west-2:123456789012:function:workflow:$LATEST"
@pytest.fixture(autouse=True)
def _assert_otel_context_balanced():
"""Assert each test leaves the OTel thread-local context as it found it.
The plugin pairs every context.attach() with a context.detach(), so no
global reset is needed to keep tests isolated. Asserting the invariant here
turns a plugin lifecycle leak into a test failure instead of hiding it.
"""
before = otel_context.get_current()
yield
assert otel_context.get_current() == before, (
"test leaked OTel context state: an attach() was not detached"
)
def _create_plugin() -> tuple[ExecutionOtelPlugin, InMemorySpanExporter]:
"""Create an ExecutionOtelPlugin wired to an in-memory exporter."""
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
plugin = ExecutionOtelPlugin(
OtelPluginConfig(
tracer_provider=provider,
context_extractor=lambda _: Context(),
enrich_logger=False,
)
)
return plugin, exporter
def _invocation_start_info() -> InvocationStartInfo:
return InvocationStartInfo(
request_id="request-1",
execution_arn=EXECUTION_ARN,
execution_start_time=START_TIME,
is_first_invocation=True,
)
def _invocation_end_info(
status: InvocationStatus = InvocationStatus.SUCCEEDED,
) -> InvocationEndInfo:
return InvocationEndInfo(
request_id="request-1",
execution_arn=EXECUTION_ARN,
execution_start_time=START_TIME,
is_first_invocation=True,
status=status,
error=None,
)
def test_operation_attributes_use_structural_user_function_marker():
plugin, _ = _create_plugin()
operation_attributes = plugin._operation_attributes(
SimpleNamespace(
operation_type=OperationType.STEP,
status=OperationStatus.STARTED,
)
)
assert (
operation_attributes["durable.operation.status"]
== OperationStatus.STARTED.value
)
user_function_attributes = plugin._operation_attributes(
SimpleNamespace(
operation_type=OperationType.STEP,
status=OperationStatus.STARTED,
is_replay_children=False,
)
)
assert "durable.operation.status" not in user_function_attributes
# ---------------------------------------------------------------------------
# derive_workflow_span_id
# ---------------------------------------------------------------------------
def test_derive_workflow_span_id_is_deterministic():
assert derive_workflow_span_id(EXECUTION_ARN) == derive_workflow_span_id(
EXECUTION_ARN
)
def test_derive_workflow_span_id_differs_by_arn():
assert derive_workflow_span_id(EXECUTION_ARN) != derive_workflow_span_id(
EXECUTION_ARN + "-other"
)
def test_derive_workflow_span_id_is_64_bit():
span_id = derive_workflow_span_id(EXECUTION_ARN)
assert 0 < span_id < 2**64
def test_derive_workflow_span_id_rejects_empty_arn():
with pytest.raises(ValueError, match="execution ARN is required"):
derive_workflow_span_id("")
# ---------------------------------------------------------------------------
# Workflow + invocation span hierarchy
# ---------------------------------------------------------------------------
def test_workflow_and_invocation_are_separate_roots_without_ambient_parent():
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
plugin.on_invocation_end(_invocation_end_info())
spans = {s.name: s for s in exporter.get_finished_spans()}
assert "Workflow" in spans
assert "Invocation" in spans
workflow = spans["Workflow"]
invocation = spans["Invocation"]
# Workflow is a root span (no parent) with the deterministic span ID.
assert workflow.parent is None
assert workflow.context.span_id == derive_workflow_span_id(EXECUTION_ARN)
assert workflow.attributes["durable.execution.arn"] == EXECUTION_ARN
assert (
workflow.attributes["durable.execution.status"]
== InvocationStatus.SUCCEEDED.value
)
# Without ambient context, Invocation starts a separate provider trace.
assert invocation.parent is None
assert invocation.context.trace_id != workflow.context.trace_id
def test_explicit_mode_invocation_span_parented_to_ambient_span():
plugin, exporter = _create_plugin()
ambient = plugin._provider.get_tracer("ambient").start_span("lambda-invocation")
token = otel_context.attach(trace.set_span_in_context(ambient))
try:
plugin.on_invocation_start(_invocation_start_info())
plugin.on_invocation_end(_invocation_end_info())
finally:
otel_context.detach(token)
ambient.end()
spans = {s.name: s for s in exporter.get_finished_spans()}
workflow = spans["Workflow"]
invocation = spans["Invocation"]
assert invocation.parent is not None
assert invocation.parent.span_id == ambient.get_span_context().span_id
assert invocation.context.trace_id == ambient.get_span_context().trace_id
assert workflow.context.trace_id != ambient.get_span_context().trace_id
def test_workflow_span_dropped_on_non_terminal_status():
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
plugin.on_invocation_end(_invocation_end_info(status=InvocationStatus.PENDING))
names = [s.name for s in exporter.get_finished_spans()]
# Invocation span is always ended/exported; the Workflow span is dropped
# (not ended) on a non-terminal status, so it must not be exported.
assert "Invocation" in names
assert "Workflow" not in names
def test_operation_parented_under_workflow_and_linked_to_invocation():
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
operation_id = "wait-1"
plugin.on_operation_start(
OperationStartInfo(
operation_id=operation_id,
operation_type=OperationType.WAIT,
sub_type=OperationSubType.WAIT,
name="wait-for-signal",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
)
)
plugin.on_operation_end(
OperationEndInfo(
operation_id=operation_id,
operation_type=OperationType.WAIT,
sub_type=OperationSubType.WAIT,
name="wait-for-signal",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.SUCCEEDED,
end_time=END_TIME,
error=None,
)
)
plugin.on_invocation_end(_invocation_end_info())
spans = {s.name: s for s in exporter.get_finished_spans()}
workflow = spans["Workflow"]
invocation = spans["Invocation"]
operation = spans["wait-for-signal"]
# Parented under the Workflow span (no parentId => Workflow fallback).
assert operation.parent is not None
assert operation.parent.span_id == workflow.context.span_id
# Linked (not parented) to the invocation span.
linked_span_ids = {link.context.span_id for link in operation.links}
assert invocation.context.span_id in linked_span_ids
def test_cross_invocation_operation_end_uses_deterministic_span_id():
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
# Backend-updated completion for an operation started in a prior invocation.
plugin.on_operation_end(
OperationEndInfo(
operation_id="step-earlier",
operation_type=OperationType.STEP,
sub_type=None,
name="earlier-step",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.SUCCEEDED,
end_time=END_TIME,
error=None,
)
)
plugin.on_invocation_end(_invocation_end_info())
matching = [s for s in exporter.get_finished_spans() if s.name == "earlier-step"]
# Exported exactly once, using the deterministic logical-operation span ID
# (no separate continuation span).
assert len(matching) == 1
assert matching[0].context.span_id == operation_id_to_span_id(
EXECUTION_ARN, "step-earlier"
)
@pytest.mark.parametrize(
("outcome", "terminal_status", "error", "expected_span_status"),
[
(
UserFunctionOutcome.SUCCEEDED,
OperationStatus.SUCCEEDED,
None,
trace.StatusCode.OK,
),
(
UserFunctionOutcome.SUCCEEDED,
OperationStatus.FAILED,
ErrorObject(
message="serialization failed",
type="SerializationError",
data=None,
stack_trace=None,
),
trace.StatusCode.ERROR,
),
],
)
def test_context_span_waits_for_terminal_operation_status(
outcome,
terminal_status,
error,
expected_span_status,
):
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
operation_id = "context-1"
plugin.on_user_function_start(
UserFunctionStartInfo(
operation_id=operation_id,
operation_type=OperationType.CONTEXT,
sub_type=OperationSubType.RUN_IN_CHILD_CONTEXT,
name="book-trip",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=1,
)
)
active_span = plugin._get_span(operation_id)
assert active_span is not None
assert (
active_span.attributes["durable.operation.status"]
== OperationStatus.STARTED.value
)
plugin.on_user_function_end(
UserFunctionEndInfo(
operation_id=operation_id,
operation_type=OperationType.CONTEXT,
sub_type=OperationSubType.RUN_IN_CHILD_CONTEXT,
name="book-trip",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=1,
outcome=outcome,
end_time=END_TIME,
error=None,
)
)
assert plugin._get_span(operation_id) is active_span
assert (
active_span.attributes["durable.operation.status"]
== OperationStatus.STARTED.value
)
assert not exporter.get_finished_spans()
plugin.on_operation_end(
OperationEndInfo(
operation_id=operation_id,
operation_type=OperationType.CONTEXT,
sub_type=OperationSubType.RUN_IN_CHILD_CONTEXT,
name="book-trip",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=terminal_status,
end_time=END_TIME,
error=error,
)
)
span = exporter.get_finished_spans()[0]
assert span.attributes["durable.operation.status"] == terminal_status.value
assert span.status.status_code is expected_span_status
plugin.on_invocation_end(_invocation_end_info())
def test_step_attempt_span_omits_operation_status():
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
operation_id = "step-1"
plugin.on_user_function_start(
UserFunctionStartInfo(
operation_id=operation_id,
operation_type=OperationType.STEP,
sub_type=OperationSubType.STEP,
name="fetch-user",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=1,
)
)
active_span = plugin._get_span("step-1:attempt:1")
assert active_span is not None
assert "durable.operation.status" not in active_span.attributes
plugin.on_user_function_end(
UserFunctionEndInfo(
operation_id=operation_id,
operation_type=OperationType.STEP,
sub_type=OperationSubType.STEP,
name="fetch-user",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=1,
outcome=UserFunctionOutcome.SUCCEEDED,
end_time=END_TIME,
error=None,
)
)
span = exporter.get_finished_spans()[0]
assert (
span.attributes["durable.attempt.outcome"]
== UserFunctionOutcome.SUCCEEDED.value
)
assert "durable.operation.status" not in span.attributes
plugin.on_invocation_end(_invocation_end_info())
# ---------------------------------------------------------------------------
# Default-provider mode: invocation span
# ---------------------------------------------------------------------------
def _create_default_mode_plugin(
monkeypatch,
) -> tuple[ExecutionOtelPlugin, InMemorySpanExporter]:
"""ExecutionOtelPlugin in global (ADOT) mode wired to an in-memory exporter.
The capture provider is installed as the global provider so
the default configuration resolves to it, letting the test assert spans while
exercising the ambient-parenting path.
"""
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
monkeypatch.setattr(trace, "get_tracer_provider", lambda: provider)
plugin = ExecutionOtelPlugin(
OtelPluginConfig(
context_extractor=lambda _: Context(),
enrich_logger=False,
)
)
return plugin, exporter
def test_default_mode_creates_invocation_span(monkeypatch):
plugin, exporter = _create_default_mode_plugin(monkeypatch)
plugin.on_invocation_start(_invocation_start_info())
plugin.on_invocation_end(_invocation_end_info())
spans = {s.name: s for s in exporter.get_finished_spans()}
# The invocation span is now created even in default-provider mode.
assert "Invocation" in spans
invocation = spans["Invocation"]
assert invocation.attributes["durable.execution.arn"] == EXECUTION_ARN
assert invocation.attributes["durable.invocation.first"] is True
assert invocation.parent is None
assert invocation.context.trace_id != spans["Workflow"].context.trace_id
def test_default_mode_invocation_span_parented_to_ambient_span(monkeypatch):
plugin, exporter = _create_default_mode_plugin(monkeypatch)
# Simulate the ambient Lambda invocation span from the ADOT layer.
ambient = plugin._provider.get_tracer("ambient").start_span("lambda-invocation")
token = otel_context.attach(trace.set_span_in_context(ambient))
try:
plugin.on_invocation_start(_invocation_start_info())
plugin.on_invocation_end(_invocation_end_info())
finally:
otel_context.detach(token)
ambient.end()
invocation = {s.name: s for s in exporter.get_finished_spans()}["Invocation"]
assert invocation.parent is not None
assert invocation.parent.span_id == ambient.get_span_context().span_id
assert invocation.context.trace_id == ambient.get_span_context().trace_id
def test_open_operation_span_not_exported_at_invocation_end():
"""A suspended operation (started, not ended) must not be exported.
on_invocation_end drops the reference without ending it; the
span is ended only when on_operation_end fires in a later invocation.
"""
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
plugin.on_operation_start(
OperationStartInfo(
operation_id="wait-1",
operation_type=OperationType.WAIT,
sub_type=OperationSubType.WAIT,
name="wait-for-signal",
parent_id=None,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
)
)
# No on_operation_end: the operation suspended.
plugin.on_invocation_end(_invocation_end_info(status=InvocationStatus.PENDING))
exported = {s.name for s in exporter.get_finished_spans()}
# The open operation span is NOT exported (never ended).
assert "wait-for-signal" not in exported
@pytest.mark.parametrize(
("status", "expected_code"),
[
(InvocationStatus.PENDING, trace.StatusCode.OK),
(InvocationStatus.SUCCEEDED, trace.StatusCode.OK),
(InvocationStatus.RETRY, trace.StatusCode.UNSET),
(InvocationStatus.FAILED, trace.StatusCode.ERROR),
],
)
def test_invocation_span_status_kind_and_attributes(status, expected_code):
"""Invocation span is INTERNAL, carries status/first; SUCCEEDED/PENDING are
OK, FAILED is ERROR, and RETRY is UNSET (STOPPED/TIMED_OUT indistinguishable)."""
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
plugin.on_invocation_end(_invocation_end_info(status=status))
invocation = {s.name: s for s in exporter.get_finished_spans()}["Invocation"]
assert invocation.kind is trace.SpanKind.INTERNAL
assert invocation.attributes is not None
assert invocation.attributes["durable.invocation.status"] == status.value
assert invocation.attributes["durable.invocation.first"] is True
assert invocation.status.status_code is expected_code
# ---------------------------------------------------------------------------
# Context attach/detach balance across the plugin lifecycle
# ---------------------------------------------------------------------------
def _step_start_info(
operation_id: str,
parent_id: str | None = None,
attempt: int = 1,
) -> UserFunctionStartInfo:
return UserFunctionStartInfo(
operation_id=operation_id,
operation_type=OperationType.STEP,
sub_type=OperationSubType.STEP,
name=operation_id,
parent_id=parent_id,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=attempt,
)
def _step_end_info(
operation_id: str,
parent_id: str | None = None,
attempt: int = 1,
outcome: UserFunctionOutcome = UserFunctionOutcome.SUCCEEDED,
) -> UserFunctionEndInfo:
return UserFunctionEndInfo(
operation_id=operation_id,
operation_type=OperationType.STEP,
sub_type=OperationSubType.STEP,
name=operation_id,
parent_id=parent_id,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=attempt,
outcome=outcome,
end_time=END_TIME,
error=None,
)
def _step_incomplete_info(
operation_id: str,
parent_id: str | None = None,
attempt: int = 1,
) -> UserFunctionIncompleteInfo:
return UserFunctionIncompleteInfo(
operation_id=operation_id,
operation_type=OperationType.STEP,
sub_type=OperationSubType.STEP,
name=operation_id,
parent_id=parent_id,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=attempt,
)
def _context_incomplete_info(
operation_id: str, parent_id: str | None = None
) -> UserFunctionIncompleteInfo:
return UserFunctionIncompleteInfo(
operation_id=operation_id,
operation_type=OperationType.CONTEXT,
sub_type=OperationSubType.RUN_IN_CHILD_CONTEXT,
name=operation_id,
parent_id=parent_id,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=1,
)
def _context_start_info(
operation_id: str, parent_id: str | None = None
) -> UserFunctionStartInfo:
return UserFunctionStartInfo(
operation_id=operation_id,
operation_type=OperationType.CONTEXT,
sub_type=OperationSubType.RUN_IN_CHILD_CONTEXT,
name=operation_id,
parent_id=parent_id,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=1,
)
def _context_end_info(
operation_id: str, parent_id: str | None = None
) -> UserFunctionEndInfo:
return UserFunctionEndInfo(
operation_id=operation_id,
operation_type=OperationType.CONTEXT,
sub_type=OperationSubType.RUN_IN_CHILD_CONTEXT,
name=operation_id,
parent_id=parent_id,
start_time=START_TIME,
is_replayed=False,
status=OperationStatus.STARTED,
is_replay_children=False,
attempt=1,
outcome=UserFunctionOutcome.SUCCEEDED,
end_time=END_TIME,
error=None,
)
def test_workflow_span_is_current_during_the_invocation():
"""Verify the Workflow span is the active span while the invocation runs."""
plugin, _ = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
assert (
trace.get_current_span().get_span_context().span_id
== plugin._workflow_span.get_span_context().span_id
)
plugin.on_invocation_end(_invocation_end_info())
@pytest.mark.parametrize(
"status",
[InvocationStatus.PENDING, InvocationStatus.SUCCEEDED, InvocationStatus.FAILED],
)
def test_invocation_end_restores_context_from_before_invocation_start(status):
"""Verify invocation cleanup leaves no plugin span current.
A non-terminal invocation used to leave the Workflow span attached, so work
after cleanup was parented to a span that had not ended.
"""
plugin, _ = _create_plugin()
before_context = otel_context.get_current()
plugin.on_invocation_start(_invocation_start_info())
plugin.on_invocation_end(_invocation_end_info(status=status))
assert otel_context.get_current() == before_context
assert not trace.get_current_span().get_span_context().is_valid
assert plugin._context_tokens == {}
@pytest.mark.parametrize(
"outcome", [UserFunctionOutcome.SUCCEEDED, UserFunctionOutcome.FAILED]
)
def test_step_scope_is_released_at_user_function_end(outcome):
"""Verify a finished step restores the context that enclosed it."""
plugin, _ = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
enclosing_context = otel_context.get_current()
plugin.on_user_function_start(_step_start_info("step-1"))
attempt_span = plugin._get_span("step-1:attempt:1")
assert attempt_span is not None
assert (
trace.get_current_span().get_span_context().span_id
== attempt_span.get_span_context().span_id
)
plugin.on_user_function_end(_step_end_info("step-1", outcome=outcome))
assert otel_context.get_current() == enclosing_context
# Only the invocation-level scope remains open.
assert set(plugin._context_tokens) == {"__invocation_context__"}
plugin.on_invocation_end(_invocation_end_info())
assert plugin._context_tokens == {}
def test_sequential_steps_do_not_accumulate_scopes():
"""Verify repeated steps unwind to the same enclosing context each time."""
plugin, _ = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
enclosing_context = otel_context.get_current()
for index in range(3):
operation_id = f"step-{index}"
plugin.on_user_function_start(_step_start_info(operation_id))
plugin.on_user_function_end(_step_end_info(operation_id))
assert otel_context.get_current() == enclosing_context
assert set(plugin._context_tokens) == {"__invocation_context__"}
plugin.on_invocation_end(_invocation_end_info())
assert plugin._context_tokens == {}
def test_nested_scopes_are_released_without_accumulating():
"""Verify a child context and its inner step unwind to their entry contexts."""
plugin, _ = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
before_context = otel_context.get_current()
context_id = "ctx-1"
plugin.on_user_function_start(_context_start_info(context_id))
inside_context = otel_context.get_current()
context_span = plugin._get_span(context_id)
assert context_span is not None
plugin.on_user_function_start(_step_start_info("ctx-1-step", parent_id=context_id))
plugin.on_user_function_end(_step_end_info("ctx-1-step", parent_id=context_id))
# The inner step restored the child-context scope, not a copy of it.
assert otel_context.get_current() == inside_context
assert (
trace.get_current_span().get_span_context().span_id
== context_span.get_span_context().span_id
)
assert set(plugin._context_tokens) == {"__invocation_context__", context_id}
plugin.on_user_function_end(_context_end_info(context_id))
assert otel_context.get_current() == before_context
assert set(plugin._context_tokens) == {"__invocation_context__"}
plugin.on_invocation_end(_invocation_end_info())
assert plugin._context_tokens == {}
def test_invocation_end_releases_scope_of_suspended_user_function():
"""Verify a user function that never ends does not leak its scope.
A suspending user function raises before ``on_user_function_end`` runs, so
invocation cleanup is what releases the scope it attached.
"""
plugin, _ = _create_plugin()
before_context = otel_context.get_current()
plugin.on_invocation_start(_invocation_start_info())
plugin.on_user_function_start(_step_start_info("step-suspends"))
assert plugin._context_tokens
plugin.on_invocation_end(_invocation_end_info(status=InvocationStatus.PENDING))
assert otel_context.get_current() == before_context
assert plugin._context_tokens == {}
def test_warm_invocation_reuse_restores_ambient_span_each_time():
"""Verify repeated invocations leave the ambient Lambda span current."""
plugin, _ = _create_plugin()
ambient_provider = TracerProvider()
ambient = ambient_provider.get_tracer("ambient").start_span("AmbientLambda")
token = otel_context.attach(trace.set_span_in_context(ambient))
try:
warm_context = otel_context.get_current()
for index in range(3):
plugin.on_invocation_start(_invocation_start_info())
operation_id = f"step-{index}"
plugin.on_user_function_start(_step_start_info(operation_id))
plugin.on_user_function_end(_step_end_info(operation_id))
plugin.on_invocation_end(_invocation_end_info())
assert otel_context.get_current() == warm_context
assert (
trace.get_current_span().get_span_context().span_id
== ambient.get_span_context().span_id
)
assert plugin._context_tokens == {}
finally:
otel_context.detach(token)
ambient.end()
def test_detach_ignores_token_attached_on_another_thread():
"""Verify a scope attached on another thread is dropped, not reset here.
A context token can only be reset on the thread that created it, so the
plugin drops foreign tokens instead of asking OpenTelemetry to fail.
"""
plugin, _ = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
before_context = otel_context.get_current()
with ThreadPoolExecutor(max_workers=1) as executor:
executor.submit(
plugin.on_user_function_start, _step_start_info("step-1")
).result()
plugin._detach_context("step-1:attempt:1")
assert "step-1:attempt:1" not in plugin._context_tokens
assert otel_context.get_current() == before_context
plugin.on_invocation_end(_invocation_end_info())
# ---------------------------------------------------------------------------
# Re-entering an operation whose scope was never released
# ---------------------------------------------------------------------------
def test_reentered_child_context_does_not_leave_abandoned_span_current():
"""Verify a timed in-process resume unwinds the abandoned scope.
A suspended child context never reaches on_user_function_end, and the
map/parallel coordinator can resume that branch in-process, re-entering the
same operation ID on the same thread. Without releasing the first scope, the
second scope's detach would restore the abandoned span and leave it current.
"""
plugin, _ = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
before_context = otel_context.get_current()
context_id = "ctx-1"
# First run: the child context suspends, so no end hook fires.
plugin.on_user_function_start(_context_start_info(context_id))
suspended_span = plugin._get_span(context_id)
assert suspended_span is not None
# Timed in-process resume re-enters the same operation.
plugin.on_user_function_start(_context_start_info(context_id))
assert len([key for key in plugin._context_tokens if key == context_id]) == 1
plugin.on_user_function_end(_context_end_info(context_id))
assert otel_context.get_current() == before_context
assert context_id not in plugin._context_tokens
assert (
trace.get_current_span().get_span_context().span_id
!= suspended_span.get_span_context().span_id
)
plugin.on_invocation_end(_invocation_end_info())
def test_reentered_step_attempt_releases_the_previous_scope():
"""Verify re-entering the same attempt key unwinds the previous scope."""
plugin, _ = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
before_context = otel_context.get_current()
plugin.on_user_function_start(_step_start_info("step-1"))
plugin.on_user_function_start(_step_start_info("step-1"))
plugin.on_user_function_end(_step_end_info("step-1"))
assert otel_context.get_current() == before_context
assert set(plugin._context_tokens) == {"__invocation_context__"}
plugin.on_invocation_end(_invocation_end_info())
assert plugin._context_tokens == {}
def test_suspension_releases_the_scope_on_the_originating_worker():
"""Verify the suspending worker releases its own scope.
A suspended user function reports no outcome, so the SDK fires
on_user_function_incomplete on the thread that ran it -- the only thread that
can reset its context token. The worker is kept alive and probed to prove it
is left clean even though the resume lands on a different thread.
"""
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
before_context = otel_context.get_current()
span_key = "step-1:attempt:1"
with ThreadPoolExecutor(max_workers=1) as worker:
def suspend_on_worker() -> tuple[int, bool]:
plugin.on_user_function_start(_step_start_info("step-1"))
attached_span_id = trace.get_current_span().get_span_context().span_id
plugin.on_user_function_incomplete(_step_incomplete_info("step-1"))
return (
attached_span_id,
trace.get_current_span().get_span_context().is_valid,
)
attached_span_id, span_still_current = worker.submit(suspend_on_worker).result()
suspended_span = plugin._get_span(span_key)
# The scope was released on the worker, and its span is left open.
assert attached_span_id != 0
assert span_still_current is False
assert span_key not in plugin._context_tokens
assert suspended_span is not None
assert not exporter.get_finished_spans()
# The timed resume lands on this thread, with nothing stale to unwind.
plugin.on_user_function_start(_step_start_info("step-1"))
assert plugin._context_tokens[span_key][0] == threading.get_ident()
plugin.on_user_function_end(_step_end_info("step-1"))
assert otel_context.get_current() == before_context
# The originating worker is still clean.
worker_span_valid = worker.submit(
lambda: trace.get_current_span().get_span_context().is_valid
).result()
assert worker_span_valid is False
plugin.on_invocation_end(_invocation_end_info())
def test_nested_suspension_unwinds_scopes_in_reverse_order():
"""Verify nested suspends release inner-first and resume without stale scopes.
The incomplete hook fires as the exception propagates outward, so the inner
context's scope is released before its enclosing one. On resume, ending the
inner operation restores the resumed outer scope rather than the one captured
for the suspended run.
"""
plugin, exporter = _create_plugin()
plugin.on_invocation_start(_invocation_start_info())
before_context = otel_context.get_current()
plugin.on_user_function_start(_context_start_info("ctx-outer"))
suspended_outer = plugin._get_span("ctx-outer")
plugin.on_user_function_start(
_context_start_info("ctx-inner", parent_id="ctx-outer")
)
assert suspended_outer is not None
# Both contexts suspend: the inner one unwinds first.
plugin.on_user_function_incomplete(
_context_incomplete_info("ctx-inner", parent_id="ctx-outer")
)
assert trace.get_current_span() is suspended_outer
plugin.on_user_function_incomplete(_context_incomplete_info("ctx-outer"))
assert otel_context.get_current() == before_context
assert set(plugin._context_tokens) == {"__invocation_context__"}
# Neither span is ended: both operations are still in flight.
assert not exporter.get_finished_spans()
# The timed in-process resume replays both contexts, outer first.
plugin.on_user_function_start(_context_start_info("ctx-outer"))
resumed_outer = plugin._get_span("ctx-outer")
plugin.on_user_function_start(
_context_start_info("ctx-inner", parent_id="ctx-outer")
)
resumed_inner = plugin._get_span("ctx-inner")
assert resumed_outer is not None
assert resumed_outer is not suspended_outer
assert trace.get_current_span() is resumed_inner
plugin.on_user_function_end(_context_end_info("ctx-inner", parent_id="ctx-outer"))
# The resumed outer scope is restored, not the one from the suspended run.
assert trace.get_current_span() is resumed_outer
plugin.on_user_function_end(_context_end_info("ctx-outer"))