Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from aws_durable_execution_sdk_python_otel import (
ExecutionOtelPlugin,
ExecutionOtelPluginFactory,
InvocationOtelPlugin,
InvocationOtelPluginFactory,
OtelPluginConfig,
)
Expand All @@ -51,6 +52,191 @@ def _handler(_event: Any, _context: DurableContext) -> str:
return "unused"


class _InheritedExecutionFactory(ExecutionOtelPluginFactory):
"""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) -> 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 _ComposedInvocationFactory:
"""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 = InvocationOtelPluginFactory(config)
self.instances: list[DurableInstrumentationPlugin] = []

def create_plugin(self, info: InvocationStartInfo) -> DurableInstrumentationPlugin:
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: 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] = [
execution,
invocation,
observer,
]
if reverse:
plugins.reverse()

with pytest.raises(PluginLoadError) as error:
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)
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()
)


@pytest.mark.parametrize("registration", ["explicit", "environment", "mixed"])
@pytest.mark.parametrize("reverse", [False, True])
def test_competing_views_rejected_before_hooks(
Expand Down
Loading