|
| 1 | +"""Exercise real installed version pairs through the public durable handler.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import contextvars |
| 6 | +from datetime import UTC, datetime |
| 7 | +from importlib.metadata import version |
| 8 | +import os |
| 9 | +from pathlib import Path |
| 10 | +import threading |
| 11 | +from types import SimpleNamespace |
| 12 | + |
| 13 | +import pytest |
| 14 | +from aws_durable_execution_sdk_python import durable_execution |
| 15 | +from aws_durable_execution_sdk_python import execution as core_execution |
| 16 | +from aws_durable_execution_sdk_python.lambda_service import ( |
| 17 | + ExecutionDetails, |
| 18 | + Operation, |
| 19 | + OperationStatus, |
| 20 | + OperationType, |
| 21 | +) |
| 22 | +from aws_durable_execution_sdk_python.plugin import DurableInstrumentationPlugin |
| 23 | +from aws_durable_execution_sdk_python_otel.execution_plugin import ExecutionOtelPlugin |
| 24 | +from aws_durable_execution_sdk_python_otel.invocation_plugin import InvocationOtelPlugin |
| 25 | +from aws_durable_execution_sdk_python_otel.otel_plugin_config import OtelPluginConfig |
| 26 | +from opentelemetry import baggage, context, trace |
| 27 | +from opentelemetry.sdk.trace import TracerProvider |
| 28 | +from opentelemetry.sdk.trace.export import SimpleSpanProcessor |
| 29 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter |
| 30 | +from packaging.version import Version |
| 31 | + |
| 32 | + |
| 33 | +class NoNetworkClient: |
| 34 | + def __getattr__(self, name: str): |
| 35 | + raise AssertionError(f"Unexpected service access: {name}") |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("view", [InvocationOtelPlugin, ExecutionOtelPlugin]) |
| 39 | +@pytest.mark.parametrize("order", ["alone", "baggage-first", "baggage-last"]) |
| 40 | +@pytest.mark.parametrize("ambient_present", [False, True]) |
| 41 | +def test_installed_pair_preserves_context_and_documents_legacy_fallback( |
| 42 | + view, order, ambient_present |
| 43 | +): |
| 44 | + legacy = os.environ.get("OTEL_COMPAT_LEGACY") == "1" |
| 45 | + assert Version(version("aws-durable-execution-sdk-python")) >= Version("2.1.0") |
| 46 | + assert version("aws-durable-execution-sdk-python-otel") == ( |
| 47 | + "1.0.0" if legacy else "1.1.0" |
| 48 | + ) |
| 49 | + assert "site-packages" in Path(core_execution.__file__).resolve().parts |
| 50 | + import aws_durable_execution_sdk_python_otel as installed_otel |
| 51 | + |
| 52 | + assert "site-packages" in Path(installed_otel.__file__).resolve().parts |
| 53 | + |
| 54 | + def run() -> None: |
| 55 | + provider = TracerProvider() |
| 56 | + exporter = InMemorySpanExporter() |
| 57 | + provider.add_span_processor(SimpleSpanProcessor(exporter)) |
| 58 | + tracer = provider.get_tracer("installed-lifecycle") |
| 59 | + phases = [] |
| 60 | + worker_threads = [] |
| 61 | + caller_thread = threading.get_ident() |
| 62 | + |
| 63 | + class BaggagePlugin(DurableInstrumentationPlugin): |
| 64 | + def on_invocation_start(self, info): |
| 65 | + self.token = context.attach(baggage.set_baggage("customer", "present")) |
| 66 | + phases.append("baggage-start") |
| 67 | + worker_threads.append(threading.get_ident()) |
| 68 | + |
| 69 | + def on_invocation_end(self, info): |
| 70 | + context.detach(self.token) |
| 71 | + phases.append("baggage-end") |
| 72 | + worker_threads.append(threading.get_ident()) |
| 73 | + |
| 74 | + plugin = view( |
| 75 | + OtelPluginConfig( |
| 76 | + tracer_provider=provider, |
| 77 | + enrich_logger=False, |
| 78 | + context_extractor=lambda _: None, |
| 79 | + ) |
| 80 | + ) |
| 81 | + plugins = { |
| 82 | + "alone": [plugin], |
| 83 | + "baggage-first": [BaggagePlugin(), plugin], |
| 84 | + "baggage-last": [plugin, BaggagePlugin()], |
| 85 | + }[order] |
| 86 | + body_parents = [] |
| 87 | + |
| 88 | + @durable_execution(plugins=plugins, boto3_client=NoNetworkClient()) |
| 89 | + def handler(event, durable_context): |
| 90 | + phases.append("body") |
| 91 | + worker_threads.append(threading.get_ident()) |
| 92 | + assert baggage.get_baggage("incoming") == "keep" |
| 93 | + assert baggage.get_baggage("customer") == ( |
| 94 | + None if order == "alone" else "present" |
| 95 | + ) |
| 96 | + parent = trace.get_current_span().get_span_context() |
| 97 | + body_parents.append(parent) |
| 98 | + with tracer.start_as_current_span("customer-span"): |
| 99 | + pass |
| 100 | + return "ok" |
| 101 | + |
| 102 | + operation = Operation( |
| 103 | + operation_id="installed", |
| 104 | + operation_type=OperationType.EXECUTION, |
| 105 | + status=OperationStatus.STARTED, |
| 106 | + start_timestamp=datetime(2026, 10, 8, tzinfo=UTC), |
| 107 | + execution_details=ExecutionDetails(input_payload="{}"), |
| 108 | + ) |
| 109 | + event = { |
| 110 | + "DurableExecutionArn": "test-arn/installed", |
| 111 | + "CheckpointToken": "token", |
| 112 | + "InitialExecutionState": { |
| 113 | + "Operations": [operation.to_json_dict()], |
| 114 | + "NextMarker": "", |
| 115 | + }, |
| 116 | + } |
| 117 | + lambda_context = SimpleNamespace( |
| 118 | + aws_request_id="installed", |
| 119 | + client_context=None, |
| 120 | + identity=None, |
| 121 | + _epoch_deadline_time_in_ms=0, |
| 122 | + invoked_function_arn="test-arn", |
| 123 | + tenant_id=None, |
| 124 | + ) |
| 125 | + ambient = tracer.start_span("host") if ambient_present else trace.INVALID_SPAN |
| 126 | + host = baggage.set_baggage( |
| 127 | + "incoming", "keep", trace.set_span_in_context(ambient) |
| 128 | + ) |
| 129 | + token = context.attach(host) |
| 130 | + try: |
| 131 | + for _ in range(2): |
| 132 | + assert handler(event, lambda_context)["Status"] == "SUCCEEDED" |
| 133 | + assert context.get_current() is host |
| 134 | + assert caller_thread not in worker_threads |
| 135 | + assert phases == ( |
| 136 | + ["body"] * 2 |
| 137 | + if order == "alone" |
| 138 | + else ["baggage-start", "body", "baggage-end"] * 2 |
| 139 | + ) |
| 140 | + spans = exporter.get_finished_spans() |
| 141 | + if legacy and view is InvocationOtelPlugin: |
| 142 | + # The released plugin does not attach an Invocation fallback. |
| 143 | + assert body_parents == [ambient.get_span_context()] * 2 |
| 144 | + else: |
| 145 | + name = "Invocation" if view is InvocationOtelPlugin else "Workflow" |
| 146 | + contexts = [span.context for span in spans if span.name == name] |
| 147 | + assert contexts |
| 148 | + assert all( |
| 149 | + parent.is_valid and parent in contexts for parent in body_parents |
| 150 | + ) |
| 151 | + users = [span for span in spans if span.name == "customer-span"] |
| 152 | + assert len(users) == 2 |
| 153 | + assert [span.parent for span in users] == [ |
| 154 | + parent if parent.is_valid else None for parent in body_parents |
| 155 | + ] |
| 156 | + assert plugin._context_tokens == {} |
| 157 | + finally: |
| 158 | + context.detach(token) |
| 159 | + ambient.end() |
| 160 | + provider.shutdown() |
| 161 | + |
| 162 | + contextvars.Context().run(run) |
0 commit comments