Skip to content

Commit a1903c9

Browse files
authored
Populate auto.version in Resource if using autoinstrumentation (#2243)
1 parent 29e4bab commit a1903c9

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.6.1-0.25b1...HEAD)
7+
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.6.1-0.25b2...HEAD)
88

99
- Add support for Python 3.10
1010
([#2207](https://github.com/open-telemetry/opentelemetry-python/pull/2207))
1111
- remove `X-B3-ParentSpanId` for B3 propagator as per OpenTelemetry specification
1212
([#2237](https://github.com/open-telemetry/opentelemetry-python/pull/2237))
13+
- Populate `auto.version` in Resource if using auto-instrumentation
14+
([#2243](https://github.com/open-telemetry/opentelemetry-python/pull/2243))
1315
- Return proxy instruments from ProxyMeter
1416
[[#2169](https://github.com/open-telemetry/opentelemetry-python/pull/2169)]
1517
- Make Measurement a concrete class

docs/examples/auto-instrumentation/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ and run the following command instead:
153153

154154
.. code:: sh
155155
156-
$ opentelemetry-instrument --trace-exporter console_span python server_uninstrumented.py
156+
$ opentelemetry-instrument --traces_exporter console python server_uninstrumented.py
157157
158158
In the console where you previously executed ``client.py``, run the following
159159
command again:

docs/examples/auto-instrumentation/server_uninstrumented.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,8 @@
1414

1515
from flask import Flask, request
1616

17-
from opentelemetry import trace
18-
from opentelemetry.sdk.trace import TracerProvider
19-
from opentelemetry.sdk.trace.export import (
20-
BatchSpanProcessor,
21-
ConsoleSpanExporter,
22-
)
23-
2417
app = Flask(__name__)
2518

26-
trace.set_tracer_provider(TracerProvider())
27-
28-
trace.get_tracer_provider().add_span_processor(
29-
BatchSpanProcessor(ConsoleSpanExporter())
30-
)
31-
3219

3320
@app.route("/server_request")
3421
def server_request():

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from abc import ABC, abstractmethod
2121
from os import environ
22-
from typing import Sequence, Tuple
22+
from typing import Dict, Optional, Sequence, Tuple, Type
2323

2424
from pkg_resources import iter_entry_points
2525

@@ -28,9 +28,11 @@
2828
OTEL_PYTHON_ID_GENERATOR,
2929
OTEL_TRACES_EXPORTER,
3030
)
31+
from opentelemetry.sdk.resources import Resource
3132
from opentelemetry.sdk.trace import TracerProvider
3233
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter
3334
from opentelemetry.sdk.trace.id_generator import IdGenerator
35+
from opentelemetry.semconv.resource import ResourceAttributes
3436

3537
_EXPORTER_OTLP = "otlp"
3638
_EXPORTER_OTLP_SPAN = "otlp_proto_grpc"
@@ -64,12 +66,21 @@ def _get_exporter_names() -> Sequence[str]:
6466

6567

6668
def _init_tracing(
67-
exporters: Sequence[SpanExporter], id_generator: IdGenerator
69+
exporters: Dict[str, Type[SpanExporter]],
70+
id_generator: IdGenerator,
71+
auto_instrumentation_version: Optional[str] = None,
6872
):
6973
# if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
7074
# from the env variable else defaults to "unknown_service"
75+
auto_resource = {}
76+
# populate version if using auto-instrumentation
77+
if auto_instrumentation_version:
78+
auto_resource[
79+
ResourceAttributes.TELEMETRY_AUTO_VERSION
80+
] = auto_instrumentation_version
7181
provider = TracerProvider(
7282
id_generator=id_generator(),
83+
resource=Resource.create(auto_resource),
7384
)
7485
trace.set_tracer_provider(provider)
7586

@@ -102,7 +113,7 @@ def _import_tracer_provider_config_components(
102113

103114
def _import_exporters(
104115
exporter_names: Sequence[str],
105-
) -> Sequence[SpanExporter]:
116+
) -> Dict[str, Type[SpanExporter]]:
106117
trace_exporters = {}
107118

108119
for (
@@ -132,12 +143,12 @@ def _import_id_generator(id_generator_name: str) -> IdGenerator:
132143
raise RuntimeError(f"{id_generator_name} is not an IdGenerator")
133144

134145

135-
def _initialize_components():
146+
def _initialize_components(auto_instrumentation_version):
136147
exporter_names = _get_exporter_names()
137148
trace_exporters = _import_exporters(exporter_names)
138149
id_generator_name = _get_id_generator()
139150
id_generator = _import_id_generator(id_generator_name)
140-
_init_tracing(trace_exporters, id_generator)
151+
_init_tracing(trace_exporters, id_generator, auto_instrumentation_version)
141152

142153

143154
class _BaseConfigurator(ABC):
@@ -180,4 +191,4 @@ class _OTelSDKConfigurator(_BaseConfigurator):
180191
"""
181192

182193
def _configure(self, **kwargs):
183-
_initialize_components()
194+
_initialize_components(kwargs.get("auto_instrumentation_version"))

opentelemetry-sdk/tests/test_configurator.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def tearDown(self):
111111
environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"}
112112
)
113113
def test_trace_init_default(self):
114-
_init_tracing({"zipkin": Exporter}, RandomIdGenerator)
114+
_init_tracing({"zipkin": Exporter}, RandomIdGenerator, "test-version")
115115

116116
self.assertEqual(self.set_provider_mock.call_count, 1)
117117
provider = self.set_provider_mock.call_args[0][0]
@@ -122,6 +122,10 @@ def test_trace_init_default(self):
122122
self.assertEqual(
123123
provider.processor.exporter.service_name, "my-test-service"
124124
)
125+
self.assertEqual(
126+
provider.resource.attributes.get("telemetry.auto.version"),
127+
"test-version",
128+
)
125129

126130
@patch.dict(
127131
environ,

0 commit comments

Comments
 (0)