|
| 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 | + @staticmethod |
| 21 | + def get_application_image_name() -> str: |
| 22 | + return "aws-application-signals-tests-appsignals.netcore-app" |
| 23 | + |
| 24 | + @override |
| 25 | + def get_application_wait_pattern(self) -> str: |
| 26 | + return "Content root path: /app" |
| 27 | + |
| 28 | + @override |
| 29 | + def get_application_extra_environment_variables(self): |
| 30 | + return {"OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED": "true"} |
| 31 | + |
| 32 | + def test_configuration_metrics(self): |
| 33 | + address: str = self.application.get_container_host_ip() |
| 34 | + port: str = self.application.get_exposed_port(self.get_application_port()) |
| 35 | + url: str = f"http://{address}:{port}/success" |
| 36 | + response: Response = request("GET", url, timeout=20) |
| 37 | + self.assertEqual(200, response.status_code) |
| 38 | + metrics: List[ResourceScopeMetric] = self.mock_collector_client.get_metrics( |
| 39 | + {LATENCY_METRIC, ERROR_METRIC, FAULT_METRIC} |
| 40 | + ) |
| 41 | + |
| 42 | + self.assertEqual(len(metrics), 3) |
| 43 | + for metric in metrics: |
| 44 | + self.assertIsNotNone(metric.metric.exponential_histogram) |
| 45 | + self.assertEqual(metric.metric.exponential_histogram.aggregation_temporality, AggregationTemporality.DELTA) |
| 46 | + |
| 47 | + # TODO: This does not work as expected, gives errors like 1467067824 not greater than 1746657330 |
| 48 | + # def test_xray_id_format(self): |
| 49 | + # """ |
| 50 | + # We are testing here that the X-Ray id format is always used by inspecting the traceid that |
| 51 | + # was in the span received by the collector, which should be consistent across multiple spans. |
| 52 | + # We are testing the following properties: |
| 53 | + # 1. Traceid is random |
| 54 | + # 2. First 32 bits of traceid is a timestamp |
| 55 | + # It is important to remember that the X-Ray traceId format had to be adapted to fit into the |
| 56 | + # definition of the OpenTelemetry traceid: |
| 57 | + # https://opentelemetry.io/docs/specs/otel/trace/api/#retrieving-the-traceid-and-spanid |
| 58 | + # Specifically for an X-Ray traceid to be a valid Otel traceId, the version digit had to be |
| 59 | + # dropped. Reference: |
| 60 | + # 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 |
| 61 | + # """ |
| 62 | + # |
| 63 | + # seen: List[str] = [] |
| 64 | + # for _ in range(100): |
| 65 | + # address: str = self.application.get_container_host_ip() |
| 66 | + # port: str = self.application.get_exposed_port(self.get_application_port()) |
| 67 | + # url: str = f"http://{address}:{port}/success" |
| 68 | + # response: Response = request("GET", url, timeout=20) |
| 69 | + # self.assertEqual(200, response.status_code) |
| 70 | + # |
| 71 | + # # Since we just made the request, the time in epoch registered in the traceid should be |
| 72 | + # # approximate equal to the current time in the test, since both run on the same host. |
| 73 | + # start_time_sec: int = int(time.time()) |
| 74 | + # |
| 75 | + # resource_scope_spans: List[ResourceScopeSpan] = self.mock_collector_client.get_traces() |
| 76 | + # target_span: ResourceScopeSpan = resource_scope_spans[0] |
| 77 | + # self.assertEqual(target_span.span.name, "GET /success") |
| 78 | + # |
| 79 | + # self.assertTrue(target_span.span.trace_id.hex() not in seen) |
| 80 | + # seen.append(target_span.span.trace_id.hex()) |
| 81 | + # |
| 82 | + # # trace_id is bytes, so we convert it to hex string and pick the first 8 byte |
| 83 | + # # that represent the timestamp, then convert it to int for timestamp in second |
| 84 | + # trace_id_time_stamp_int: int = int(target_span.span.trace_id.hex()[:8], 16) |
| 85 | + # |
| 86 | + # # Give 2 minutes time range of tolerance for the trace timestamp |
| 87 | + # self.assertGreater(trace_id_time_stamp_int, start_time_sec - 60) |
| 88 | + # self.assertGreater(start_time_sec + 60, trace_id_time_stamp_int) |
| 89 | + # self.mock_collector_client.clear_signals() |
0 commit comments