-
Notifications
You must be signed in to change notification settings - Fork 432
chore(tests): refactor E2E tracer to ease maintenance, writing tests and parallelization #1457
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
heitorlessa
merged 19 commits into
aws-powertools:develop
from
heitorlessa:chore/refactor-e2e-tracer
Aug 18, 2022
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
50a3cc1
feat(tracer): use new infrastructure
heitorlessa 52706c6
feat: include POWERTOOLS_SERVICE_NAME env var
heitorlessa 4069c08
feat: refactor tests and get_traces
heitorlessa 5aac27b
chore: leftover docstring and return type
heitorlessa d8c5ca4
Merge branch 'develop' into chore/refactor-e2e-tracer
heitorlessa 4ec4a0b
feat: test sync and async capture
heitorlessa b7807cf
chore: leftover model
heitorlessa fb50f41
chore: break up helpers into builders and fetchers
heitorlessa 85657e9
fix: flaky X-Ray trace retrieval now with minimal traces opt
heitorlessa 6275374
chore: break up last helper for lambda invocation
heitorlessa dd786ed
chore: move asset models into asset module
heitorlessa d1d36a0
chore: address everyone's feedback
heitorlessa 70516ca
chore: address import feedback lint
heitorlessa 7a8b5e1
chore: fix new feedback set
heitorlessa 92df461
chore: remove trace_ids attr left over
heitorlessa c1c71d1
chore: move trace builders to the correct location
heitorlessa 15b3285
feat: use generator for memory efficient recursion
heitorlessa 9bb71eb
feat: split sync and async; test for nest fetching
heitorlessa d80c21e
Merge branch 'develop' into chore/refactor-e2e-tracer
heitorlessa 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pytest | ||
|
||
from tests.e2e.tracer.infrastructure import TracerStack | ||
from tests.e2e.utils.infrastructure import deploy_once | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="module") | ||
def infrastructure(request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory, worker_id: str): | ||
"""Setup and teardown logic for E2E test infrastructure | ||
|
||
Parameters | ||
---------- | ||
request : pytest.FixtureRequest | ||
pytest request fixture to introspect absolute path to test being executed | ||
tmp_path_factory : pytest.TempPathFactory | ||
pytest temporary path factory to discover shared tmp when multiple CPU processes are spun up | ||
worker_id : str | ||
pytest-xdist worker identification to detect whether parallelization is enabled | ||
|
||
Yields | ||
------ | ||
Dict[str, str] | ||
CloudFormation Outputs from deployed infrastructure | ||
""" | ||
yield from deploy_once(stack=TracerStack, request=request, tmp_path_factory=tmp_path_factory, worker_id=worker_id) |
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,16 @@ | ||
import asyncio | ||
from uuid import uuid4 | ||
|
||
from aws_lambda_powertools import Tracer | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
tracer = Tracer() | ||
|
||
|
||
@tracer.capture_method | ||
async def async_get_users(): | ||
return [{"id": f"{uuid4()}"} for _ in range(5)] | ||
|
||
|
||
def lambda_handler(event: dict, context: LambdaContext): | ||
return asyncio.run(async_get_users()) |
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 |
---|---|---|
@@ -1,25 +1,16 @@ | ||
import asyncio | ||
import os | ||
from uuid import uuid4 | ||
|
||
from aws_lambda_powertools import Tracer | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
tracer = Tracer(service="e2e-tests-app") | ||
tracer = Tracer() | ||
|
||
ANNOTATION_KEY = os.environ["ANNOTATION_KEY"] | ||
ANNOTATION_VALUE = os.environ["ANNOTATION_VALUE"] | ||
ANNOTATION_ASYNC_VALUE = os.environ["ANNOTATION_ASYNC_VALUE"] | ||
|
||
@tracer.capture_method | ||
def get_todos(): | ||
return [{"id": f"{uuid4()}", "completed": False} for _ in range(5)] | ||
|
||
|
||
@tracer.capture_lambda_handler | ||
def lambda_handler(event: dict, context: LambdaContext): | ||
tracer.put_annotation(key=ANNOTATION_KEY, value=ANNOTATION_VALUE) | ||
tracer.put_metadata(key=ANNOTATION_KEY, value=ANNOTATION_VALUE) | ||
return asyncio.run(collect_payment()) | ||
|
||
|
||
@tracer.capture_method | ||
async def collect_payment() -> str: | ||
tracer.put_annotation(key=ANNOTATION_KEY, value=ANNOTATION_ASYNC_VALUE) | ||
tracer.put_metadata(key=ANNOTATION_KEY, value=ANNOTATION_ASYNC_VALUE) | ||
return "success" | ||
return get_todos() |
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,17 @@ | ||
from pathlib import Path | ||
|
||
from tests.e2e.utils.data_builder import build_service_name | ||
from tests.e2e.utils.infrastructure import BaseInfrastructureV2 | ||
|
||
|
||
class TracerStack(BaseInfrastructureV2): | ||
# Maintenance: Tracer doesn't support dynamic service injection (tracer.py L310) | ||
# we could move after handler response or adopt env vars usage in e2e tests | ||
SERVICE_NAME: str = build_service_name() | ||
|
||
def __init__(self, handlers_dir: Path, feature_name: str = "tracer") -> None: | ||
super().__init__(feature_name, handlers_dir) | ||
|
||
def create_resources(self) -> None: | ||
env_vars = {"POWERTOOLS_SERVICE_NAME": self.SERVICE_NAME} | ||
self.create_lambda_functions(function_props={"environment": env_vars}) |
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 |
---|---|---|
@@ -1,51 +1,69 @@ | ||
import datetime | ||
import uuid | ||
|
||
import boto3 | ||
import pytest | ||
from e2e import conftest | ||
from e2e.utils import helpers | ||
|
||
from tests.e2e.tracer.handlers import async_capture, basic_handler | ||
from tests.e2e.tracer.infrastructure import TracerStack | ||
from tests.e2e.utils import data_builder, data_fetcher | ||
|
||
|
||
@pytest.fixture | ||
def basic_handler_fn_arn(infrastructure: dict) -> str: | ||
return infrastructure.get("BasicHandlerArn", "") | ||
|
||
|
||
@pytest.fixture | ||
def basic_handler_fn(infrastructure: dict) -> str: | ||
return infrastructure.get("BasicHandler", "") | ||
|
||
|
||
@pytest.fixture | ||
def async_fn_arn(infrastructure: dict) -> str: | ||
return infrastructure.get("AsyncCaptureArn", "") | ||
|
||
|
||
@pytest.fixture | ||
def async_fn(infrastructure: dict) -> str: | ||
return infrastructure.get("AsyncCapture", "") | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def config() -> conftest.LambdaConfig: | ||
return { | ||
"parameters": {"tracing": "ACTIVE"}, | ||
"environment_variables": { | ||
"ANNOTATION_KEY": f"e2e-tracer-{str(uuid.uuid4()).replace('-','_')}", | ||
"ANNOTATION_VALUE": "stored", | ||
"ANNOTATION_ASYNC_VALUE": "payments", | ||
}, | ||
} | ||
def test_lambda_handler_trace_is_visible(basic_handler_fn_arn: str, basic_handler_fn: str): | ||
# GIVEN | ||
handler_name = basic_handler.lambda_handler.__name__ | ||
handler_subsegment = f"## {handler_name}" | ||
handler_metadata_key = f"{handler_name} response" | ||
|
||
method_name = basic_handler.get_todos.__name__ | ||
method_subsegment = f"## {method_name}" | ||
handler_metadata_key = f"{method_name} response" | ||
|
||
trace_query = data_builder.build_trace_default_query(function_name=basic_handler_fn) | ||
|
||
# WHEN | ||
_, execution_time = data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn) | ||
data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn) | ||
|
||
# THEN | ||
trace = data_fetcher.get_traces(start_date=execution_time, filter_expression=trace_query, minimum_traces=2) | ||
|
||
assert len(trace.get_annotation(key="ColdStart", value=True)) == 1 | ||
assert len(trace.get_metadata(key=handler_metadata_key, namespace=TracerStack.SERVICE_NAME)) == 2 | ||
assert len(trace.get_metadata(key=handler_metadata_key, namespace=TracerStack.SERVICE_NAME)) == 2 | ||
assert len(trace.get_subsegment(name=handler_subsegment)) == 2 | ||
assert len(trace.get_subsegment(name=method_subsegment)) == 2 | ||
|
||
|
||
def test_basic_lambda_async_trace_visible(execute_lambda: conftest.InfrastructureOutput, config: conftest.LambdaConfig): | ||
def test_async_trace_is_visible(async_fn_arn: str, async_fn: str): | ||
# GIVEN | ||
lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="basichandlerarn") | ||
start_date = execute_lambda.get_lambda_execution_time() | ||
end_date = start_date + datetime.timedelta(minutes=5) | ||
trace_filter_exporession = f'service("{lambda_name}")' | ||
async_fn_name = async_capture.async_get_users.__name__ | ||
async_fn_name_subsegment = f"## {async_fn_name}" | ||
async_fn_name_metadata_key = f"{async_fn_name} response" | ||
|
||
trace_query = data_builder.build_trace_default_query(function_name=async_fn) | ||
|
||
# WHEN | ||
trace = helpers.get_traces( | ||
start_date=start_date, | ||
end_date=end_date, | ||
filter_expression=trace_filter_exporession, | ||
xray_client=boto3.client("xray"), | ||
) | ||
_, execution_time = data_fetcher.get_lambda_response(lambda_arn=async_fn_arn) | ||
|
||
# THEN | ||
info = helpers.find_trace_additional_info(trace=trace) | ||
print(info) | ||
handler_trace_segment = [trace_segment for trace_segment in info if trace_segment.name == "## lambda_handler"][0] | ||
collect_payment_trace_segment = [ | ||
trace_segment for trace_segment in info if trace_segment.name == "## collect_payment" | ||
][0] | ||
|
||
annotation_key = config["environment_variables"]["ANNOTATION_KEY"] | ||
expected_value = config["environment_variables"]["ANNOTATION_VALUE"] | ||
expected_async_value = config["environment_variables"]["ANNOTATION_ASYNC_VALUE"] | ||
|
||
assert handler_trace_segment.annotations["Service"] == "e2e-tests-app" | ||
assert handler_trace_segment.metadata["e2e-tests-app"][annotation_key] == expected_value | ||
assert collect_payment_trace_segment.metadata["e2e-tests-app"][annotation_key] == expected_async_value | ||
trace = data_fetcher.get_traces(start_date=execution_time, filter_expression=trace_query) | ||
|
||
assert len(trace.get_subsegment(name=async_fn_name_subsegment)) == 1 | ||
assert len(trace.get_metadata(key=async_fn_name_metadata_key, namespace=TracerStack.SERVICE_NAME)) == 1 |
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.
Uh oh!
There was an error while loading. Please reload this page.