diff --git a/.github/workflows/telemetry-conformance-tests.yml b/.github/workflows/telemetry-conformance-tests.yml index c4728faf2..36fa52f9d 100644 --- a/.github/workflows/telemetry-conformance-tests.yml +++ b/.github/workflows/telemetry-conformance-tests.yml @@ -42,8 +42,8 @@ jobs: id-token: write # The pinned SHA also selects the runner and telemetry-extension revision the shared # workflow installs, so it must be reachable from a branch. It is the merge commit of - # aws-durable-execution-conformance-tests PR #183 on that repository's main branch. - uses: aws/aws-durable-execution-conformance-tests/.github/workflows/telemetry-suite.yml@cf13ef27d6e13a4423f7e0a178f9a3d4bb1099f0 + # aws-durable-execution-conformance-tests PR #185 on that repository's main branch. + uses: aws/aws-durable-execution-conformance-tests/.github/workflows/telemetry-suite.yml@b380cbf19bbc4f08a2d7c593d4bf24f585946415 with: language: python resource_prefix: python diff --git a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/README.md b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/README.md index 088573ec1..3e4a0aed4 100644 --- a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/README.md +++ b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/README.md @@ -21,6 +21,7 @@ This package holds the SDK-specific part: the handler and the SAM template that | `telemetry` | `telemetry-1` | One successful single-step execution emits one `SUCCEEDED` record with `input`, `output`, ARN-derived identity fields, and `operationsByName`. | | `telemetry` | `telemetry-2` | One failed execution emits one `FAILED` record with an `error` on the execution and on `operationsByName.charge`. | | `telemetry` | `telemetry-3` | An `OperationOverride.result` transform exports exactly `{userId}` of a three-key step result; the record has no `input` or `output`. | +| `telemetry` | `telemetry-4` | With `operation_detail="full-tree"`, the record holds the map, its two iteration contexts, and the two steps by name. | ## Layout @@ -29,6 +30,7 @@ src/ telemetry_1_success.py # single step "increment"; plugin + CloudWatchLogsExporter telemetry_2_failure.py # step "charge" fails; handler raises "payment failed" from the step error telemetry_3_result_select.py # step "fetch-user"; override transform keeps only userId in the record + telemetry_4_full_tree_map.py # map "process-items" with step "greet-"; full-tree detail records all five operations Makefile # one build- target per function, used by `sam build` requirements.txt # core SDK + telemetry exporter, pinned to ${PYTHON_SDK_REF} template.yaml # declares TelemetryLogGroupName and creates the group diff --git a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/src/Makefile b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/src/Makefile index c655f70d6..28732156a 100644 --- a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/src/Makefile +++ b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/src/Makefile @@ -1,4 +1,4 @@ -.PHONY: build-Telemetry1Success build-Telemetry2Failure build-Telemetry3ResultSelect build-handlers +.PHONY: build-Telemetry1Success build-Telemetry2Failure build-Telemetry3ResultSelect build-Telemetry4FullTreeMap build-handlers # Every function builds from the same src/ directory with the same recipe. # SAM calls one target per logical ID, so each target points at the shared one. @@ -8,6 +8,8 @@ build-Telemetry2Failure: build-handlers build-Telemetry3ResultSelect: build-handlers +build-Telemetry4FullTreeMap: build-handlers + build-handlers: python3 -m pip install --upgrade \ --target "$(ARTIFACTS_DIR)" \ diff --git a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/src/telemetry_4_full_tree_map.py b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/src/telemetry_4_full_tree_map.py new file mode 100644 index 000000000..5d6c4cf80 --- /dev/null +++ b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/src/telemetry_4_full_tree_map.py @@ -0,0 +1,79 @@ +# SPDX-FileCopyrightText: 2026-present Amazon.com, Inc. or its affiliates. +# +# SPDX-License-Identifier: Apache-2.0 +"""Full-tree operation detail for requirement telemetry-4. + +The handler maps over the event ``[1, 2]`` with ``maxConcurrency=1``. The item +namer names each iteration ``item-``. Each iteration runs one step named +``greet-``. The plugin runs with ``operation_detail="full-tree"``. So the +record's ``operationsByName`` holds the map context ``process-items``, the two +iteration contexts ``item-1`` and ``item-2``, and the two steps ``greet-1`` and +``greet-2``. Under ``top-level`` detail the record would hold only +``process-items``. + +The handler uses the real SDK API only. It builds no record and calls no +exporter method. A missing or wrong record is the plugin's defect, and that +defect is the signal the suite measures. +""" + +from __future__ import annotations + +import os +from typing import Any + +from aws_durable_execution_sdk_python import ( + DurableContext, + StepContext, + durable_execution, + durable_step, +) +from aws_durable_execution_sdk_python.config import MapConfig +from aws_durable_execution_sdk_python_telemetry_exporter import ( + CloudWatchLogsExporter, + ContentConfig, + DurableExecutionTelemetryConfig, + durable_execution_telemetry, +) + + +# The template sets this variable. Without it the exporter has no target. +# So the module fails at import instead of dropping records silently. +log_group_name = os.environ.get("TELEMETRY_LOG_GROUP_NAME") +if not log_group_name: + raise RuntimeError("TELEMETRY_LOG_GROUP_NAME must be set") + + +@durable_step +def greet(_step_context: StepContext, item: int) -> str: + return f"Hello, {item}!" + + +def map_fn(ctx: DurableContext, item: int, _index: int, _items: Any) -> str: + result: str = ctx.step(greet(item), name=f"greet-{item}") + return result + + +@durable_execution( + plugins=[ + durable_execution_telemetry( + DurableExecutionTelemetryConfig( + exporters=[CloudWatchLogsExporter(log_group_name=log_group_name)], + emit_mode="on-complete", + sampling_rate=1, + operation_detail="full-tree", + content=ContentConfig(input=True, output=True), + ) + ) + ] +) +def handler(event: list[int], context: DurableContext) -> list[str]: + results: list[str] = context.map( + event, + map_fn, + name="process-items", + config=MapConfig( + max_concurrency=1, + item_namer=lambda item, index: f"item-{item}", + ), + ).get_results() + return results diff --git a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/template.yaml b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/template.yaml index 0c0ec803d..a276301a7 100644 --- a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/template.yaml +++ b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/template.yaml @@ -74,3 +74,17 @@ Resources: Handler: telemetry_3_result_select.handler FunctionName: !Sub "${AWS::StackName}-telemetry-3" Role: !Ref LambdaExecutionRoleArn + + Telemetry4FullTreeMap: + Type: AWS::Serverless::Function + DependsOn: TelemetryLogGroup + Metadata: + BuildMethod: makefile + TestingMetadata: + TestDescription: + - telemetry-4 + Properties: + CodeUri: src/ + Handler: telemetry_4_full_tree_map.handler + FunctionName: !Sub "${AWS::StackName}-telemetry-4" + Role: !Ref LambdaExecutionRoleArn diff --git a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/tests/test_telemetry_examples.py b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/tests/test_telemetry_examples.py index 961ac3c23..c1bb66e98 100644 --- a/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/tests/test_telemetry_examples.py +++ b/packages/aws-durable-execution-sdk-python-conformance-tests-telemetry/tests/test_telemetry_examples.py @@ -34,7 +34,12 @@ SDK_REPOSITORY_URL = "git+https://github.com/aws/aws-durable-execution-sdk-python.git" EXPECTED_MODULES: frozenset[str] = frozenset( - {"telemetry_1_success", "telemetry_2_failure", "telemetry_3_result_select"} + { + "telemetry_1_success", + "telemetry_2_failure", + "telemetry_3_result_select", + "telemetry_4_full_tree_map", + } ) # Logical ID -> requirement ID. The function name suffix must equal the # requirement ID, so the runner can match the deployed function to its test. @@ -42,6 +47,7 @@ "Telemetry1Success": "telemetry-1", "Telemetry2Failure": "telemetry-2", "Telemetry3ResultSelect": "telemetry-3", + "Telemetry4FullTreeMap": "telemetry-4", } @@ -130,6 +136,18 @@ def test_handler_modules_are_exactly_the_expected_set() -> None: assert {path.stem for path in SRC_DIR.glob("*.py")} == EXPECTED_MODULES +def test_every_handler_module_is_deployed_by_exactly_one_function( + template: dict[str, Any], +) -> None: + """Two functions on one module would leave one requirement untested.""" + deployed_modules = sorted( + resource["Properties"]["Handler"].partition(".")[0] + for resource in functions(template).values() + ) + + assert deployed_modules == sorted(EXPECTED_MODULES) + + def test_requirements_install_both_packages_from_one_commit() -> None: requirements: str = (SRC_DIR / "requirements.txt").read_text(encoding="utf-8") @@ -350,3 +368,53 @@ def test_telemetry_3_handler_exports_only_the_selected_result_key( assert fetch_user["status"] == "SUCCEEDED" assert fetch_user["count"] == 1 assert fetch_user["result"] == {"userId": "u-123"} + + +def test_telemetry_4_handler_records_the_full_operation_tree( + logs_client: FakeLogsClient, +) -> None: + """telemetry-4 pins five operationsByName keys under full-tree detail.""" + module = load_handler_module("telemetry_4_full_tree_map") + + with DurableFunctionTestRunner( + handler=module.handler, execution_timeout=15 + ) as runner: + result = runner.run(input="[1, 2]") + + assert result.status is InvocationStatus.SUCCEEDED + assert result.result is not None + assert json.loads(result.result) == ["Hello, 1!", "Hello, 2!"] + assert len(logs_client.put_log_events_calls) == 1 + assert logs_client.put_log_events_calls[0]["logGroupName"] == LOG_GROUP_NAME + record = logs_client.records()[0] + assert record["status"] == "SUCCEEDED" + assert record["input"] == [1, 2] + assert record["output"] == ["Hello, 1!", "Hello, 2!"] + operations = record["operationsByName"] + assert set(operations) == { + "process-items", + "item-1", + "item-2", + "greet-1", + "greet-2", + } + expected_kinds = { + "process-items": ("CONTEXT", "Map"), + "item-1": ("CONTEXT", "MapIteration"), + "item-2": ("CONTEXT", "MapIteration"), + "greet-1": ("STEP", "Step"), + "greet-2": ("STEP", "Step"), + } + for name, (operation_type, sub_type) in expected_kinds.items(): + operation = operations[name] + assert operation["type"] == operation_type + assert operation["subType"] == sub_type + assert operation["status"] == "SUCCEEDED" + assert operation["count"] == 1 + assert operation["failedCount"] == 0 + assert "result" not in operation + assert "error" not in operation + if operation_type == "CONTEXT": + assert "maxAttempt" not in operation + else: + assert operation["maxAttempt"] == 1