From 7c08b0630eda9a3cd0b66298f8be8a5ce89939b0 Mon Sep 17 00:00:00 2001 From: Thomas Pierce Date: Thu, 11 Jan 2024 09:35:18 -0800 Subject: [PATCH] Various fixes In this commit we are fixing various things: * Update project path to start with amazon, not opentelemetry. * Update isort to consider amazon first party. * Fix imports with project path and isort update. * Update some modules/classes to be internal, based on package private classes in Java implementation. * Add basic unit tests to ensure imports are working as expected and setting up the foundation to implement full unit tests later. --- .isort.cfg | 2 +- aws-opentelemetry-distro/pyproject.toml | 6 +++--- .../{aws_attribute_keys.py => _aws_attribute_keys.py} | 0 ...enerator.py => _aws_metric_attribute_generator.py} | 5 ++--- .../distro/aws_opentelemetry_configurator.py | 1 - .../opentelemetry/distro/aws_opentelemetry_distro.py | 1 - .../distro/aws_span_metrics_processor.py | 2 +- .../distro/aws_span_metrics_processor_builder.py | 9 ++++----- .../distro/test_aws_metric_attribute_generator.py | 11 +++++++++++ .../distro/test_aws_opentelementry_configurator.py | 6 ++---- .../distro/test_aws_opentelemetry_distro.py | 1 - .../distro/test_aws_span_metrics_processor.py | 11 +++++++++++ .../distro/test_aws_span_metrics_processor_builder.py | 11 +++++++++++ 13 files changed, 46 insertions(+), 20 deletions(-) rename aws-opentelemetry-distro/src/amazon/opentelemetry/distro/{aws_attribute_keys.py => _aws_attribute_keys.py} (100%) rename aws-opentelemetry-distro/src/amazon/opentelemetry/distro/{aws_metric_attribute_generator.py => _aws_metric_attribute_generator.py} (88%) create mode 100644 aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attribute_generator.py create mode 100644 aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor.py create mode 100644 aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor_builder.py diff --git a/.isort.cfg b/.isort.cfg index 65861ec45..4b689e9f6 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -15,5 +15,5 @@ profile=black multi_line_output=3 skip=target skip_glob=**/gen/*,.venv*/*,venv*/*,.tox/* -known_first_party=opentelemetry +known_first_party=opentelemetry,amazon known_third_party=psutil,pytest diff --git a/aws-opentelemetry-distro/pyproject.toml b/aws-opentelemetry-distro/pyproject.toml index 3d337e154..963e29540 100644 --- a/aws-opentelemetry-distro/pyproject.toml +++ b/aws-opentelemetry-distro/pyproject.toml @@ -21,10 +21,10 @@ dependencies = [ test = [] [project.entry-points.opentelemetry_configurator] -aws_configurator = "opentelemetry.distro.aws_opentelemetry_configurator:AwsOpenTelemetryConfigurator" +aws_configurator = "amazon.opentelemetry.distro.aws_opentelemetry_configurator:AwsOpenTelemetryConfigurator" [project.entry-points.opentelemetry_distro] -aws_distro = "opentelemetry.distro.aws_opentelemetry_distro:AwsOpenTelemetryDistro" +aws_distro = "amazon.opentelemetry.distro.aws_opentelemetry_distro:AwsOpenTelemetryDistro" [project.urls] Homepage = "https://github.com/aws-observability/aws-otel-python-instrumentation/tree/main/aws-opentelemetry-distro" @@ -39,4 +39,4 @@ include = [ ] [tool.hatch.build.targets.wheel] -packages = ["src/amazon/opentelemetry"] +packages = ["src/amazon"] diff --git a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_attribute_keys.py b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_aws_attribute_keys.py similarity index 100% rename from aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_attribute_keys.py rename to aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_aws_attribute_keys.py diff --git a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_metric_attribute_generator.py b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_aws_metric_attribute_generator.py similarity index 88% rename from aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_metric_attribute_generator.py rename to aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_aws_metric_attribute_generator.py index 2bb8f8752..d71069f53 100644 --- a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_metric_attribute_generator.py +++ b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_aws_metric_attribute_generator.py @@ -1,12 +1,11 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 -from metric_attribute_generator import MetricAttributeGenerator - +from amazon.opentelemetry.distro.metric_attribute_generator import MetricAttributeGenerator from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import BoundedAttributes, ReadableSpan -class AwsMetricAttributeGenerator(MetricAttributeGenerator): +class _AwsMetricAttributeGenerator(MetricAttributeGenerator): """AwsMetricAttributeGenerator generates specific metric attributes for incoming and outgoing traffic. AwsMetricAttributeGenerator generates very specific metric attributes based on low-cardinality span and resource diff --git a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py index 4515cd9a9..39b2822df 100644 --- a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py +++ b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py @@ -1,6 +1,5 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 - from opentelemetry.sdk._configuration import _BaseConfigurator from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter diff --git a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_distro.py b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_distro.py index 6730e4ba3..f71116074 100644 --- a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_distro.py +++ b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_distro.py @@ -1,6 +1,5 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 - from logging import getLogger from opentelemetry.instrumentation.distro import BaseDistro diff --git a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor.py b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor.py index 11a47dba8..c0ba2bbb1 100644 --- a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor.py +++ b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor.py @@ -2,9 +2,9 @@ # SPDX-License-Identifier: Apache-2.0 from typing import Optional -from metric_attribute_generator import MetricAttributeGenerator from typing_extensions import override +from amazon.opentelemetry.distro.metric_attribute_generator import MetricAttributeGenerator from opentelemetry.context import Context from opentelemetry.metrics import Histogram from opentelemetry.sdk.resources import Resource diff --git a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor_builder.py b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor_builder.py index d1fd2929e..c70ee7651 100644 --- a/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor_builder.py +++ b/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor_builder.py @@ -1,9 +1,8 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 -from aws_metric_attribute_generator import AwsMetricAttributeGenerator -from aws_span_metrics_processor import AwsSpanMetricsProcessor -from metric_attribute_generator import MetricAttributeGenerator - +from amazon.opentelemetry.distro._aws_metric_attribute_generator import _AwsMetricAttributeGenerator +from amazon.opentelemetry.distro.aws_span_metrics_processor import AwsSpanMetricsProcessor +from amazon.opentelemetry.distro.metric_attribute_generator import MetricAttributeGenerator from opentelemetry.sdk.metrics import Histogram, Meter, MeterProvider from opentelemetry.sdk.resources import Resource @@ -14,7 +13,7 @@ _LATENCY_UNITS: str = "Milliseconds" # Defaults -_DEFAULT_GENERATOR: MetricAttributeGenerator = AwsMetricAttributeGenerator() +_DEFAULT_GENERATOR: MetricAttributeGenerator = _AwsMetricAttributeGenerator() _DEFAULT_SCOPE_NAME: str = "AwsSpanMetricsProcessor" diff --git a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attribute_generator.py b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attribute_generator.py new file mode 100644 index 000000000..ef9ef7e19 --- /dev/null +++ b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attribute_generator.py @@ -0,0 +1,11 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +from unittest import TestCase + +from amazon.opentelemetry.distro._aws_metric_attribute_generator import _AwsMetricAttributeGenerator + + +class TestAwsMetricAttributeGenerator(TestCase): + def test_basic(self): + generator: _AwsMetricAttributeGenerator = _AwsMetricAttributeGenerator() + self.assertEqual(generator.generate_metric_attributes_dict_from_span(None, None), {}) 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 f96c3edcb..ca6bc0b26 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,15 +1,13 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 - from unittest import TestCase -from opentelemetry.distro.aws_opentelemetry_configurator import AwsOpenTelemetryConfigurator, AwsTracerProvider +from amazon.opentelemetry.distro.aws_opentelemetry_configurator import AwsOpenTelemetryConfigurator, AwsTracerProvider class TestAwsOpenTelemetryConfigurator(TestCase): - # pylint: disable=no-self-use def test_default_configuration(self): configurator = AwsOpenTelemetryConfigurator() configurator.configure() trace_provider = configurator.get_trace_provider() - assert isinstance(trace_provider, AwsTracerProvider) + self.assertTrue(isinstance(trace_provider, AwsTracerProvider)) diff --git a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelemetry_distro.py b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelemetry_distro.py index 859ade167..b77e4fbf8 100644 --- a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelemetry_distro.py +++ b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelemetry_distro.py @@ -1,6 +1,5 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 - from unittest import TestCase from pkg_resources import DistributionNotFound, require diff --git a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor.py b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor.py new file mode 100644 index 000000000..ee47bfd9f --- /dev/null +++ b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor.py @@ -0,0 +1,11 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +from unittest import TestCase + +from amazon.opentelemetry.distro.aws_span_metrics_processor import AwsSpanMetricsProcessor + + +class TestAwsSpanMetricsProcessor(TestCase): + def test_basic(self): + processor: AwsSpanMetricsProcessor = AwsSpanMetricsProcessor(None, None, None, None, None) + self.assertTrue(processor.force_flush) diff --git a/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor_builder.py b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor_builder.py new file mode 100644 index 000000000..17c28732e --- /dev/null +++ b/aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor_builder.py @@ -0,0 +1,11 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +from unittest import TestCase + +from amazon.opentelemetry.distro.aws_span_metrics_processor_builder import AwsSpanMetricsProcessorBuilder + + +class TestAwsSpanMetricsProcessorBuilder(TestCase): + def test_basic(self): + builder: AwsSpanMetricsProcessorBuilder = AwsSpanMetricsProcessorBuilder(None, None) + self.assertIs(builder.set_scope_name("test"), builder)