|
| 1 | +"""Shared status regressions through both public plugin lifecycles.""" |
| 2 | + |
| 3 | +from collections.abc import Iterator |
| 4 | +from datetime import UTC, datetime, timedelta |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import pytest |
| 8 | +from aws_durable_execution_sdk_python.lambda_service import ( |
| 9 | + ErrorObject, |
| 10 | + OperationStatus, |
| 11 | + OperationSubType, |
| 12 | +) |
| 13 | +from aws_durable_execution_sdk_python.plugin import ( |
| 14 | + InvocationEndInfo, |
| 15 | + InvocationStartInfo, |
| 16 | + InvocationStatus, |
| 17 | + OperationEndInfo, |
| 18 | + OperationStartInfo, |
| 19 | + OperationType, |
| 20 | +) |
| 21 | +from opentelemetry import context |
| 22 | +from opentelemetry.sdk.trace import TracerProvider |
| 23 | +from opentelemetry.sdk.trace.export import SimpleSpanProcessor |
| 24 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter |
| 25 | +from opentelemetry.trace import StatusCode |
| 26 | + |
| 27 | +from aws_durable_execution_sdk_python_otel import ( |
| 28 | + ExecutionOtelPlugin, |
| 29 | + InvocationOtelPlugin, |
| 30 | + OtelPluginConfig, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +START = datetime(2026, 10, 1, tzinfo=UTC) |
| 35 | +END = START + timedelta(seconds=1) |
| 36 | +ARN = ( |
| 37 | + "arn:aws:lambda:us-west-2:123456789012:function:status:1/durable-execution/test/id" |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +InstrumentedView = tuple[ |
| 42 | + ExecutionOtelPlugin | InvocationOtelPlugin, InMemorySpanExporter |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(params=[ExecutionOtelPlugin, InvocationOtelPlugin]) |
| 47 | +def instrumented_view(request: pytest.FixtureRequest) -> Iterator[InstrumentedView]: |
| 48 | + exporter = InMemorySpanExporter() |
| 49 | + provider = TracerProvider() |
| 50 | + provider.add_span_processor(SimpleSpanProcessor(exporter)) |
| 51 | + plugin = request.param( |
| 52 | + OtelPluginConfig( |
| 53 | + tracer_provider=provider, |
| 54 | + context_extractor=lambda _: None, |
| 55 | + enrich_logger=False, |
| 56 | + ) |
| 57 | + ) |
| 58 | + original = context.get_current() |
| 59 | + yield plugin, exporter |
| 60 | + assert context.get_current() == original |
| 61 | + provider.shutdown() |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "status", |
| 66 | + [ |
| 67 | + OperationStatus.SUCCEEDED, |
| 68 | + OperationStatus.FAILED, |
| 69 | + OperationStatus.CANCELLED, |
| 70 | + OperationStatus.TIMED_OUT, |
| 71 | + OperationStatus.STOPPED, |
| 72 | + ], |
| 73 | +) |
| 74 | +@pytest.mark.parametrize("with_error", [False, True]) |
| 75 | +@pytest.mark.parametrize("started_here", [False, True]) |
| 76 | +def test_operation_status_and_exception( |
| 77 | + instrumented_view: InstrumentedView, |
| 78 | + status: OperationStatus, |
| 79 | + with_error: bool, |
| 80 | + started_here: bool, |
| 81 | +) -> None: |
| 82 | + """A delivered terminal hook must not label an unsuccessful operation OK.""" |
| 83 | + plugin, exporter = instrumented_view |
| 84 | + invocation: dict[str, Any] = dict( |
| 85 | + request_id="first", |
| 86 | + execution_arn=ARN, |
| 87 | + execution_start_time=START, |
| 88 | + is_first_invocation=True, |
| 89 | + ) |
| 90 | + plugin.on_invocation_start(InvocationStartInfo(**invocation)) |
| 91 | + operation: dict[str, Any] = dict( |
| 92 | + operation_id="wait", |
| 93 | + name="wait", |
| 94 | + parent_id=None, |
| 95 | + operation_type=OperationType.WAIT, |
| 96 | + sub_type=OperationSubType.WAIT, |
| 97 | + start_time=START, |
| 98 | + is_replayed=False, |
| 99 | + ) |
| 100 | + if started_here: |
| 101 | + plugin.on_operation_start( |
| 102 | + OperationStartInfo(**operation, status=OperationStatus.STARTED) |
| 103 | + ) |
| 104 | + error = ( |
| 105 | + ErrorObject( |
| 106 | + message="deadline exceeded", |
| 107 | + type="TimeoutError", |
| 108 | + data=None, |
| 109 | + stack_trace=None, |
| 110 | + ) |
| 111 | + if with_error |
| 112 | + else None |
| 113 | + ) |
| 114 | + plugin.on_operation_end( |
| 115 | + OperationEndInfo(**operation, end_time=END, status=status, error=error) |
| 116 | + ) |
| 117 | + plugin.on_invocation_end( |
| 118 | + InvocationEndInfo(**invocation, status=InvocationStatus.SUCCEEDED) |
| 119 | + ) |
| 120 | + span = next(s for s in exporter.get_finished_spans() if s.name == "wait") |
| 121 | + assert span.attributes is not None |
| 122 | + assert span.attributes["durable.operation.status"] == status.value |
| 123 | + expected = ( |
| 124 | + StatusCode.ERROR |
| 125 | + if with_error |
| 126 | + else ( |
| 127 | + StatusCode.OK if status is OperationStatus.SUCCEEDED else StatusCode.UNSET |
| 128 | + ) |
| 129 | + ) |
| 130 | + assert span.status.status_code is expected |
| 131 | + if with_error: |
| 132 | + assert span.status.description == "deadline exceeded" |
| 133 | + assert len(span.events) == 1 |
| 134 | + assert span.events[0].name == "exception" |
| 135 | + assert span.events[0].attributes is not None |
| 136 | + assert span.events[0].attributes["exception.message"] == "deadline exceeded" |
| 137 | + else: |
| 138 | + assert not span.events |
| 139 | + |
| 140 | + |
| 141 | +def test_retry_then_resume_exports_retrying( |
| 142 | + instrumented_view: InstrumentedView, |
| 143 | +) -> None: |
| 144 | + """Telemetry normalizes RETRY without changing the core enum or next invocation.""" |
| 145 | + plugin, exporter = instrumented_view |
| 146 | + original = context.get_current() |
| 147 | + for index, status in enumerate( |
| 148 | + [InvocationStatus.RETRY, InvocationStatus.SUCCEEDED] |
| 149 | + ): |
| 150 | + invocation: dict[str, Any] = dict( |
| 151 | + request_id=f"request-{index}", |
| 152 | + execution_arn=ARN, |
| 153 | + execution_start_time=START, |
| 154 | + is_first_invocation=index == 0, |
| 155 | + ) |
| 156 | + plugin.on_invocation_start(InvocationStartInfo(**invocation)) |
| 157 | + plugin.on_invocation_end(InvocationEndInfo(**invocation, status=status)) |
| 158 | + assert context.get_current() == original |
| 159 | + spans = [s for s in exporter.get_finished_spans() if s.name == "Invocation"] |
| 160 | + assert [ |
| 161 | + s.attributes["durable.invocation.status"] |
| 162 | + for s in spans |
| 163 | + if s.attributes is not None |
| 164 | + ] == [ |
| 165 | + "RETRYING", |
| 166 | + "SUCCEEDED", |
| 167 | + ] |
| 168 | + assert [s.status.status_code for s in spans] == [StatusCode.UNSET, StatusCode.OK] |
| 169 | + assert InvocationStatus.RETRY.value == "RETRY" |
| 170 | + assert spans[0].context is not None |
| 171 | + assert spans[1].context is not None |
| 172 | + assert spans[0].context.trace_id == spans[1].context.trace_id |
0 commit comments