|
1 | | -import datetime |
2 | | -import uuid |
3 | | - |
4 | | -import boto3 |
5 | 1 | import pytest |
6 | | -from e2e import conftest |
7 | | -from e2e.utils import helpers |
| 2 | + |
| 3 | +from tests.e2e.tracer.handlers import async_capture, basic_handler |
| 4 | +from tests.e2e.tracer.infrastructure import TracerStack |
| 5 | +from tests.e2e.utils import data_builder, data_fetcher |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def basic_handler_fn_arn(infrastructure: dict) -> str: |
| 10 | + return infrastructure.get("BasicHandlerArn", "") |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def basic_handler_fn(infrastructure: dict) -> str: |
| 15 | + return infrastructure.get("BasicHandler", "") |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def async_fn_arn(infrastructure: dict) -> str: |
| 20 | + return infrastructure.get("AsyncCaptureArn", "") |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def async_fn(infrastructure: dict) -> str: |
| 25 | + return infrastructure.get("AsyncCapture", "") |
8 | 26 |
|
9 | 27 |
|
10 | | -@pytest.fixture(scope="module") |
11 | | -def config() -> conftest.LambdaConfig: |
12 | | - return { |
13 | | - "parameters": {"tracing": "ACTIVE"}, |
14 | | - "environment_variables": { |
15 | | - "ANNOTATION_KEY": f"e2e-tracer-{str(uuid.uuid4()).replace('-','_')}", |
16 | | - "ANNOTATION_VALUE": "stored", |
17 | | - "ANNOTATION_ASYNC_VALUE": "payments", |
18 | | - }, |
19 | | - } |
| 28 | +def test_lambda_handler_trace_is_visible(basic_handler_fn_arn: str, basic_handler_fn: str): |
| 29 | + # GIVEN |
| 30 | + handler_name = basic_handler.lambda_handler.__name__ |
| 31 | + handler_subsegment = f"## {handler_name}" |
| 32 | + handler_metadata_key = f"{handler_name} response" |
| 33 | + |
| 34 | + method_name = basic_handler.get_todos.__name__ |
| 35 | + method_subsegment = f"## {method_name}" |
| 36 | + handler_metadata_key = f"{method_name} response" |
| 37 | + |
| 38 | + trace_query = data_builder.build_trace_default_query(function_name=basic_handler_fn) |
| 39 | + |
| 40 | + # WHEN |
| 41 | + _, execution_time = data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn) |
| 42 | + data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn) |
| 43 | + |
| 44 | + # THEN |
| 45 | + trace = data_fetcher.get_traces(start_date=execution_time, filter_expression=trace_query, minimum_traces=2) |
| 46 | + |
| 47 | + assert len(trace.get_annotation(key="ColdStart", value=True)) == 1 |
| 48 | + assert len(trace.get_metadata(key=handler_metadata_key, namespace=TracerStack.SERVICE_NAME)) == 2 |
| 49 | + assert len(trace.get_metadata(key=handler_metadata_key, namespace=TracerStack.SERVICE_NAME)) == 2 |
| 50 | + assert len(trace.get_subsegment(name=handler_subsegment)) == 2 |
| 51 | + assert len(trace.get_subsegment(name=method_subsegment)) == 2 |
20 | 52 |
|
21 | 53 |
|
22 | | -def test_basic_lambda_async_trace_visible(execute_lambda: conftest.InfrastructureOutput, config: conftest.LambdaConfig): |
| 54 | +def test_async_trace_is_visible(async_fn_arn: str, async_fn: str): |
23 | 55 | # GIVEN |
24 | | - lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="basichandlerarn") |
25 | | - start_date = execute_lambda.get_lambda_execution_time() |
26 | | - end_date = start_date + datetime.timedelta(minutes=5) |
27 | | - trace_filter_exporession = f'service("{lambda_name}")' |
| 56 | + async_fn_name = async_capture.async_get_users.__name__ |
| 57 | + async_fn_name_subsegment = f"## {async_fn_name}" |
| 58 | + async_fn_name_metadata_key = f"{async_fn_name} response" |
| 59 | + |
| 60 | + trace_query = data_builder.build_trace_default_query(function_name=async_fn) |
28 | 61 |
|
29 | 62 | # WHEN |
30 | | - trace = helpers.get_traces( |
31 | | - start_date=start_date, |
32 | | - end_date=end_date, |
33 | | - filter_expression=trace_filter_exporession, |
34 | | - xray_client=boto3.client("xray"), |
35 | | - ) |
| 63 | + _, execution_time = data_fetcher.get_lambda_response(lambda_arn=async_fn_arn) |
36 | 64 |
|
37 | 65 | # THEN |
38 | | - info = helpers.find_trace_additional_info(trace=trace) |
39 | | - print(info) |
40 | | - handler_trace_segment = [trace_segment for trace_segment in info if trace_segment.name == "## lambda_handler"][0] |
41 | | - collect_payment_trace_segment = [ |
42 | | - trace_segment for trace_segment in info if trace_segment.name == "## collect_payment" |
43 | | - ][0] |
44 | | - |
45 | | - annotation_key = config["environment_variables"]["ANNOTATION_KEY"] |
46 | | - expected_value = config["environment_variables"]["ANNOTATION_VALUE"] |
47 | | - expected_async_value = config["environment_variables"]["ANNOTATION_ASYNC_VALUE"] |
48 | | - |
49 | | - assert handler_trace_segment.annotations["Service"] == "e2e-tests-app" |
50 | | - assert handler_trace_segment.metadata["e2e-tests-app"][annotation_key] == expected_value |
51 | | - assert collect_payment_trace_segment.metadata["e2e-tests-app"][annotation_key] == expected_async_value |
| 66 | + trace = data_fetcher.get_traces(start_date=execution_time, filter_expression=trace_query) |
| 67 | + |
| 68 | + assert len(trace.get_subsegment(name=async_fn_name_subsegment)) == 1 |
| 69 | + assert len(trace.get_metadata(key=async_fn_name_metadata_key, namespace=TracerStack.SERVICE_NAME)) == 1 |
0 commit comments