From 9a15b2ba73ba5560033d5d34d4f89ff2b35422a3 Mon Sep 17 00:00:00 2001 From: nandanadileep <116407139+nandanadileep@users.noreply.github.com> Date: Sat, 31 Oct 2026 01:04:52 +0000 Subject: [PATCH 1/2] fix: keep exclusive plugin groups for wrappers --- .../execution_plugin.py | 5 +- .../invocation_plugin.py | 5 +- .../tests/e2e/test_view_registration.py | 103 +++++++++++++++++- .../plugin.py | 95 ++++++++++++++++ .../plugin_discovery.py | 92 +--------------- 5 files changed, 208 insertions(+), 92 deletions(-) diff --git a/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/execution_plugin.py b/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/execution_plugin.py index 356990ede..4df9c68ab 100644 --- a/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/execution_plugin.py +++ b/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/execution_plugin.py @@ -34,7 +34,7 @@ import datetime import logging import threading -from typing import Any, TYPE_CHECKING, cast +from typing import Any, ClassVar, TYPE_CHECKING, cast from aws_durable_execution_sdk_python.plugin import ( DurableInstrumentationPlugin, @@ -155,6 +155,9 @@ class ExecutionOtelPlugin(DurableInstrumentationPlugin): span). """ + __durable_registration_api__: ClassVar[int] = 1 + exclusive_group: ClassVar[str | None] = "aws-durable-execution-otel-view" + def __init__(self, config: OtelPluginConfig | None = None) -> None: self._config = config or OtelPluginConfig() self._context_extractor: ContextExtractor = ( diff --git a/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/invocation_plugin.py b/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/invocation_plugin.py index 4eab6c7c3..bc2601f96 100644 --- a/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/invocation_plugin.py +++ b/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/invocation_plugin.py @@ -5,7 +5,7 @@ import datetime import logging import threading -from typing import Any, TYPE_CHECKING, cast +from typing import Any, ClassVar, TYPE_CHECKING, cast from aws_durable_execution_sdk_python.plugin import ( DurableInstrumentationPlugin, @@ -134,6 +134,9 @@ class InvocationOtelPlugin(DurableInstrumentationPlugin): provider installed by the ADOT Lambda layer). """ + __durable_registration_api__: ClassVar[int] = 1 + exclusive_group: ClassVar[str | None] = "aws-durable-execution-otel-view" + DEFAULT_INSTRUMENT_NAME = "aws-durable-execution-sdk-python" def __init__(self, config: OtelPluginConfig | None = None) -> None: diff --git a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_view_registration.py b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_view_registration.py index 7ec2b696d..09fecbbe6 100644 --- a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_view_registration.py +++ b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_view_registration.py @@ -4,12 +4,24 @@ from types import SimpleNamespace from collections.abc import Iterator from typing import Any, cast +from unittest.mock import Mock import pytest from aws_durable_execution_sdk_python import DurableContext from aws_durable_execution_sdk_python.config import Duration from aws_durable_execution_sdk_python.exceptions import PluginLoadError -from aws_durable_execution_sdk_python.execution import durable_execution +from aws_durable_execution_sdk_python.execution import ( + durable_execution, + DurableExecutionInvocationInputWithClient, + InitialExecutionState, +) +from aws_durable_execution_sdk_python.lambda_service import ( + DurableServiceClient, + ExecutionDetails, + Operation, + OperationStatus, + OperationType, +) from aws_durable_execution_sdk_python.plugin import ( DurableInstrumentationPlugin, DurableInstrumentationPluginFactory, @@ -51,6 +63,95 @@ def _handler(_event: Any, _context: DurableContext) -> str: return "unused" +def _invoke_once(configured: Any) -> Any: + """Call a decorated handler once against an injected in-process service client. + + The local runner retries an invocation whose handler raises, turning a + registration conflict raised at invocation start into a timeout; calling the + handler directly keeps the raised error observable. + """ + operation = Operation( + operation_id="exec1", + operation_type=OperationType.EXECUTION, + status=OperationStatus.STARTED, + execution_details=ExecutionDetails(input_payload="{}"), + ) + invocation_input = DurableExecutionInvocationInputWithClient( + durable_execution_arn="arn:test:execution/exec1", + checkpoint_token="token", # noqa: S106 - test fixture value + initial_execution_state=InitialExecutionState( + operations=[operation], next_marker="" + ), + service_client=Mock(spec=DurableServiceClient), + ) + lambda_context = Mock( + aws_request_id="test-request", + client_context=None, + identity=None, + _epoch_deadline_time_in_ms=1_000_000, + invoked_function_arn=None, + tenant_id=None, + ) + return configured(invocation_input, lambda_context) + + +class _DelegatingFactory: + """A user factory delegating to a built-in one, carrying no registration marker. + + The reported shape: ``create_plugin`` and nothing else, so its class has no + marker even though the plugin it returns does. + """ + + def __init__(self, delegate: Any) -> None: self._delegate = delegate + + def create_plugin(self, info: InvocationStartInfo) -> DurableInstrumentationPlugin: + return self._delegate.create_plugin(info) + + +@pytest.mark.parametrize("wrapped", ["execution", "invocation", "both"]) +@pytest.mark.parametrize("reverse", [False, True]) +def test_wrapper_factory_cannot_bypass_view_exclusion( + telemetry: tuple[TracerProvider, InMemorySpanExporter], + wrapped: str, + reverse: bool, +) -> None: + provider, exporter = telemetry + config = OtelPluginConfig(tracer_provider=provider, enrich_logger=False) + execution = ExecutionOtelPluginFactory(config) + invocation = InvocationOtelPluginFactory(config) + plugins: list[DurableInstrumentationPluginFactory] = [ + _DelegatingFactory(execution), + _DelegatingFactory(invocation), + ] + if wrapped == "execution": + plugins[1] = invocation + elif wrapped == "invocation": + plugins[0] = execution + if reverse: + plugins.reverse() + + configured = durable_execution(_handler, plugins=plugins) + with pytest.raises(PluginLoadError) as error: + _invoke_once(configured) + assert "ExecutionOtelPlugin" in str(error.value) + assert "InvocationOtelPlugin" in str(error.value) + assert "Keep only one" in str(error.value) + assert not exporter.get_finished_spans() + + +def test_single_wrapped_view_with_unrelated_plugin_is_accepted( + telemetry: tuple[TracerProvider, InMemorySpanExporter], +) -> None: + provider, exporter = telemetry + config = OtelPluginConfig(tracer_provider=provider, enrich_logger=False) + execution = ExecutionOtelPluginFactory(config) + configured = durable_execution( + _handler, plugins=[_DelegatingFactory(execution), _ObserverFactory()] + ) + assert _invoke_once(configured)["Status"] == "SUCCEEDED" + assert not exporter.get_finished_spans() + + @pytest.mark.parametrize("registration", ["explicit", "environment", "mixed"]) @pytest.mark.parametrize("reverse", [False, True]) def test_competing_views_rejected_before_hooks( diff --git a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin.py b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin.py index d23b6aad9..4f921b372 100644 --- a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin.py +++ b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin.py @@ -12,6 +12,7 @@ from enum import Enum from typing import Any, Callable, MutableMapping, Protocol, cast +from aws_durable_execution_sdk_python.exceptions import PluginLoadError from aws_durable_execution_sdk_python.identifier import OperationIdentifier from aws_durable_execution_sdk_python.lambda_service import ( DurableExecutionInvocationOutput, @@ -719,6 +720,94 @@ def _factory_name(factory: object) -> str: return _type_name(factory) +def _module_qualified_name(named: object) -> str: + """Join a class's or function's module and qualified name. + + Both attributes are read with a default, because a rejected entry can be any + object at all, and a missing name must not become an ``AttributeError``. + """ + module = getattr(named, "__module__", None) or "unknown module" + qualified = getattr(named, "__qualname__", None) or getattr(named, "__name__", None) + return f"{module}.{qualified or ''}" + + +def _registration_api_owner(plugin_type: type[object]) -> type[object] | None: + """Find the first explicit marker in the MRO without metaclass descriptors. + + A subclass inherits its declaring class's registration contract. Its legacy + same-named fields/helpers are not part of that contract unless it repeats + the marker. An explicit unsupported marker shadows any inherited opt-in. + """ + mro: tuple[type[object], ...] = type.__dict__["__mro__"].__get__( + plugin_type, type(plugin_type) + ) + for declaring_type in mro: + namespace = type.__dict__["__dict__"].__get__( + declaring_type, type(declaring_type) + ) + if "__durable_registration_api__" in namespace: + version = namespace["__durable_registration_api__"] + return declaring_type if type(version) is int and version == 1 else None + return None + + +def _exclusive_groups(plugin_type: type[object]) -> list[str]: + """Accumulate opted-in MRO contracts without interpreting legacy fields.""" + mro: tuple[type[object], ...] = type.__dict__["__mro__"].__get__( + plugin_type, type(plugin_type) + ) + groups: list[str] = [] + for declaring_type in mro: + namespace = type.__dict__["__dict__"].__get__( + declaring_type, type(declaring_type) + ) + version = namespace.get("__durable_registration_api__") + if type(version) is not int or version != 1: + continue + try: + group = getattr(declaring_type, "exclusive_group", None) + except Exception as error: + raise PluginLoadError( + f"Cannot read exclusive_group for {_module_qualified_name(plugin_type)}." + ) from error + if group is None: + continue + if type(group) is not str or not group.strip(): + raise PluginLoadError( + f"Durable instrumentation plugin {_module_qualified_name(plugin_type)} " + "must declare exclusive_group as None or a non-empty string." + ) + # Repeating a marker or a group does not register this one plugin twice. + if group not in groups: + groups.append(group) + return groups + + +def _validate_exclusive_groups( + plugin_types: Sequence[type[object]], +) -> None: + """Reject competing instrumentation before any lifecycle hooks run.""" + groups: dict[str, type[object]] = {} + for plugin_type in plugin_types: + if _registration_api_owner(plugin_type) is None: + continue + for group in _exclusive_groups(plugin_type): + if (previous := groups.get(group)) is not None: + if previous is plugin_type: + raise PluginLoadError( + f"Durable instrumentation plugin {_module_qualified_name(plugin_type)} " + f"is registered more than once in exclusive group '{group}'. " + "Register this plugin only once in plugins." + ) + raise PluginLoadError( + f"Durable instrumentation plugins {_module_qualified_name(previous)} " + f"and {_module_qualified_name(plugin_type)} are mutually exclusive " + f"(group '{group}'). Keep only one plugin from this group in " + "plugins and DURABLE_EXECUTION_PLUGINS." + ) + groups[group] = plugin_type + + # Raised out of plugin code, these three are not reports of a plugin defect but # instructions to the thread that is running: stop. Containing one would drop the # instruction and return a thread that was told to unwind to the work after the @@ -1448,6 +1537,12 @@ def _create_plugins(self, info: InvocationStartInfo) -> None: ) continue plugins.append(plugin) + # The returned plugin is the actual instrumentation, and its concrete + # type may declare an exclusive group even when the factory that built it + # does not -- a delegating wrapper carries no marker of its own. Check + # the returned types before any hook is dispatched, and outside the + # containment above: a conflict is configuration, not a plugin failure. + _validate_exclusive_groups([type(plugin) for plugin in plugins]) self._plugins = plugins @staticmethod diff --git a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin_discovery.py b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin_discovery.py index 67c570ae7..7e2e69199 100644 --- a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin_discovery.py +++ b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin_discovery.py @@ -10,6 +10,9 @@ from aws_durable_execution_sdk_python.exceptions import PluginLoadError from aws_durable_execution_sdk_python.plugin import ( DurableInstrumentationPluginFactory, + _module_qualified_name, + _registration_api_owner, + _validate_exclusive_groups, ) @@ -54,18 +57,6 @@ def _distribution_name(entry_point: metadata.EntryPoint) -> str: return distribution.metadata.get("Name", "unknown distribution") -def _module_qualified_name(named: object) -> str: - """Join a class's or function's module and qualified name. - - Both attributes are read with a default, because a rejected entry can be any - object at all. A missing name must not replace the configuration error with an - ``AttributeError``. - """ - module = getattr(named, "__module__", None) or "unknown module" - qualified = getattr(named, "__qualname__", None) or getattr(named, "__name__", None) - return f"{module}.{qualified or ''}" - - def _describe_value(value: object) -> str: """Describe a rejected registration entry so the caller can identify it. @@ -286,26 +277,6 @@ def _factory_type(factory: object) -> type[object]: ) -def _registration_api_owner(plugin_type: type[object]) -> type[object] | None: - """Find the first explicit marker in the MRO without metaclass descriptors. - - A subclass inherits its declaring class's registration contract. Its legacy - same-named fields/helpers are not part of that contract unless it repeats - the marker. An explicit unsupported marker shadows any inherited opt-in. - """ - mro: tuple[type[object], ...] = type.__dict__["__mro__"].__get__( - plugin_type, type(plugin_type) - ) - for declaring_type in mro: - namespace = type.__dict__["__dict__"].__get__( - declaring_type, type(declaring_type) - ) - if "__durable_registration_api__" in namespace: - version = namespace["__durable_registration_api__"] - return declaring_type if type(version) is int and version == 1 else None - return None - - def _registration_result_callback(plugin: object, owner: type[object]) -> object | None: """Bind the opted-in contract without calling a legacy subclass helper.""" if owner is _factory_type(plugin): @@ -322,63 +293,6 @@ def _registration_result_callback(plugin: object, owner: type[object]) -> object return None -def _exclusive_groups(plugin_type: type[object]) -> list[str]: - """Accumulate opted-in MRO contracts without interpreting legacy fields.""" - mro: tuple[type[object], ...] = type.__dict__["__mro__"].__get__( - plugin_type, type(plugin_type) - ) - groups: list[str] = [] - for declaring_type in mro: - namespace = type.__dict__["__dict__"].__get__( - declaring_type, type(declaring_type) - ) - version = namespace.get("__durable_registration_api__") - if type(version) is not int or version != 1: - continue - try: - group = getattr(declaring_type, "exclusive_group", None) - except Exception as error: - raise PluginLoadError( - f"Cannot read exclusive_group for {_module_qualified_name(plugin_type)}." - ) from error - if group is None: - continue - if type(group) is not str or not group.strip(): - raise PluginLoadError( - f"Durable instrumentation plugin {_module_qualified_name(plugin_type)} " - "must declare exclusive_group as None or a non-empty string." - ) - # Repeating a marker or a group does not register this one plugin twice. - if group not in groups: - groups.append(group) - return groups - - -def _validate_exclusive_groups( - plugin_types: Sequence[type[object]], -) -> None: - """Reject competing instrumentation before any lifecycle hooks run.""" - groups: dict[str, type[object]] = {} - for plugin_type in plugin_types: - if _registration_api_owner(plugin_type) is None: - continue - for group in _exclusive_groups(plugin_type): - if (previous := groups.get(group)) is not None: - if previous is plugin_type: - raise PluginLoadError( - f"Durable instrumentation plugin {_module_qualified_name(plugin_type)} " - f"is registered more than once in exclusive group '{group}'. " - "Register this plugin only once in plugins." - ) - raise PluginLoadError( - f"Durable instrumentation plugins {_module_qualified_name(previous)} " - f"and {_module_qualified_name(plugin_type)} are mutually exclusive " - f"(group '{group}'). Keep only one plugin from this group in " - "plugins and DURABLE_EXECUTION_PLUGINS." - ) - groups[group] = plugin_type - - def _notify_registration_result( plugins: Sequence[object], *, From 3c8b08f05fc7b18474899d96b9172452af9b66f2 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 3 Nov 2026 03:18:25 +0000 Subject: [PATCH 2/2] test: keep OTel wrapper registration metadata OTel test factory wrappers omitted the built-in factories' registration marker and exclusive group, so competing views were not rejected at handler registration. - Restore production plugin and OTel classes to match main - Inherit or declare registration metadata on OTel wrappers - Reject competing wrapped views at durable_execution time Co-authored-by: Nandana Dileep --- .../execution_plugin.py | 5 +- .../invocation_plugin.py | 5 +- .../tests/e2e/test_branch_context_int.py | 18 +- .../tests/e2e/test_fallback_root_lifecycle.py | 20 +- .../e2e/test_invocation_wait_resume_int.py | 18 +- .../tests/e2e/test_view_registration.py | 219 ++++++++++++------ .../plugin.py | 95 -------- .../plugin_discovery.py | 92 +++++++- 8 files changed, 295 insertions(+), 177 deletions(-) diff --git a/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/execution_plugin.py b/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/execution_plugin.py index 4df9c68ab..356990ede 100644 --- a/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/execution_plugin.py +++ b/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/execution_plugin.py @@ -34,7 +34,7 @@ import datetime import logging import threading -from typing import Any, ClassVar, TYPE_CHECKING, cast +from typing import Any, TYPE_CHECKING, cast from aws_durable_execution_sdk_python.plugin import ( DurableInstrumentationPlugin, @@ -155,9 +155,6 @@ class ExecutionOtelPlugin(DurableInstrumentationPlugin): span). """ - __durable_registration_api__: ClassVar[int] = 1 - exclusive_group: ClassVar[str | None] = "aws-durable-execution-otel-view" - def __init__(self, config: OtelPluginConfig | None = None) -> None: self._config = config or OtelPluginConfig() self._context_extractor: ContextExtractor = ( diff --git a/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/invocation_plugin.py b/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/invocation_plugin.py index bc2601f96..4eab6c7c3 100644 --- a/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/invocation_plugin.py +++ b/packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/invocation_plugin.py @@ -5,7 +5,7 @@ import datetime import logging import threading -from typing import Any, ClassVar, TYPE_CHECKING, cast +from typing import Any, TYPE_CHECKING, cast from aws_durable_execution_sdk_python.plugin import ( DurableInstrumentationPlugin, @@ -134,9 +134,6 @@ class InvocationOtelPlugin(DurableInstrumentationPlugin): provider installed by the ADOT Lambda layer). """ - __durable_registration_api__: ClassVar[int] = 1 - exclusive_group: ClassVar[str | None] = "aws-durable-execution-otel-view" - DEFAULT_INSTRUMENT_NAME = "aws-durable-execution-sdk-python" def __init__(self, config: OtelPluginConfig | None = None) -> None: diff --git a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_branch_context_int.py b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_branch_context_int.py index 821581574..7f42f5c6a 100644 --- a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_branch_context_int.py +++ b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_branch_context_int.py @@ -45,6 +45,22 @@ def create_plugin(self, info: InvocationStartInfo) -> _PluginT: return plugin +class _RecordingOtelFactory(Generic[_PluginT]): + """OTel-specific recording wrapper that keeps factory registration metadata.""" + + __durable_registration_api__ = 1 + exclusive_group = "aws-durable-execution-otel-view" + + def __init__(self, build: Callable[[], _PluginT]) -> None: + self.build = build + self.instances: list[_PluginT] = [] + + def create_plugin(self, info: InvocationStartInfo) -> _PluginT: + plugin = self.build() + self.instances.append(plugin) + return plugin + + @pytest.mark.parametrize( ("view", "failed_start"), [ @@ -111,7 +127,7 @@ def on_invocation_start(self, _info: Any) -> None: plugins.append(_RecordingFactory(Bind)) if failed_start: plugins.append(_RecordingFactory(Broken)) - otel_factory = _RecordingFactory( + otel_factory = _RecordingOtelFactory( lambda: view( OtelPluginConfig( tracer_provider=provider, diff --git a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_fallback_root_lifecycle.py b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_fallback_root_lifecycle.py index a67995562..bbb64d898 100644 --- a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_fallback_root_lifecycle.py +++ b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_fallback_root_lifecycle.py @@ -45,6 +45,22 @@ def create_plugin(self, info: InvocationStartInfo) -> _PluginT: return plugin +class _RecordingOtelFactory(Generic[_PluginT]): + """OTel-specific recording wrapper that keeps factory registration metadata.""" + + __durable_registration_api__ = 1 + exclusive_group = "aws-durable-execution-otel-view" + + def __init__(self, build: Callable[[], _PluginT]) -> None: + self.build = build + self.instances: list[_PluginT] = [] + + def create_plugin(self, info: InvocationStartInfo) -> _PluginT: + plugin = self.build() + self.instances.append(plugin) + return plugin + + class _CaptureInvocationEnd(DurableInstrumentationPlugin): def __init__(self, exporter: InMemorySpanExporter) -> None: self.exporter = exporter @@ -65,7 +81,7 @@ def test_fallback_anchor_survives_suspension( provider.add_span_processor( BatchSpanProcessor(exporter, schedule_delay_millis=60000) ) - factory = _RecordingFactory( + factory = _RecordingOtelFactory( lambda: plugin_type( OtelPluginConfig( tracer_provider=provider, @@ -177,7 +193,7 @@ def get_description(self) -> str: provider.add_span_processor( BatchSpanProcessor(exporter, schedule_delay_millis=60000) ) - factory = _RecordingFactory( + factory = _RecordingOtelFactory( lambda: plugin_type( OtelPluginConfig( tracer_provider=provider, diff --git a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_invocation_wait_resume_int.py b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_invocation_wait_resume_int.py index bf1365fb9..f35a045c8 100644 --- a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_invocation_wait_resume_int.py +++ b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_invocation_wait_resume_int.py @@ -289,6 +289,22 @@ def create_plugin(self, info: InvocationStartInfo) -> _PluginT: return plugin +class _RecordingOtelFactory(Generic[_PluginT]): + """OTel-specific recording wrapper that keeps factory registration metadata.""" + + __durable_registration_api__ = 1 + exclusive_group = "aws-durable-execution-otel-view" + + def __init__(self, build: Callable[[], _PluginT]) -> None: + self.build = build + self.instances: list[_PluginT] = [] + + def create_plugin(self, info: InvocationStartInfo) -> _PluginT: + plugin = self.build() + self.instances.append(plugin) + return plugin + + class InheritedInvocationPlugin(InvocationOtelPlugin): pass @@ -322,7 +338,7 @@ def test_handler_user_spans_inherit_context_across_resume_and_failure( exporter = InMemorySpanExporter() provider = TracerProvider() provider.add_span_processor(SimpleSpanProcessor(exporter)) - factory = _RecordingFactory( + factory = _RecordingOtelFactory( lambda: plugin_type( OtelPluginConfig(tracer_provider=provider, enrich_logger=False) ) diff --git a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_view_registration.py b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_view_registration.py index 09fecbbe6..3f16cb05e 100644 --- a/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_view_registration.py +++ b/packages/aws-durable-execution-sdk-python-otel/tests/e2e/test_view_registration.py @@ -4,24 +4,12 @@ from types import SimpleNamespace from collections.abc import Iterator from typing import Any, cast -from unittest.mock import Mock import pytest from aws_durable_execution_sdk_python import DurableContext from aws_durable_execution_sdk_python.config import Duration from aws_durable_execution_sdk_python.exceptions import PluginLoadError -from aws_durable_execution_sdk_python.execution import ( - durable_execution, - DurableExecutionInvocationInputWithClient, - InitialExecutionState, -) -from aws_durable_execution_sdk_python.lambda_service import ( - DurableServiceClient, - ExecutionDetails, - Operation, - OperationStatus, - OperationType, -) +from aws_durable_execution_sdk_python.execution import durable_execution from aws_durable_execution_sdk_python.plugin import ( DurableInstrumentationPlugin, DurableInstrumentationPluginFactory, @@ -38,6 +26,7 @@ from aws_durable_execution_sdk_python_otel import ( ExecutionOtelPlugin, ExecutionOtelPluginFactory, + InvocationOtelPlugin, InvocationOtelPluginFactory, OtelPluginConfig, ) @@ -63,93 +52,189 @@ def _handler(_event: Any, _context: DurableContext) -> str: return "unused" -def _invoke_once(configured: Any) -> Any: - """Call a decorated handler once against an injected in-process service client. +class _InheritedExecutionFactory(ExecutionOtelPluginFactory): + """OTel wrapper that inherits the built-in factory registration metadata.""" - The local runner retries an invocation whose handler raises, turning a - registration conflict raised at invocation start into a timeout; calling the - handler directly keeps the raised error observable. - """ - operation = Operation( - operation_id="exec1", - operation_type=OperationType.EXECUTION, - status=OperationStatus.STARTED, - execution_details=ExecutionDetails(input_payload="{}"), - ) - invocation_input = DurableExecutionInvocationInputWithClient( - durable_execution_arn="arn:test:execution/exec1", - checkpoint_token="token", # noqa: S106 - test fixture value - initial_execution_state=InitialExecutionState( - operations=[operation], next_marker="" - ), - service_client=Mock(spec=DurableServiceClient), - ) - lambda_context = Mock( - aws_request_id="test-request", - client_context=None, - identity=None, - _epoch_deadline_time_in_ms=1_000_000, - invoked_function_arn=None, - tenant_id=None, - ) - return configured(invocation_input, lambda_context) + def __init__(self, config: OtelPluginConfig | None = None) -> None: + super().__init__(config) + self.instances: list[DurableInstrumentationPlugin] = [] + + def create_plugin(self, info: InvocationStartInfo) -> ExecutionOtelPlugin: + plugin = super().create_plugin(info) + self.instances.append(plugin) + return plugin + + +class _InheritedInvocationFactory(InvocationOtelPluginFactory): + """OTel wrapper that inherits the built-in factory registration metadata.""" + + def __init__(self, config: OtelPluginConfig | None = None) -> None: + super().__init__(config) + self.instances: list[DurableInstrumentationPlugin] = [] + + def create_plugin(self, info: InvocationStartInfo) -> InvocationOtelPlugin: + plugin = super().create_plugin(info) + self.instances.append(plugin) + return plugin + + +class _ComposedExecutionFactory: + """OTel composition wrapper that declares the registration contract.""" + + __durable_registration_api__ = 1 + exclusive_group = "aws-durable-execution-otel-view" + + def __init__(self, config: OtelPluginConfig | None = None) -> None: + self._delegate = ExecutionOtelPluginFactory(config) + self.instances: list[DurableInstrumentationPlugin] = [] + + def create_plugin(self, info: InvocationStartInfo) -> DurableInstrumentationPlugin: + plugin = self._delegate.create_plugin(info) + self.instances.append(plugin) + return plugin -class _DelegatingFactory: - """A user factory delegating to a built-in one, carrying no registration marker. +class _ComposedInvocationFactory: + """OTel composition wrapper that declares the registration contract.""" - The reported shape: ``create_plugin`` and nothing else, so its class has no - marker even though the plugin it returns does. - """ + __durable_registration_api__ = 1 + exclusive_group = "aws-durable-execution-otel-view" - def __init__(self, delegate: Any) -> None: self._delegate = delegate + def __init__(self, config: OtelPluginConfig | None = None) -> None: + self._delegate = InvocationOtelPluginFactory(config) + self.instances: list[DurableInstrumentationPlugin] = [] def create_plugin(self, info: InvocationStartInfo) -> DurableInstrumentationPlugin: - return self._delegate.create_plugin(info) + plugin = self._delegate.create_plugin(info) + self.instances.append(plugin) + return plugin + + +def _wrapped_execution_factory( + config: OtelPluginConfig, style: str +) -> DurableInstrumentationPluginFactory: + if style == "inherit": + return _InheritedExecutionFactory(config) + return _ComposedExecutionFactory(config) + + +def _wrapped_invocation_factory( + config: OtelPluginConfig, style: str +) -> DurableInstrumentationPluginFactory: + if style == "inherit": + return _InheritedInvocationFactory(config) + return _ComposedInvocationFactory(config) + + +def _created_plugins(factory: DurableInstrumentationPluginFactory) -> list[object]: + return list(getattr(factory, "instances", [])) @pytest.mark.parametrize("wrapped", ["execution", "invocation", "both"]) @pytest.mark.parametrize("reverse", [False, True]) +@pytest.mark.parametrize("style", ["inherit", "compose"]) def test_wrapper_factory_cannot_bypass_view_exclusion( telemetry: tuple[TracerProvider, InMemorySpanExporter], + monkeypatch: pytest.MonkeyPatch, wrapped: str, reverse: bool, + style: str, ) -> None: provider, exporter = telemetry + created: list[str] = [] + handler_calls: list[str] = [] + original_execution_create = ExecutionOtelPluginFactory.create_plugin + original_invocation_create = InvocationOtelPluginFactory.create_plugin + + def create_execution( + self: ExecutionOtelPluginFactory, info: InvocationStartInfo + ) -> DurableInstrumentationPlugin: + created.append("execution") + return original_execution_create(self, info) + + def create_invocation( + self: InvocationOtelPluginFactory, info: InvocationStartInfo + ) -> DurableInstrumentationPlugin: + created.append("invocation") + return original_invocation_create(self, info) + + monkeypatch.setattr(ExecutionOtelPluginFactory, "create_plugin", create_execution) + monkeypatch.setattr(InvocationOtelPluginFactory, "create_plugin", create_invocation) + + def handler(_event: Any, _context: DurableContext) -> str: + handler_calls.append("ran") + return "unused" + config = OtelPluginConfig(tracer_provider=provider, enrich_logger=False) - execution = ExecutionOtelPluginFactory(config) - invocation = InvocationOtelPluginFactory(config) + execution: DurableInstrumentationPluginFactory = ( + _wrapped_execution_factory(config, style) + if wrapped in {"execution", "both"} + else ExecutionOtelPluginFactory(config) + ) + invocation: DurableInstrumentationPluginFactory = ( + _wrapped_invocation_factory(config, style) + if wrapped in {"invocation", "both"} + else InvocationOtelPluginFactory(config) + ) + observer = _ObserverFactory() plugins: list[DurableInstrumentationPluginFactory] = [ - _DelegatingFactory(execution), - _DelegatingFactory(invocation), + execution, + invocation, + observer, ] - if wrapped == "execution": - plugins[1] = invocation - elif wrapped == "invocation": - plugins[0] = execution if reverse: plugins.reverse() - configured = durable_execution(_handler, plugins=plugins) with pytest.raises(PluginLoadError) as error: - _invoke_once(configured) - assert "ExecutionOtelPlugin" in str(error.value) - assert "InvocationOtelPlugin" in str(error.value) - assert "Keep only one" in str(error.value) + durable_execution(handler, plugins=plugins) + message = str(error.value) + assert type(execution).__qualname__ in message + assert type(invocation).__qualname__ in message + assert "mutually exclusive" in message + assert "Keep only one" in message + assert "aws-durable-execution-otel-view" in message + assert created == [] + assert _created_plugins(execution) == [] + assert _created_plugins(invocation) == [] + assert observer.instances == [] + assert observer.statuses == [] + assert handler_calls == [] assert not exporter.get_finished_spans() +@pytest.mark.parametrize( + "wrapper", + [ + _InheritedExecutionFactory, + _InheritedInvocationFactory, + _ComposedExecutionFactory, + _ComposedInvocationFactory, + ], +) def test_single_wrapped_view_with_unrelated_plugin_is_accepted( telemetry: tuple[TracerProvider, InMemorySpanExporter], + wrapper: type[Any], ) -> None: provider, exporter = telemetry config = OtelPluginConfig(tracer_provider=provider, enrich_logger=False) - execution = ExecutionOtelPluginFactory(config) - configured = durable_execution( - _handler, plugins=[_DelegatingFactory(execution), _ObserverFactory()] + factory = wrapper(config) + observer = _ObserverFactory() + handler_calls: list[str] = [] + + def handler(_event: Any, _context: DurableContext) -> str: + handler_calls.append("ran") + return "unused" + + configured = durable_execution(handler, plugins=[factory, observer]) + with DurableFunctionTestRunner(handler=configured) as runner: + assert runner.run(input="{}", timeout=15).status.value == "SUCCEEDED" + assert handler_calls == ["ran"] + assert len(factory.instances) == 1 + assert observer.statuses == [InvocationStatus.SUCCEEDED] + assert any( + span.name in {"Workflow", "Invocation"} + for span in exporter.get_finished_spans() ) - assert _invoke_once(configured)["Status"] == "SUCCEEDED" - assert not exporter.get_finished_spans() @pytest.mark.parametrize("registration", ["explicit", "environment", "mixed"]) diff --git a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin.py b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin.py index 4f921b372..d23b6aad9 100644 --- a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin.py +++ b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin.py @@ -12,7 +12,6 @@ from enum import Enum from typing import Any, Callable, MutableMapping, Protocol, cast -from aws_durable_execution_sdk_python.exceptions import PluginLoadError from aws_durable_execution_sdk_python.identifier import OperationIdentifier from aws_durable_execution_sdk_python.lambda_service import ( DurableExecutionInvocationOutput, @@ -720,94 +719,6 @@ def _factory_name(factory: object) -> str: return _type_name(factory) -def _module_qualified_name(named: object) -> str: - """Join a class's or function's module and qualified name. - - Both attributes are read with a default, because a rejected entry can be any - object at all, and a missing name must not become an ``AttributeError``. - """ - module = getattr(named, "__module__", None) or "unknown module" - qualified = getattr(named, "__qualname__", None) or getattr(named, "__name__", None) - return f"{module}.{qualified or ''}" - - -def _registration_api_owner(plugin_type: type[object]) -> type[object] | None: - """Find the first explicit marker in the MRO without metaclass descriptors. - - A subclass inherits its declaring class's registration contract. Its legacy - same-named fields/helpers are not part of that contract unless it repeats - the marker. An explicit unsupported marker shadows any inherited opt-in. - """ - mro: tuple[type[object], ...] = type.__dict__["__mro__"].__get__( - plugin_type, type(plugin_type) - ) - for declaring_type in mro: - namespace = type.__dict__["__dict__"].__get__( - declaring_type, type(declaring_type) - ) - if "__durable_registration_api__" in namespace: - version = namespace["__durable_registration_api__"] - return declaring_type if type(version) is int and version == 1 else None - return None - - -def _exclusive_groups(plugin_type: type[object]) -> list[str]: - """Accumulate opted-in MRO contracts without interpreting legacy fields.""" - mro: tuple[type[object], ...] = type.__dict__["__mro__"].__get__( - plugin_type, type(plugin_type) - ) - groups: list[str] = [] - for declaring_type in mro: - namespace = type.__dict__["__dict__"].__get__( - declaring_type, type(declaring_type) - ) - version = namespace.get("__durable_registration_api__") - if type(version) is not int or version != 1: - continue - try: - group = getattr(declaring_type, "exclusive_group", None) - except Exception as error: - raise PluginLoadError( - f"Cannot read exclusive_group for {_module_qualified_name(plugin_type)}." - ) from error - if group is None: - continue - if type(group) is not str or not group.strip(): - raise PluginLoadError( - f"Durable instrumentation plugin {_module_qualified_name(plugin_type)} " - "must declare exclusive_group as None or a non-empty string." - ) - # Repeating a marker or a group does not register this one plugin twice. - if group not in groups: - groups.append(group) - return groups - - -def _validate_exclusive_groups( - plugin_types: Sequence[type[object]], -) -> None: - """Reject competing instrumentation before any lifecycle hooks run.""" - groups: dict[str, type[object]] = {} - for plugin_type in plugin_types: - if _registration_api_owner(plugin_type) is None: - continue - for group in _exclusive_groups(plugin_type): - if (previous := groups.get(group)) is not None: - if previous is plugin_type: - raise PluginLoadError( - f"Durable instrumentation plugin {_module_qualified_name(plugin_type)} " - f"is registered more than once in exclusive group '{group}'. " - "Register this plugin only once in plugins." - ) - raise PluginLoadError( - f"Durable instrumentation plugins {_module_qualified_name(previous)} " - f"and {_module_qualified_name(plugin_type)} are mutually exclusive " - f"(group '{group}'). Keep only one plugin from this group in " - "plugins and DURABLE_EXECUTION_PLUGINS." - ) - groups[group] = plugin_type - - # Raised out of plugin code, these three are not reports of a plugin defect but # instructions to the thread that is running: stop. Containing one would drop the # instruction and return a thread that was told to unwind to the work after the @@ -1537,12 +1448,6 @@ def _create_plugins(self, info: InvocationStartInfo) -> None: ) continue plugins.append(plugin) - # The returned plugin is the actual instrumentation, and its concrete - # type may declare an exclusive group even when the factory that built it - # does not -- a delegating wrapper carries no marker of its own. Check - # the returned types before any hook is dispatched, and outside the - # containment above: a conflict is configuration, not a plugin failure. - _validate_exclusive_groups([type(plugin) for plugin in plugins]) self._plugins = plugins @staticmethod diff --git a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin_discovery.py b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin_discovery.py index 7e2e69199..67c570ae7 100644 --- a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin_discovery.py +++ b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/plugin_discovery.py @@ -10,9 +10,6 @@ from aws_durable_execution_sdk_python.exceptions import PluginLoadError from aws_durable_execution_sdk_python.plugin import ( DurableInstrumentationPluginFactory, - _module_qualified_name, - _registration_api_owner, - _validate_exclusive_groups, ) @@ -57,6 +54,18 @@ def _distribution_name(entry_point: metadata.EntryPoint) -> str: return distribution.metadata.get("Name", "unknown distribution") +def _module_qualified_name(named: object) -> str: + """Join a class's or function's module and qualified name. + + Both attributes are read with a default, because a rejected entry can be any + object at all. A missing name must not replace the configuration error with an + ``AttributeError``. + """ + module = getattr(named, "__module__", None) or "unknown module" + qualified = getattr(named, "__qualname__", None) or getattr(named, "__name__", None) + return f"{module}.{qualified or ''}" + + def _describe_value(value: object) -> str: """Describe a rejected registration entry so the caller can identify it. @@ -277,6 +286,26 @@ def _factory_type(factory: object) -> type[object]: ) +def _registration_api_owner(plugin_type: type[object]) -> type[object] | None: + """Find the first explicit marker in the MRO without metaclass descriptors. + + A subclass inherits its declaring class's registration contract. Its legacy + same-named fields/helpers are not part of that contract unless it repeats + the marker. An explicit unsupported marker shadows any inherited opt-in. + """ + mro: tuple[type[object], ...] = type.__dict__["__mro__"].__get__( + plugin_type, type(plugin_type) + ) + for declaring_type in mro: + namespace = type.__dict__["__dict__"].__get__( + declaring_type, type(declaring_type) + ) + if "__durable_registration_api__" in namespace: + version = namespace["__durable_registration_api__"] + return declaring_type if type(version) is int and version == 1 else None + return None + + def _registration_result_callback(plugin: object, owner: type[object]) -> object | None: """Bind the opted-in contract without calling a legacy subclass helper.""" if owner is _factory_type(plugin): @@ -293,6 +322,63 @@ def _registration_result_callback(plugin: object, owner: type[object]) -> object return None +def _exclusive_groups(plugin_type: type[object]) -> list[str]: + """Accumulate opted-in MRO contracts without interpreting legacy fields.""" + mro: tuple[type[object], ...] = type.__dict__["__mro__"].__get__( + plugin_type, type(plugin_type) + ) + groups: list[str] = [] + for declaring_type in mro: + namespace = type.__dict__["__dict__"].__get__( + declaring_type, type(declaring_type) + ) + version = namespace.get("__durable_registration_api__") + if type(version) is not int or version != 1: + continue + try: + group = getattr(declaring_type, "exclusive_group", None) + except Exception as error: + raise PluginLoadError( + f"Cannot read exclusive_group for {_module_qualified_name(plugin_type)}." + ) from error + if group is None: + continue + if type(group) is not str or not group.strip(): + raise PluginLoadError( + f"Durable instrumentation plugin {_module_qualified_name(plugin_type)} " + "must declare exclusive_group as None or a non-empty string." + ) + # Repeating a marker or a group does not register this one plugin twice. + if group not in groups: + groups.append(group) + return groups + + +def _validate_exclusive_groups( + plugin_types: Sequence[type[object]], +) -> None: + """Reject competing instrumentation before any lifecycle hooks run.""" + groups: dict[str, type[object]] = {} + for plugin_type in plugin_types: + if _registration_api_owner(plugin_type) is None: + continue + for group in _exclusive_groups(plugin_type): + if (previous := groups.get(group)) is not None: + if previous is plugin_type: + raise PluginLoadError( + f"Durable instrumentation plugin {_module_qualified_name(plugin_type)} " + f"is registered more than once in exclusive group '{group}'. " + "Register this plugin only once in plugins." + ) + raise PluginLoadError( + f"Durable instrumentation plugins {_module_qualified_name(previous)} " + f"and {_module_qualified_name(plugin_type)} are mutually exclusive " + f"(group '{group}'). Keep only one plugin from this group in " + "plugins and DURABLE_EXECUTION_PLUGINS." + ) + groups[group] = plugin_type + + def _notify_registration_result( plugins: Sequence[object], *,