|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +import time |
| 4 | +from typing import List |
| 5 | + |
| 6 | +from mock_collector_client import ResourceScopeMetric, ResourceScopeSpan |
| 7 | +from requests import Response, request |
| 8 | +from typing_extensions import override |
| 9 | + |
| 10 | +from amazon.base.contract_test_base import ContractTestBase |
| 11 | +from amazon.utils.application_signals_constants import ERROR_METRIC, FAULT_METRIC, LATENCY_METRIC |
| 12 | +from opentelemetry.sdk.metrics.export import AggregationTemporality |
| 13 | + |
| 14 | +# Tests in this class are supposed to validate that the SDK was configured in the correct way: It |
| 15 | +# uses the X-Ray ID format. Metrics are deltaPreferred. Type of the metrics are exponentialHistogram |
| 16 | + |
| 17 | + |
| 18 | +class ConfigurationTest(ContractTestBase): |
| 19 | + @override |
| 20 | + def get_application_image_name(self) -> str: |
| 21 | + return "aws-application-signals-tests-django-app" |
| 22 | + |
| 23 | + @override |
| 24 | + def get_application_wait_pattern(self) -> str: |
| 25 | + return "Quit the server with CONTROL-C." |
| 26 | + |
| 27 | + @override |
| 28 | + def get_application_extra_environment_variables(self): |
| 29 | + return {"DJANGO_SETTINGS_MODULE": "django_server.settings"} |
| 30 | + |
| 31 | + def test_configuration_metrics(self): |
| 32 | + address: str = self.application.get_container_host_ip() |
| 33 | + port: str = self.application.get_exposed_port(self.get_application_port()) |
| 34 | + url: str = f"http://{address}:{port}/success" |
| 35 | + response: Response = request("GET", url, timeout=20) |
| 36 | + self.assertEqual(200, response.status_code) |
| 37 | + metrics: List[ResourceScopeMetric] = self.mock_collector_client.get_metrics( |
| 38 | + {LATENCY_METRIC, ERROR_METRIC, FAULT_METRIC} |
| 39 | + ) |
| 40 | + |
| 41 | + self.assertEqual(len(metrics), 3) |
| 42 | + for metric in metrics: |
| 43 | + self.assertIsNotNone(metric.metric.exponential_histogram) |
| 44 | + self.assertEqual(metric.metric.exponential_histogram.aggregation_temporality, AggregationTemporality.DELTA) |
| 45 | + |
| 46 | + def test_xray_id_format(self): |
| 47 | + """ |
| 48 | + We are testing here that the X-Ray id format is always used by inspecting the traceid that |
| 49 | + was in the span received by the collector, which should be consistent across multiple spans. |
| 50 | + We are testing the following properties: |
| 51 | + 1. Traceid is random |
| 52 | + 2. First 32 bits of traceid is a timestamp |
| 53 | + It is important to remember that the X-Ray traceId format had to be adapted to fit into the |
| 54 | + definition of the OpenTelemetry traceid: |
| 55 | + https://opentelemetry.io/docs/specs/otel/trace/api/#retrieving-the-traceid-and-spanid |
| 56 | + Specifically for an X-Ray traceid to be a valid Otel traceId, the version digit had to be |
| 57 | + dropped. Reference: |
| 58 | + https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/aws_xray_id_generator.py |
| 59 | + """ |
| 60 | + |
| 61 | + seen: List[str] = [] |
| 62 | + for _ in range(100): |
| 63 | + address: str = self.application.get_container_host_ip() |
| 64 | + port: str = self.application.get_exposed_port(self.get_application_port()) |
| 65 | + url: str = f"http://{address}:{port}/success" |
| 66 | + response: Response = request("GET", url, timeout=20) |
| 67 | + self.assertEqual(200, response.status_code) |
| 68 | + |
| 69 | + # Since we just made the request, the time in epoch registered in the traceid should be |
| 70 | + # approximate equal to the current time in the test, since both run on the same host. |
| 71 | + start_time_sec: int = int(time.time()) |
| 72 | + |
| 73 | + resource_scope_spans: List[ResourceScopeSpan] = self.mock_collector_client.get_traces() |
| 74 | + target_span: ResourceScopeSpan = resource_scope_spans[0] |
| 75 | + self.assertEqual(target_span.span.name, "GET success") |
| 76 | + |
| 77 | + self.assertTrue(target_span.span.trace_id.hex() not in seen) |
| 78 | + seen.append(target_span.span.trace_id.hex()) |
| 79 | + |
| 80 | + # trace_id is bytes, so we convert it to hex string and pick the first 8 byte |
| 81 | + # that represent the timestamp, then convert it to int for timestamp in second |
| 82 | + trace_id_time_stamp_int: int = int(target_span.span.trace_id.hex()[:8], 16) |
| 83 | + |
| 84 | + # Give 2 minutes time range of tolerance for the trace timestamp |
| 85 | + self.assertGreater(trace_id_time_stamp_int, start_time_sec - 60) |
| 86 | + self.assertGreater(start_time_sec + 60, trace_id_time_stamp_int) |
| 87 | + self.mock_collector_client.clear_signals() |
0 commit comments