Skip to content
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

Implement Unit Test for Tracer Configurer Module #38

Merged
merged 20 commits into from
Feb 7, 2024
1 change: 1 addition & 0 deletions aws-opentelemetry-distro/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies = [
"opentelemetry-api ~= 1.12",
"opentelemetry-instrumentation == 0.43b0",
"opentelemetry-sdk ~= 1.13",
"opentelemetry-distro == 0.43b0",
thpierce marked this conversation as resolved.
Show resolved Hide resolved
"opentelemetry-sdk-extension-aws ~= 2.0.1",
"opentelemetry-exporter-otlp-proto-grpc == 1.22.0"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from amazon.opentelemetry.distro.attribute_propagating_span_processor import AttributePropagatingSpanProcessor
from opentelemetry.sdk.trace import ReadableSpan, Span, Tracer, TracerProvider
from opentelemetry.semconv.trace import MessagingOperationValues, SpanAttributes
from opentelemetry.trace import SpanContext, SpanKind, TraceFlags, TraceState, set_span_in_context, set_tracer_provider
from opentelemetry.trace import SpanContext, SpanKind, TraceFlags, TraceState, set_span_in_context


def _get_ingress_operation(span: Span):
Expand Down Expand Up @@ -36,7 +36,6 @@ def setUp(self):
sampler=None,
resource=None,
)
set_tracer_provider(self.provider)
self.provider.add_span_processor(self.processor)
self.tracer: Tracer = self.provider.get_tracer("awsxray")

Expand Down
thpierce marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +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 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):
thpierce marked this conversation as resolved.
Show resolved Hide resolved
@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)
Loading