From dd0ff73aafe0baf0c46d625c21dbbd65e0ab778b Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 27 Oct 2021 16:11:04 -0700 Subject: [PATCH 1/3] comment --- docs/examples/auto-instrumentation/README.rst | 2 +- .../server_uninstrumented.py | 14 -------------- .../sdk/_configuration/__init__.py | 19 +++++++++++++------ opentelemetry-sdk/tests/test_configurator.py | 6 +++++- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/docs/examples/auto-instrumentation/README.rst b/docs/examples/auto-instrumentation/README.rst index aeb1e0be8e9..7ebd8a76d35 100644 --- a/docs/examples/auto-instrumentation/README.rst +++ b/docs/examples/auto-instrumentation/README.rst @@ -153,7 +153,7 @@ and run the following command instead: .. code:: sh - $ opentelemetry-instrument --trace-exporter console_span python server_uninstrumented.py + $ opentelemetry-instrument --traces_exporter console python server_uninstrumented.py In the console where you previously executed ``client.py``, run the following command again: diff --git a/docs/examples/auto-instrumentation/server_uninstrumented.py b/docs/examples/auto-instrumentation/server_uninstrumented.py index e6fa75f1b56..4ef06af1fab 100644 --- a/docs/examples/auto-instrumentation/server_uninstrumented.py +++ b/docs/examples/auto-instrumentation/server_uninstrumented.py @@ -14,22 +14,8 @@ from flask import Flask, request -from opentelemetry import trace -from opentelemetry.sdk.trace import TracerProvider -from opentelemetry.sdk.trace.export import ( - BatchSpanProcessor, - ConsoleSpanExporter, -) - app = Flask(__name__) -trace.set_tracer_provider(TracerProvider()) - -trace.get_tracer_provider().add_span_processor( - BatchSpanProcessor(ConsoleSpanExporter()) -) - - @app.route("/server_request") def server_request(): print(request.args.get("param")) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index b49e4d8e598..a30df82cec8 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -19,7 +19,8 @@ from abc import ABC, abstractmethod from os import environ -from typing import Sequence, Tuple +from typing import Dict, Optional, Sequence, Tuple, Type +from opentelemetry.sdk.resources import Resource from pkg_resources import iter_entry_points @@ -31,6 +32,7 @@ from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter from opentelemetry.sdk.trace.id_generator import IdGenerator +from opentelemetry.semconv.resource import ResourceAttributes _EXPORTER_OTLP = "otlp" _EXPORTER_OTLP_SPAN = "otlp_proto_grpc" @@ -64,12 +66,17 @@ def _get_exporter_names() -> Sequence[str]: def _init_tracing( - exporters: Sequence[SpanExporter], id_generator: IdGenerator + exporters: Dict[str, Type[SpanExporter]], id_generator: IdGenerator, auto_instrumentation_version: Optional[str] = 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(), + resource=Resource.create(auto_resource), ) trace.set_tracer_provider(provider) @@ -102,7 +109,7 @@ def _import_tracer_provider_config_components( def _import_exporters( exporter_names: Sequence[str], -) -> Sequence[SpanExporter]: +) -> Dict[str, Type[SpanExporter]]: trace_exporters = {} for ( @@ -132,12 +139,12 @@ def _import_id_generator(id_generator_name: str) -> IdGenerator: raise RuntimeError(f"{id_generator_name} is not an IdGenerator") -def _initialize_components(): +def _initialize_components(auto_instrumentation_version): exporter_names = _get_exporter_names() trace_exporters = _import_exporters(exporter_names) id_generator_name = _get_id_generator() id_generator = _import_id_generator(id_generator_name) - _init_tracing(trace_exporters, id_generator) + _init_tracing(trace_exporters, id_generator, auto_instrumentation_version) class _BaseConfigurator(ABC): @@ -180,4 +187,4 @@ class _OTelSDKConfigurator(_BaseConfigurator): """ def _configure(self, **kwargs): - _initialize_components() + _initialize_components(kwargs.get("auto_instrumentation_version")) diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index e7619d64dcc..9243b80c675 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -111,7 +111,7 @@ def tearDown(self): environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"} ) def test_trace_init_default(self): - _init_tracing({"zipkin": Exporter}, RandomIdGenerator) + _init_tracing({"zipkin": Exporter}, RandomIdGenerator, "test-version") self.assertEqual(self.set_provider_mock.call_count, 1) provider = self.set_provider_mock.call_args[0][0] @@ -122,6 +122,10 @@ def test_trace_init_default(self): self.assertEqual( provider.processor.exporter.service_name, "my-test-service" ) + self.assertEqual( + provider.resource.attributes.get("telemetry.auto.version"), + "test-version", + ) @patch.dict( environ, From 05269229068478ea6cd9ba9304e250dd6e1b8b30 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 27 Oct 2021 16:20:11 -0700 Subject: [PATCH 2/3] lint --- CHANGELOG.md | 4 +++- .../auto-instrumentation/server_uninstrumented.py | 1 + .../src/opentelemetry/sdk/_configuration/__init__.py | 8 ++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85ce4ddeeef..8093db96f9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.6.1-0.25b1...HEAD) +## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.6.1-0.25b2...HEAD) - Add support for Python 3.10 ([#2207](https://github.com/open-telemetry/opentelemetry-python/pull/2207)) - remove `X-B3-ParentSpanId` for B3 propagator as per OpenTelemetry specification ([#2237](https://github.com/open-telemetry/opentelemetry-python/pull/2237)) +- Populate `auto.version` in Resource if using auto-instrumentation + ([#2243](https://github.com/open-telemetry/opentelemetry-python/pull/2243)) ## [1.6.2-0.25b2](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.6.2-0.25b2) - 2021-10-19 diff --git a/docs/examples/auto-instrumentation/server_uninstrumented.py b/docs/examples/auto-instrumentation/server_uninstrumented.py index 4ef06af1fab..9c247a049a8 100644 --- a/docs/examples/auto-instrumentation/server_uninstrumented.py +++ b/docs/examples/auto-instrumentation/server_uninstrumented.py @@ -16,6 +16,7 @@ app = Flask(__name__) + @app.route("/server_request") def server_request(): print(request.args.get("param")) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index a30df82cec8..b7ae0c16fde 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -66,14 +66,18 @@ def _get_exporter_names() -> Sequence[str]: def _init_tracing( - exporters: Dict[str, Type[SpanExporter]], id_generator: IdGenerator, auto_instrumentation_version: Optional[str] = None + exporters: Dict[str, Type[SpanExporter]], + id_generator: IdGenerator, + auto_instrumentation_version: Optional[str] = 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 + auto_resource[ + ResourceAttributes.TELEMETRY_AUTO_VERSION + ] = auto_instrumentation_version provider = TracerProvider( id_generator=id_generator(), resource=Resource.create(auto_resource), From b087edb7cac541365d0c8f051709916297c4affe Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 27 Oct 2021 16:24:50 -0700 Subject: [PATCH 3/3] lint --- .../src/opentelemetry/sdk/_configuration/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index b7ae0c16fde..cb4aed53b53 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -20,7 +20,6 @@ from abc import ABC, abstractmethod from os import environ from typing import Dict, Optional, Sequence, Tuple, Type -from opentelemetry.sdk.resources import Resource from pkg_resources import iter_entry_points @@ -29,6 +28,7 @@ OTEL_PYTHON_ID_GENERATOR, OTEL_TRACES_EXPORTER, ) +from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter from opentelemetry.sdk.trace.id_generator import IdGenerator