From 340be9dd5cb4fd0cb368b8ec10a8947d37811dfe Mon Sep 17 00:00:00 2001 From: ADOT Patch workflow Date: Wed, 7 Feb 2024 12:05:27 -0800 Subject: [PATCH] Implement test for builder and move tracer configurer test to correct location --- ...bute_propagating_span_processor_builder.py | 8 +++ ...metric_attributes_span_exporter_builder.py | 3 ++ .../test_aws_opentelementry_configurator.py | 50 +++++++++++++++-- .../distro/test_aws_tracer_configurer.py | 54 ------------------- 4 files changed, 57 insertions(+), 58 deletions(-) delete mode 100644 aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_tracer_configurer.py diff --git a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_attribute_propagating_span_processor_builder.py b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_attribute_propagating_span_processor_builder.py index dbd30969b..6f5286fce 100644 --- a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_attribute_propagating_span_processor_builder.py +++ b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_attribute_propagating_span_processor_builder.py @@ -2,6 +2,8 @@ # SPDX-License-Identifier: Apache-2.0 from unittest import TestCase +from amazon.opentelemetry.distro._aws_span_processing_util import get_ingress_operation +from amazon.opentelemetry.distro.attribute_propagating_span_processor import AttributePropagatingSpanProcessor from amazon.opentelemetry.distro.attribute_propagating_span_processor_builder import ( AttributePropagatingSpanProcessorBuilder, ) @@ -11,3 +13,9 @@ class TestAttributePropagatingSpanProcessorBuilder(TestCase): def test_basic(self): builder: AttributePropagatingSpanProcessorBuilder = AttributePropagatingSpanProcessorBuilder() self.assertIs(builder.set_propagation_data_key("test"), builder) + self.assertIs(builder.set_propagation_data_extractor(get_ingress_operation), builder) + self.assertIs(builder.set_attributes_keys_to_propagate(["test"]), builder) + span_processor: AttributePropagatingSpanProcessor = builder.build() + self.assertIs(span_processor._propagation_data_key, "test") + self.assertIs(span_processor._propagation_data_extractor, get_ingress_operation) + self.assertEqual(span_processor._attribute_keys_to_propagate, tuple(["test"])) diff --git a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attributes_span_exporter_builder.py b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attributes_span_exporter_builder.py index c76440e18..6e864fd4c 100644 --- a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attributes_span_exporter_builder.py +++ b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attributes_span_exporter_builder.py @@ -3,6 +3,7 @@ from unittest import TestCase from amazon.opentelemetry.distro._aws_metric_attribute_generator import _AwsMetricAttributeGenerator +from amazon.opentelemetry.distro.aws_metric_attributes_span_exporter import AwsMetricAttributesSpanExporter from amazon.opentelemetry.distro.aws_metric_attributes_span_exporter_builder import ( AwsMetricAttributesSpanExporterBuilder, ) @@ -13,3 +14,5 @@ def test_basic(self): generator: _AwsMetricAttributeGenerator = _AwsMetricAttributeGenerator() builder: AwsMetricAttributesSpanExporterBuilder = AwsMetricAttributesSpanExporterBuilder(None, None) self.assertIs(builder.set_generator(generator), builder) + exporter: AwsMetricAttributesSpanExporter = builder.build() + self.assertIs(exporter._generator, generator) diff --git a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py index 9910c1fe8..8d9775bb8 100644 --- a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py +++ b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py @@ -1,12 +1,54 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 +import os +import time from unittest import TestCase from amazon.opentelemetry.distro.aws_opentelemetry_configurator import AwsOpenTelemetryConfigurator -from opentelemetry.sdk._configuration import _OTelSDKConfigurator +from amazon.opentelemetry.distro.aws_opentelemetry_distro import AwsOpenTelemetryDistro +from opentelemetry.environment_variables import OTEL_LOGS_EXPORTER, OTEL_METRICS_EXPORTER, OTEL_TRACES_EXPORTER +from opentelemetry.sdk.environment_variables import OTEL_TRACES_SAMPLER, OTEL_TRACES_SAMPLER_ARG +from opentelemetry.sdk.trace import Span, Tracer, TracerProvider +from opentelemetry.trace import get_tracer_provider +# This class setup Tracer Provider Globally, which can only set once +# if there is another setup for tracer provider, may cause issue class TestAwsOpenTelemetryConfigurator(TestCase): - def test_default_configuration(self): - configurator = AwsOpenTelemetryConfigurator() - self.assertTrue(isinstance(configurator, _OTelSDKConfigurator)) + @classmethod + def setUpClass(cls): + os.environ.setdefault(OTEL_TRACES_EXPORTER, "none") + os.environ.setdefault(OTEL_METRICS_EXPORTER, "none") + os.environ.setdefault(OTEL_LOGS_EXPORTER, "none") + os.environ.setdefault(OTEL_TRACES_SAMPLER, "traceidratio") + os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, "0.01") + aws_open_telemetry_distro: AwsOpenTelemetryDistro = AwsOpenTelemetryDistro() + aws_open_telemetry_distro.configure() + aws_otel_configurator: AwsOpenTelemetryConfigurator = AwsOpenTelemetryConfigurator() + aws_otel_configurator.configure() + cls.tracer_provider: TracerProvider = get_tracer_provider() + + # The probability of this passing once without correct IDs is low, 20 times is inconceivable. + def test_provide_generate_xray_ids(self): + for _ in range(20): + tracer: Tracer = self.tracer_provider.get_tracer("test") + start_time_sec: int = int(time.time()) + span: Span = tracer.start_span("test") + trace_id: int = span.get_span_context().trace_id + trace_id_4_byte_hex: str = hex(trace_id)[2:10] + trace_id_4_byte_int: int = int(trace_id_4_byte_hex, 16) + self.assertGreaterEqual(trace_id_4_byte_int, start_time_sec) + + # Sanity check that the trace ID ratio sampler works fine with the x-ray generator. + def test_trace_id_ratio_sampler(self): + for _ in range(20): + num_spans: int = 100000 + num_sampled: int = 0 + tracer: Tracer = self.tracer_provider.get_tracer("test") + for _ in range(num_spans): + span: Span = tracer.start_span("test") + if span.get_span_context().trace_flags.sampled: + num_sampled += 1 + span.end() + # Configured for 1%, confirm there are at most 5% to account for randomness and reduce test flakiness. + self.assertGreater(0.05, num_sampled / num_spans) diff --git a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_tracer_configurer.py b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_tracer_configurer.py deleted file mode 100644 index 9f93e8314..000000000 --- a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_tracer_configurer.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -# SPDX-License-Identifier: Apache-2.0 -import os -import time -from unittest import TestCase - -from amazon.opentelemetry.distro.aws_opentelemetry_configurator import AwsOpenTelemetryConfigurator -from amazon.opentelemetry.distro.aws_opentelemetry_distro import AwsOpenTelemetryDistro -from opentelemetry.environment_variables import OTEL_LOGS_EXPORTER, OTEL_METRICS_EXPORTER, OTEL_TRACES_EXPORTER -from opentelemetry.sdk.environment_variables import OTEL_TRACES_SAMPLER, OTEL_TRACES_SAMPLER_ARG -from opentelemetry.sdk.trace import Span, Tracer, TracerProvider -from opentelemetry.trace import get_tracer_provider - - -# This class setup Tracer Provider Globally, which can only set once -# if there is another setup for tracer provider, may cause issue -class TestAwsTracerConfigurer(TestCase): - @classmethod - def setUpClass(cls): - os.environ.setdefault(OTEL_TRACES_EXPORTER, "none") - os.environ.setdefault(OTEL_METRICS_EXPORTER, "none") - os.environ.setdefault(OTEL_LOGS_EXPORTER, "none") - os.environ.setdefault(OTEL_TRACES_SAMPLER, "traceidratio") - os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, "0.01") - aws_open_telemetry_distro: AwsOpenTelemetryDistro = AwsOpenTelemetryDistro() - aws_open_telemetry_distro.configure() - aws_otel_configurator: AwsOpenTelemetryConfigurator = AwsOpenTelemetryConfigurator() - aws_otel_configurator.configure() - cls.tracer_provider: TracerProvider = get_tracer_provider() - - # The probability of this passing once without correct IDs is low, 20 times is inconceivable. - def test_provide_generate_xray_ids(self): - for _ in range(20): - tracer: Tracer = self.tracer_provider.get_tracer("test") - start_time_sec: int = int(time.time()) - span: Span = tracer.start_span("test") - trace_id: int = span.get_span_context().trace_id - trace_id_4_byte_hex: str = hex(trace_id)[2:10] - trace_id_4_byte_int: int = int(trace_id_4_byte_hex, 16) - self.assertGreaterEqual(trace_id_4_byte_int, start_time_sec) - - # Sanity check that the trace ID ratio sampler works fine with the x-ray generator. - def test_trace_id_ratio_sampler(self): - for _ in range(20): - num_spans: int = 100000 - num_sampled: int = 0 - tracer: Tracer = self.tracer_provider.get_tracer("test") - for _ in range(num_spans): - span: Span = tracer.start_span("test") - if span.get_span_context().trace_flags.sampled: - num_sampled += 1 - span.end() - # Configured for 1%, confirm there are at most 5% to account for randomness and reduce test flakiness. - self.assertGreater(0.05, num_sampled / num_spans)