From 0348b3d4281d9500f6785f0dcf9fe53fe800e268 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sat, 7 Jan 2023 03:56:16 +0530 Subject: [PATCH 1/7] create single resource instance --- .../sdk/_configuration/__init__.py | 53 ++++------- opentelemetry-sdk/tests/test_configurator.py | 92 ++++++++----------- 2 files changed, 58 insertions(+), 87 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index a1ef3b76a27..873b11fb0d0 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -177,20 +177,12 @@ def _init_tracing( exporters: Dict[str, Type[SpanExporter]], id_generator: IdGenerator = None, sampler: Sampler = None, - auto_instrumentation_version: Optional[str] = None, + resource: Resource = None, ): - # if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name - # from the env variable else defaults to "unknown_service" - auto_resource = {} - # populate version if using auto-instrumentation - if auto_instrumentation_version: - auto_resource[ - ResourceAttributes.TELEMETRY_AUTO_VERSION - ] = auto_instrumentation_version provider = TracerProvider( id_generator=id_generator, sampler=sampler, - resource=Resource.create(auto_resource), + resource=resource, ) set_tracer_provider(provider) @@ -203,17 +195,8 @@ def _init_tracing( def _init_metrics( exporters: Dict[str, Type[MetricExporter]], - auto_instrumentation_version: Optional[str] = None, + resource: Resource = None, ): - # if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name - # from the env variable else defaults to "unknown_service" - auto_resource = {} - # populate version if using auto-instrumentation - if auto_instrumentation_version: - auto_resource[ - ResourceAttributes.TELEMETRY_AUTO_VERSION - ] = auto_instrumentation_version - metric_readers = [] for _, exporter_class in exporters.items(): @@ -223,24 +206,16 @@ def _init_metrics( ) provider = MeterProvider( - resource=Resource.create(auto_resource), metric_readers=metric_readers + resource=resource, metric_readers=metric_readers ) set_meter_provider(provider) def _init_logging( exporters: Dict[str, Type[LogExporter]], - auto_instrumentation_version: Optional[str] = None, + resource: Resource = None, ): - # if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name - # from the env variable else defaults to "unknown_service" - auto_resource = {} - # populate version if using auto-instrumentation - if auto_instrumentation_version: - auto_resource[ - ResourceAttributes.TELEMETRY_AUTO_VERSION - ] = auto_instrumentation_version - provider = LoggerProvider(resource=Resource.create(auto_resource)) + provider = LoggerProvider(resource=resource) set_logger_provider(provider) for _, exporter_class in exporters.items(): @@ -343,18 +318,28 @@ def _initialize_components(auto_instrumentation_version): sampler = _import_sampler(sampler_name) id_generator_name = _get_id_generator() id_generator = _import_id_generator(id_generator_name) + # if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name + # from the env variable else defaults to "unknown_service" + auto_resource = {} + # populate version if using auto-instrumentation + if auto_instrumentation_version: + auto_resource[ + ResourceAttributes.TELEMETRY_AUTO_VERSION + ] = auto_instrumentation_version + resource = Resource.create(auto_resource) + _init_tracing( exporters=trace_exporters, id_generator=id_generator, sampler=sampler, - auto_instrumentation_version=auto_instrumentation_version, + resource=resource, ) - _init_metrics(metric_exporters, auto_instrumentation_version) + _init_metrics(metric_exporters, resource) logging_enabled = os.getenv( _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "false" ) if logging_enabled.strip().lower() == "true": - _init_logging(log_exporters, auto_instrumentation_version) + _init_logging(log_exporters, resource) class _BaseConfigurator(ABC): diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index a27c7a49a1d..7b3164639be 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -221,9 +221,7 @@ def should_sample( class CustomRatioSampler(TraceIdRatioBased): def __init__(self, ratio): if not isinstance(ratio, float): - raise ValueError( - "CustomRatioSampler ratio argument is not a float." - ) + raise ValueError("CustomRatioSampler ratio argument is not a float.") self.ratio = ratio super().__init__(ratio) @@ -302,14 +300,18 @@ def tearDown(self): self.set_provider_patcher.stop() # pylint: disable=protected-access - @patch.dict( - environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"} - ) + @patch.dict(environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"}) def test_trace_init_default(self): + + auto_resource = Resource.create( + { + "telemetry.auto.version": "test-version", + } + ) _init_tracing( {"zipkin": Exporter}, id_generator=RandomIdGenerator(), - auto_instrumentation_version="test-version", + resource=auto_resource, ) self.assertEqual(self.set_provider_mock.call_count, 1) @@ -318,9 +320,7 @@ def test_trace_init_default(self): self.assertIsInstance(provider.id_generator, RandomIdGenerator) self.assertIsInstance(provider.processor, Processor) self.assertIsInstance(provider.processor.exporter, Exporter) - self.assertEqual( - provider.processor.exporter.service_name, "my-test-service" - ) + self.assertEqual(provider.processor.exporter.service_name, "my-test-service") self.assertEqual( provider.resource.attributes.get("telemetry.auto.version"), "test-version", @@ -331,9 +331,7 @@ def test_trace_init_default(self): {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-otlp-test-service"}, ) def test_trace_init_otlp(self): - _init_tracing( - {"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator() - ) + _init_tracing({"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator()) self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] @@ -352,9 +350,7 @@ def test_trace_init_otlp(self): @patch("opentelemetry.sdk._configuration.iter_entry_points") def test_trace_init_custom_id_generator(self, mock_iter_entry_points): mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint("custom_id_generator", CustomIdGenerator) - ] + return_value=[IterEntryPoint("custom_id_generator", CustomIdGenerator)] ) id_generator_name = _get_id_generator() id_generator = _import_id_generator(id_generator_name) @@ -362,9 +358,7 @@ def test_trace_init_custom_id_generator(self, mock_iter_entry_points): provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider.id_generator, CustomIdGenerator) - @patch.dict( - "os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"} - ) + @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"}) def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -441,9 +435,7 @@ def test_trace_init_custom_sampler_with_env_unused_arg( OTEL_TRACES_SAMPLER_ARG: "0.5", }, ) - def test_trace_init_custom_ratio_sampler_with_env( - self, mock_iter_entry_points - ): + def test_trace_init_custom_ratio_sampler_with_env(self, mock_iter_entry_points): mock_iter_entry_points.configure_mock( return_value=[ IterEntryPoint( @@ -577,7 +569,12 @@ def tearDown(self): ] def test_logging_init_empty(self): - _init_logging({}, "auto-version") + auto_resource = Resource.create( + { + "telemetry.auto.version": "auto-version", + } + ) + _init_logging({}, resource=auto_resource) self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider, DummyLoggerProvider) @@ -592,7 +589,8 @@ def test_logging_init_empty(self): {"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service"}, ) def test_logging_init_exporter(self): - _init_logging({"otlp": DummyOTLPLogExporter}) + resource = Resource.create({}) + _init_logging({"otlp": DummyOTLPLogExporter}, resource=resource) self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider, DummyLoggerProvider) @@ -602,9 +600,7 @@ def test_logging_init_exporter(self): "otlp-service", ) self.assertIsInstance(provider.processor, DummyLogRecordProcessor) - self.assertIsInstance( - provider.processor.exporter, DummyOTLPLogExporter - ) + self.assertIsInstance(provider.processor.exporter, DummyOTLPLogExporter) logging.getLogger(__name__).error("hello") self.assertTrue(provider.processor.exporter.export_called) @@ -658,15 +654,18 @@ def tearDown(self): self.provider_patch.stop() def test_metrics_init_empty(self): - _init_metrics({}, "auto-version") + auto_resource = Resource.create( + { + "telemetry.auto.version": "auto-version", + } + ) + _init_metrics({}, resource=auto_resource) self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider, DummyMeterProvider) self.assertIsInstance(provider._sdk_config.resource, Resource) self.assertEqual( - provider._sdk_config.resource.attributes.get( - "telemetry.auto.version" - ), + provider._sdk_config.resource.attributes.get("telemetry.auto.version"), "auto-version", ) @@ -675,7 +674,8 @@ def test_metrics_init_empty(self): {"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service"}, ) def test_metrics_init_exporter(self): - _init_metrics({"otlp": DummyOTLPMetricExporter}) + resource = Resource.create({}) + _init_metrics({"otlp": DummyOTLPMetricExporter}, resource=resource) self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider, DummyMeterProvider) @@ -699,15 +699,9 @@ class TestExporterNames(TestCase): }, ) def test_otlp_exporter(self): - self.assertEqual( - _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC] - ) - self.assertEqual( - _get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC] - ) - self.assertEqual( - _get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP] - ) + self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC]) + self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]) + self.assertEqual(_get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP]) @patch.dict( environ, @@ -719,12 +713,8 @@ def test_otlp_exporter(self): }, ) def test_otlp_custom_exporter(self): - self.assertEqual( - _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP] - ) - self.assertEqual( - _get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC] - ) + self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]) + self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]) @patch.dict( environ, @@ -738,9 +728,7 @@ def test_otlp_custom_exporter(self): def test_otlp_exporter_conflict(self): # Verify that OTEL_*_EXPORTER is used, and a warning is logged with self.assertLogs(level="WARNING") as logs_context: - self.assertEqual( - _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP] - ) + self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]) assert len(logs_context.output) == 1 with self.assertLogs(level="WARNING") as logs_context: @@ -751,9 +739,7 @@ def test_otlp_exporter_conflict(self): @patch.dict(environ, {"OTEL_TRACES_EXPORTER": "jaeger,zipkin"}) def test_multiple_exporters(self): - self.assertEqual( - sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"] - ) + self.assertEqual(sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"]) @patch.dict(environ, {"OTEL_TRACES_EXPORTER": "none"}) def test_none_exporters(self): From 340e7e90ae73c25800562f9c1e660826f13b2e07 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sat, 7 Jan 2023 04:11:21 +0530 Subject: [PATCH 2/7] Fix lint --- opentelemetry-sdk/tests/test_configurator.py | 64 +++++++++++++++----- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 7b3164639be..8a038e37365 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -221,7 +221,9 @@ def should_sample( class CustomRatioSampler(TraceIdRatioBased): def __init__(self, ratio): if not isinstance(ratio, float): - raise ValueError("CustomRatioSampler ratio argument is not a float.") + raise ValueError( + "CustomRatioSampler ratio argument is not a float." + ) self.ratio = ratio super().__init__(ratio) @@ -300,7 +302,9 @@ def tearDown(self): self.set_provider_patcher.stop() # pylint: disable=protected-access - @patch.dict(environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"}) + @patch.dict( + environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"} + ) def test_trace_init_default(self): auto_resource = Resource.create( @@ -320,7 +324,9 @@ def test_trace_init_default(self): self.assertIsInstance(provider.id_generator, RandomIdGenerator) self.assertIsInstance(provider.processor, Processor) self.assertIsInstance(provider.processor.exporter, Exporter) - self.assertEqual(provider.processor.exporter.service_name, "my-test-service") + self.assertEqual( + provider.processor.exporter.service_name, "my-test-service" + ) self.assertEqual( provider.resource.attributes.get("telemetry.auto.version"), "test-version", @@ -331,7 +337,9 @@ def test_trace_init_default(self): {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-otlp-test-service"}, ) def test_trace_init_otlp(self): - _init_tracing({"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator()) + _init_tracing( + {"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator() + ) self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] @@ -350,7 +358,9 @@ def test_trace_init_otlp(self): @patch("opentelemetry.sdk._configuration.iter_entry_points") def test_trace_init_custom_id_generator(self, mock_iter_entry_points): mock_iter_entry_points.configure_mock( - return_value=[IterEntryPoint("custom_id_generator", CustomIdGenerator)] + return_value=[ + IterEntryPoint("custom_id_generator", CustomIdGenerator) + ] ) id_generator_name = _get_id_generator() id_generator = _import_id_generator(id_generator_name) @@ -358,7 +368,9 @@ def test_trace_init_custom_id_generator(self, mock_iter_entry_points): provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider.id_generator, CustomIdGenerator) - @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"}) + @patch.dict( + "os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"} + ) def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -435,7 +447,9 @@ def test_trace_init_custom_sampler_with_env_unused_arg( OTEL_TRACES_SAMPLER_ARG: "0.5", }, ) - def test_trace_init_custom_ratio_sampler_with_env(self, mock_iter_entry_points): + def test_trace_init_custom_ratio_sampler_with_env( + self, mock_iter_entry_points + ): mock_iter_entry_points.configure_mock( return_value=[ IterEntryPoint( @@ -600,7 +614,9 @@ def test_logging_init_exporter(self): "otlp-service", ) self.assertIsInstance(provider.processor, DummyLogRecordProcessor) - self.assertIsInstance(provider.processor.exporter, DummyOTLPLogExporter) + self.assertIsInstance( + provider.processor.exporter, DummyOTLPLogExporter + ) logging.getLogger(__name__).error("hello") self.assertTrue(provider.processor.exporter.export_called) @@ -665,7 +681,9 @@ def test_metrics_init_empty(self): self.assertIsInstance(provider, DummyMeterProvider) self.assertIsInstance(provider._sdk_config.resource, Resource) self.assertEqual( - provider._sdk_config.resource.attributes.get("telemetry.auto.version"), + provider._sdk_config.resource.attributes.get( + "telemetry.auto.version" + ), "auto-version", ) @@ -699,9 +717,15 @@ class TestExporterNames(TestCase): }, ) def test_otlp_exporter(self): - self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC]) - self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]) - self.assertEqual(_get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP]) + self.assertEqual( + _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC] + ) + self.assertEqual( + _get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC] + ) + self.assertEqual( + _get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP] + ) @patch.dict( environ, @@ -713,8 +737,12 @@ def test_otlp_exporter(self): }, ) def test_otlp_custom_exporter(self): - self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]) - self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]) + self.assertEqual( + _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP] + ) + self.assertEqual( + _get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC] + ) @patch.dict( environ, @@ -728,7 +756,9 @@ def test_otlp_custom_exporter(self): def test_otlp_exporter_conflict(self): # Verify that OTEL_*_EXPORTER is used, and a warning is logged with self.assertLogs(level="WARNING") as logs_context: - self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]) + self.assertEqual( + _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP] + ) assert len(logs_context.output) == 1 with self.assertLogs(level="WARNING") as logs_context: @@ -739,7 +769,9 @@ def test_otlp_exporter_conflict(self): @patch.dict(environ, {"OTEL_TRACES_EXPORTER": "jaeger,zipkin"}) def test_multiple_exporters(self): - self.assertEqual(sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"]) + self.assertEqual( + sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"] + ) @patch.dict(environ, {"OTEL_TRACES_EXPORTER": "none"}) def test_none_exporters(self): From ff8c5901878060879b7970a003ecccf9744db262 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sat, 7 Jan 2023 04:14:20 +0530 Subject: [PATCH 3/7] Fix lint --- .../src/opentelemetry/sdk/_configuration/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 873b11fb0d0..67cfd6bc682 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -205,9 +205,7 @@ def _init_metrics( PeriodicExportingMetricReader(exporter_class(**exporter_args)) ) - provider = MeterProvider( - resource=resource, metric_readers=metric_readers - ) + provider = MeterProvider(resource=resource, metric_readers=metric_readers) set_meter_provider(provider) From a9336a071facdcb43ff9e247be72b92378a30f6f Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sat, 7 Jan 2023 04:19:45 +0530 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0f43a10da0..a73e15017a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Create a single resource instance + ([#3118](https://github.com/open-telemetry/opentelemetry-python/pull/3118)) + ## Version 1.15.0/0.36b0 (2022-12-09) - Regenerate opentelemetry-proto to be compatible with protobuf 3 and 4 From 548db263a20b6014f0c347b8d8e60f8ab131ba0d Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Fri, 3 Feb 2023 00:25:22 +0530 Subject: [PATCH 5/7] Add another test --- opentelemetry-sdk/tests/test_configurator.py | 89 +++++++++----------- 1 file changed, 41 insertions(+), 48 deletions(-) diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 8a038e37365..044f73e0f1a 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -221,9 +221,7 @@ def should_sample( class CustomRatioSampler(TraceIdRatioBased): def __init__(self, ratio): if not isinstance(ratio, float): - raise ValueError( - "CustomRatioSampler ratio argument is not a float." - ) + raise ValueError("CustomRatioSampler ratio argument is not a float.") self.ratio = ratio super().__init__(ratio) @@ -302,9 +300,7 @@ def tearDown(self): self.set_provider_patcher.stop() # pylint: disable=protected-access - @patch.dict( - environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"} - ) + @patch.dict(environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"}) def test_trace_init_default(self): auto_resource = Resource.create( @@ -324,9 +320,7 @@ def test_trace_init_default(self): self.assertIsInstance(provider.id_generator, RandomIdGenerator) self.assertIsInstance(provider.processor, Processor) self.assertIsInstance(provider.processor.exporter, Exporter) - self.assertEqual( - provider.processor.exporter.service_name, "my-test-service" - ) + self.assertEqual(provider.processor.exporter.service_name, "my-test-service") self.assertEqual( provider.resource.attributes.get("telemetry.auto.version"), "test-version", @@ -337,9 +331,7 @@ def test_trace_init_default(self): {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-otlp-test-service"}, ) def test_trace_init_otlp(self): - _init_tracing( - {"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator() - ) + _init_tracing({"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator()) self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] @@ -358,9 +350,7 @@ def test_trace_init_otlp(self): @patch("opentelemetry.sdk._configuration.iter_entry_points") def test_trace_init_custom_id_generator(self, mock_iter_entry_points): mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint("custom_id_generator", CustomIdGenerator) - ] + return_value=[IterEntryPoint("custom_id_generator", CustomIdGenerator)] ) id_generator_name = _get_id_generator() id_generator = _import_id_generator(id_generator_name) @@ -368,9 +358,7 @@ def test_trace_init_custom_id_generator(self, mock_iter_entry_points): provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider.id_generator, CustomIdGenerator) - @patch.dict( - "os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"} - ) + @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"}) def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -447,9 +435,7 @@ def test_trace_init_custom_sampler_with_env_unused_arg( OTEL_TRACES_SAMPLER_ARG: "0.5", }, ) - def test_trace_init_custom_ratio_sampler_with_env( - self, mock_iter_entry_points - ): + def test_trace_init_custom_ratio_sampler_with_env(self, mock_iter_entry_points): mock_iter_entry_points.configure_mock( return_value=[ IterEntryPoint( @@ -614,9 +600,7 @@ def test_logging_init_exporter(self): "otlp-service", ) self.assertIsInstance(provider.processor, DummyLogRecordProcessor) - self.assertIsInstance( - provider.processor.exporter, DummyOTLPLogExporter - ) + self.assertIsInstance(provider.processor.exporter, DummyOTLPLogExporter) logging.getLogger(__name__).error("hello") self.assertTrue(provider.processor.exporter.export_called) @@ -645,6 +629,31 @@ def test_logging_init_enable_env(self, logging_mock, tracing_mock): self.assertEqual(logging_mock.call_count, 1) self.assertEqual(tracing_mock.call_count, 1) + @patch.dict( + environ, + { + "OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service", + "OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "True", + }, + ) + @patch("opentelemetry.sdk._configuration._init_tracing") + @patch("opentelemetry.sdk._configuration._init_logging") + @patch("opentelemetry.sdk._configuration._init_metrics") + def test_initialize_components_resource( + self, metrics_mock, logging_mock, tracing_mock + ): + _initialize_components("auto-version") + self.assertEqual(logging_mock.call_count, 1) + self.assertEqual(tracing_mock.call_count, 1) + self.assertEqual(metrics_mock.call_count, 1) + + logging_resource = logging_mock.call_args.args[1] + tracing_resource = tracing_mock.call_args.kwargs["resource"] + metrics_resource = metrics_mock.call_args.args[1] + self.assertEqual(logging_resource, tracing_resource) + self.assertEqual(logging_resource, metrics_resource) + self.assertEqual(tracing_resource, metrics_resource) + class TestMetricsInit(TestCase): def setUp(self): @@ -681,9 +690,7 @@ def test_metrics_init_empty(self): self.assertIsInstance(provider, DummyMeterProvider) self.assertIsInstance(provider._sdk_config.resource, Resource) self.assertEqual( - provider._sdk_config.resource.attributes.get( - "telemetry.auto.version" - ), + provider._sdk_config.resource.attributes.get("telemetry.auto.version"), "auto-version", ) @@ -717,15 +724,9 @@ class TestExporterNames(TestCase): }, ) def test_otlp_exporter(self): - self.assertEqual( - _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC] - ) - self.assertEqual( - _get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC] - ) - self.assertEqual( - _get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP] - ) + self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC]) + self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]) + self.assertEqual(_get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP]) @patch.dict( environ, @@ -737,12 +738,8 @@ def test_otlp_exporter(self): }, ) def test_otlp_custom_exporter(self): - self.assertEqual( - _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP] - ) - self.assertEqual( - _get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC] - ) + self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]) + self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]) @patch.dict( environ, @@ -756,9 +753,7 @@ def test_otlp_custom_exporter(self): def test_otlp_exporter_conflict(self): # Verify that OTEL_*_EXPORTER is used, and a warning is logged with self.assertLogs(level="WARNING") as logs_context: - self.assertEqual( - _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP] - ) + self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]) assert len(logs_context.output) == 1 with self.assertLogs(level="WARNING") as logs_context: @@ -769,9 +764,7 @@ def test_otlp_exporter_conflict(self): @patch.dict(environ, {"OTEL_TRACES_EXPORTER": "jaeger,zipkin"}) def test_multiple_exporters(self): - self.assertEqual( - sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"] - ) + self.assertEqual(sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"]) @patch.dict(environ, {"OTEL_TRACES_EXPORTER": "none"}) def test_none_exporters(self): From 33c4bbac610696e752db8d3d8d843eedf41de05c Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Fri, 3 Feb 2023 00:29:28 +0530 Subject: [PATCH 6/7] Black format --- opentelemetry-sdk/tests/test_configurator.py | 52 +++++++++++++++----- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 500aeeb0e0d..134f79427f8 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -224,7 +224,9 @@ def should_sample( class CustomRatioSampler(TraceIdRatioBased): def __init__(self, ratio): if not isinstance(ratio, float): - raise ValueError("CustomRatioSampler ratio argument is not a float.") + raise ValueError( + "CustomRatioSampler ratio argument is not a float." + ) self.ratio = ratio super().__init__(ratio) @@ -303,7 +305,9 @@ def tearDown(self): self.set_provider_patcher.stop() # pylint: disable=protected-access - @patch.dict(environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"}) + @patch.dict( + environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"} + ) def test_trace_init_default(self): auto_resource = Resource.create( @@ -323,7 +327,9 @@ def test_trace_init_default(self): self.assertIsInstance(provider.id_generator, RandomIdGenerator) self.assertIsInstance(provider.processor, Processor) self.assertIsInstance(provider.processor.exporter, Exporter) - self.assertEqual(provider.processor.exporter.service_name, "my-test-service") + self.assertEqual( + provider.processor.exporter.service_name, "my-test-service" + ) self.assertEqual( provider.resource.attributes.get("telemetry.auto.version"), "test-version", @@ -334,7 +340,9 @@ def test_trace_init_default(self): {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-otlp-test-service"}, ) def test_trace_init_otlp(self): - _init_tracing({"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator()) + _init_tracing( + {"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator() + ) self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] @@ -364,7 +372,9 @@ def test_trace_init_custom_id_generator(self, mock_entry_points): provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider.id_generator, CustomIdGenerator) - @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"}) + @patch.dict( + "os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"} + ) def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -697,7 +707,9 @@ def test_metrics_init_empty(self): self.assertIsInstance(provider, DummyMeterProvider) self.assertIsInstance(provider._sdk_config.resource, Resource) self.assertEqual( - provider._sdk_config.resource.attributes.get("telemetry.auto.version"), + provider._sdk_config.resource.attributes.get( + "telemetry.auto.version" + ), "auto-version", ) @@ -731,9 +743,15 @@ class TestExporterNames(TestCase): }, ) def test_otlp_exporter(self): - self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC]) - self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]) - self.assertEqual(_get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP]) + self.assertEqual( + _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC] + ) + self.assertEqual( + _get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC] + ) + self.assertEqual( + _get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP] + ) @patch.dict( environ, @@ -745,8 +763,12 @@ def test_otlp_exporter(self): }, ) def test_otlp_custom_exporter(self): - self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]) - self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]) + self.assertEqual( + _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP] + ) + self.assertEqual( + _get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC] + ) @patch.dict( environ, @@ -760,7 +782,9 @@ def test_otlp_custom_exporter(self): def test_otlp_exporter_conflict(self): # Verify that OTEL_*_EXPORTER is used, and a warning is logged with self.assertLogs(level="WARNING") as logs_context: - self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]) + self.assertEqual( + _get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP] + ) assert len(logs_context.output) == 1 with self.assertLogs(level="WARNING") as logs_context: @@ -771,7 +795,9 @@ def test_otlp_exporter_conflict(self): @patch.dict(environ, {"OTEL_TRACES_EXPORTER": "jaeger,zipkin"}) def test_multiple_exporters(self): - self.assertEqual(sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"]) + self.assertEqual( + sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"] + ) @patch.dict(environ, {"OTEL_TRACES_EXPORTER": "none"}) def test_none_exporters(self): From 634c828569474a28d8625bb08d7b3a2d6f3473e7 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Fri, 3 Feb 2023 08:26:08 +0530 Subject: [PATCH 7/7] Fix tests for < py3.8 --- opentelemetry-sdk/tests/test_configurator.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 134f79427f8..aa1c58cb51b 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -664,9 +664,12 @@ def test_initialize_components_resource( self.assertEqual(tracing_mock.call_count, 1) self.assertEqual(metrics_mock.call_count, 1) - logging_resource = logging_mock.call_args.args[1] - tracing_resource = tracing_mock.call_args.kwargs["resource"] - metrics_resource = metrics_mock.call_args.args[1] + _, args, _ = logging_mock.mock_calls[0] + logging_resource = args[1] + _, _, kwargs = tracing_mock.mock_calls[0] + tracing_resource = kwargs["resource"] + _, args, _ = metrics_mock.mock_calls[0] + metrics_resource = args[1] self.assertEqual(logging_resource, tracing_resource) self.assertEqual(logging_resource, metrics_resource) self.assertEqual(tracing_resource, metrics_resource)