Skip to content

Commit

Permalink
Merge from main.
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhlogin committed Feb 7, 2024
2 parents 7b1af3a + 596dda6 commit 26f893f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 62 deletions.
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ cd ./aws-opentelemetry-distro
python3 -m build --outdir ../dist
cd ../dist
pkg_version=$(grep '__version__' ../aws-opentelemetry-distro/src/amazon/opentelemetry/distro/version.py | awk -F '"' '{print $2}')
pip wheel --no-deps aws_opentelemetry_distro-${pkg_version}.tar.gz
pip install aws_opentelemetry_distro-${pkg_version}-py3-none-any.whl --force-reinstall
cd ..
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
_OTelSDKConfigurator,
)
from opentelemetry.sdk.environment_variables import _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED
from opentelemetry.sdk.extension.aws.resource.ec2 import AwsEc2ResourceDetector
from opentelemetry.sdk.extension.aws.resource.ecs import AwsEcsResourceDetector
from opentelemetry.sdk.extension.aws.resource.eks import AwsEksResourceDetector
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics._internal.instrument import (
Counter,
Expand All @@ -37,7 +40,7 @@
UpDownCounter,
)
from opentelemetry.sdk.metrics.export import AggregationTemporality, PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.resources import Resource, get_aggregated_resources
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter
from opentelemetry.sdk.trace.id_generator import IdGenerator
Expand Down Expand Up @@ -88,7 +91,14 @@ def _initialize_components(auto_instrumentation_version):
# populate version if using auto-instrumentation
if auto_instrumentation_version:
auto_resource[ResourceAttributes.TELEMETRY_AUTO_VERSION] = auto_instrumentation_version
resource = Resource.create(auto_resource)

resource = get_aggregated_resources(
[
AwsEc2ResourceDetector(),
AwsEksResourceDetector(),
AwsEcsResourceDetector(),
]
).merge(Resource.create(auto_resource))

_init_tracing(
exporters=trace_exporters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def build(self) -> AwsSpanMetricsProcessor:
error_histogram: Histogram = meter.create_histogram(_ERROR)
fault_histogram: Histogram = meter.create_histogram(_FAULT)
latency_histogram: Histogram = meter.create_histogram(_LATENCY, unit=_LATENCY_UNITS)
# TODO: Remove the Histogram name override after the CWAgent is fixed with metric name case-insensitive.
error_histogram.name = _ERROR
fault_histogram.name = _FAULT
latency_histogram.name = _LATENCY

return AwsSpanMetricsProcessor(
error_histogram, fault_histogram, latency_histogram, self._generator, self._resource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from unittest import TestCase
from unittest.mock import MagicMock

from amazon.opentelemetry.distro.attribute_propagating_span_processor import AttributePropagatingSpanProcessor
from amazon.opentelemetry.distro.attribute_propagating_span_processor_builder import (
AttributePropagatingSpanProcessorBuilder,
)
from opentelemetry.sdk.trace import ReadableSpan


class TestAttributePropagatingSpanProcessorBuilder(TestCase):
def test_basic(self):
builder: AttributePropagatingSpanProcessorBuilder = AttributePropagatingSpanProcessorBuilder()
self.assertIs(builder.set_propagation_data_key("test"), builder)

def mock_extractor(_: ReadableSpan) -> str:
return "test"

self.assertIs(builder.set_propagation_data_extractor(mock_extractor), 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.assertEqual(span_processor._propagation_data_extractor(MagicMock()), "test")
self.assertEqual(span_processor._attribute_keys_to_propagate, tuple(["test"]))
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from unittest import TestCase
from unittest.mock import MagicMock

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,
)


class TestAwsMetricAttributesSpanExporterBuilder(TestCase):
def test_basic(self):
generator: _AwsMetricAttributeGenerator = _AwsMetricAttributeGenerator()
generator: _AwsMetricAttributeGenerator = MagicMock()
generator.test_key = "test"
builder: AwsMetricAttributesSpanExporterBuilder = AwsMetricAttributesSpanExporterBuilder(None, None)
self.assertIs(builder.set_generator(generator), builder)
exporter: AwsMetricAttributesSpanExporter = builder.build()
self.assertIs(exporter._generator.test_key, "test")
Original file line number Diff line number Diff line change
@@ -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)

This file was deleted.

0 comments on commit 26f893f

Please sign in to comment.