-
Notifications
You must be signed in to change notification settings - Fork 23
fix(otel): rely on Lambda layers for OpenTelemetry dependencies #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #!/usr/bin/env python3 | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import email | ||
| import zipfile | ||
| from pathlib import Path | ||
|
|
||
| from packaging.requirements import Requirement | ||
| from packaging.utils import canonicalize_name | ||
|
|
||
|
|
||
| EXPECTED_STANDALONE_DEPENDENCIES = { | ||
| "opentelemetry-api", | ||
| "opentelemetry-propagator-aws-xray", | ||
| "opentelemetry-sdk", | ||
| } | ||
|
|
||
|
|
||
| def check_otel_wheel_dependencies(wheel: Path) -> None: | ||
| """Validate that OpenTelemetry dependencies are layer-provided by default.""" | ||
| requirements = _read_requirements(wheel) | ||
| base_otel_dependencies = { | ||
| canonicalize_name(requirement.name) | ||
| for requirement in requirements | ||
| if canonicalize_name(requirement.name).startswith("opentelemetry-") | ||
| and (requirement.marker is None or requirement.marker.evaluate({"extra": ""})) | ||
| } | ||
| if base_otel_dependencies: | ||
| names = ", ".join(sorted(base_otel_dependencies)) | ||
| raise ValueError( | ||
| f"{wheel.name} installs OpenTelemetry dependencies by default: {names}" | ||
| ) | ||
|
|
||
| standalone_dependencies = { | ||
| canonicalize_name(requirement.name) | ||
| for requirement in requirements | ||
| if canonicalize_name(requirement.name).startswith("opentelemetry-") | ||
| and requirement.marker is not None | ||
| and requirement.marker.evaluate({"extra": "standalone"}) | ||
| } | ||
| if standalone_dependencies != EXPECTED_STANDALONE_DEPENDENCIES: | ||
| expected = ", ".join(sorted(EXPECTED_STANDALONE_DEPENDENCIES)) | ||
| actual = ", ".join(sorted(standalone_dependencies)) or "none" | ||
| raise ValueError( | ||
| f"{wheel.name} standalone OpenTelemetry dependencies must be " | ||
| f"{expected}; found {actual}" | ||
| ) | ||
|
|
||
|
|
||
| def _read_requirements(wheel: Path) -> list[Requirement]: | ||
| if not wheel.is_file(): | ||
| raise FileNotFoundError(wheel) | ||
|
|
||
| with zipfile.ZipFile(wheel) as archive: | ||
| metadata_files = [ | ||
| name for name in archive.namelist() if name.endswith(".dist-info/METADATA") | ||
| ] | ||
| if len(metadata_files) != 1: | ||
| raise ValueError( | ||
| f"{wheel.name} must contain exactly one dist-info/METADATA file" | ||
| ) | ||
| metadata = email.message_from_bytes(archive.read(metadata_files[0])) | ||
|
|
||
| return [ | ||
| Requirement(value) for value in metadata.get_all("Requires-Dist", failobj=[]) | ||
| ] | ||
|
|
||
|
|
||
| def main(argv: list[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser( | ||
| description="Validate the OTel plugin wheel dependency contract." | ||
| ) | ||
| parser.add_argument("wheel", type=Path) | ||
| args = parser.parse_args(argv) | ||
|
|
||
| check_otel_wheel_dependencies(args.wheel) | ||
| print(args.wheel) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
96 changes: 96 additions & 0 deletions
96
.github/scripts/tests/test_check_otel_wheel_dependencies.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import sys | ||
| import zipfile | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) | ||
|
|
||
| from check_otel_wheel_dependencies import check_otel_wheel_dependencies | ||
|
|
||
|
|
||
| def _write_wheel(tmp_path: Path, requirements: tuple[str, ...]) -> Path: | ||
| wheel = tmp_path / "aws_durable_execution_sdk_python_otel-1.0.0-py3-none-any.whl" | ||
| metadata = [ | ||
| "Metadata-Version: 2.4", | ||
| "Name: aws-durable-execution-sdk-python-otel", | ||
| "Version: 1.0.0", | ||
| *(f"Requires-Dist: {requirement}" for requirement in requirements), | ||
| "", | ||
| ] | ||
| with zipfile.ZipFile(wheel, "w") as archive: | ||
| archive.writestr( | ||
| "aws_durable_execution_sdk_python_otel-1.0.0.dist-info/METADATA", | ||
| "\n".join(metadata), | ||
| ) | ||
| return wheel | ||
|
|
||
|
|
||
| def test_accepts_layer_provided_dependencies_with_standalone_extra( | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| wheel = _write_wheel( | ||
| tmp_path, | ||
| ( | ||
| "aws-durable-execution-sdk-python>=1.8.0", | ||
| "OpenTelemetry_API>=1.20.0; extra == 'standalone'", | ||
| "opentelemetry.sdk>=1.20.0; extra == 'standalone'", | ||
| "OpenTelemetry-Propagator_AWS-XRay; extra == 'standalone'", | ||
| ), | ||
| ) | ||
|
|
||
| check_otel_wheel_dependencies(wheel) | ||
|
|
||
|
|
||
| def test_rejects_default_opentelemetry_dependency(tmp_path: Path) -> None: | ||
| wheel = _write_wheel( | ||
| tmp_path, | ||
| ( | ||
| "aws-durable-execution-sdk-python>=1.8.0", | ||
| "opentelemetry-sdk>=1.20.0", | ||
| "opentelemetry-api>=1.20.0; extra == 'standalone'", | ||
| "opentelemetry-sdk>=1.20.0; extra == 'standalone'", | ||
| "opentelemetry-propagator-aws-xray; extra == 'standalone'", | ||
| ), | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError, match="installs OpenTelemetry dependencies"): | ||
| check_otel_wheel_dependencies(wheel) | ||
|
|
||
|
|
||
| def test_rejects_noncanonical_default_opentelemetry_dependency( | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| wheel = _write_wheel( | ||
| tmp_path, | ||
| ( | ||
| "aws-durable-execution-sdk-python>=1.8.0", | ||
| "OpenTelemetry_SDK>=1.20.0", | ||
| "opentelemetry-api>=1.20.0; extra == 'standalone'", | ||
| "opentelemetry-sdk>=1.20.0; extra == 'standalone'", | ||
| "opentelemetry-propagator-aws-xray; extra == 'standalone'", | ||
| ), | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError, match="installs OpenTelemetry dependencies"): | ||
| check_otel_wheel_dependencies(wheel) | ||
|
|
||
|
|
||
| def test_rejects_incomplete_standalone_extra(tmp_path: Path) -> None: | ||
| wheel = _write_wheel( | ||
| tmp_path, | ||
| ( | ||
| "aws-durable-execution-sdk-python>=1.8.0", | ||
| "opentelemetry-api>=1.20.0; extra == 'standalone'", | ||
| "opentelemetry-sdk>=1.20.0; extra == 'standalone'", | ||
| ), | ||
| ) | ||
|
|
||
| with pytest.raises( | ||
| ValueError, match="standalone OpenTelemetry dependencies must be" | ||
| ): | ||
| check_otel_wheel_dependencies(wheel) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codex AI review
[P2] Avoid host-dependent marker evaluation
Marker.evaluate()fills unspecified values such asplatform_machinefrom the CI runner. A default dependency guarded byplatform_machine == "aarch64"therefore passes this check on x86 while still installing OpenTelemetry on ARM Lambda, recreating the layer-shadowing problem. Validate that each OTel marker logically requiresextra == "standalone"(or evaluate every supported target environment), and add a platform-marker test.