diff --git a/CHANGELOG.md b/CHANGELOG.md index 55dd541b19d..bdc06dd7563 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)) - Return proxy instruments from ProxyMeter [[#2169](https://github.com/open-telemetry/opentelemetry-python/pull/2169)] - Make Measurement a concrete class 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..9c247a049a8 100644 --- a/docs/examples/auto-instrumentation/server_uninstrumented.py +++ b/docs/examples/auto-instrumentation/server_uninstrumented.py @@ -14,21 +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(): diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index b49e4d8e598..cb4aed53b53 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -19,7 +19,7 @@ from abc import ABC, abstractmethod from os import environ -from typing import Sequence, Tuple +from typing import Dict, Optional, Sequence, Tuple, Type from pkg_resources import iter_entry_points @@ -28,9 +28,11 @@ 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 +from opentelemetry.semconv.resource import ResourceAttributes _EXPORTER_OTLP = "otlp" _EXPORTER_OTLP_SPAN = "otlp_proto_grpc" @@ -64,12 +66,21 @@ 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 +113,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 +143,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 +191,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,