Skip to content

Commit

Permalink
Populate auto.version in Resource if using autoinstrumentation (#2243)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Nov 6, 2021
1 parent 29e4bab commit a1903c9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/auto-instrumentation/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 0 additions & 13 deletions docs/examples/auto-instrumentation/server_uninstrumented.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
23 changes: 17 additions & 6 deletions opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -180,4 +191,4 @@ class _OTelSDKConfigurator(_BaseConfigurator):
"""

def _configure(self, **kwargs):
_initialize_components()
_initialize_components(kwargs.get("auto_instrumentation_version"))
6 changes: 5 additions & 1 deletion opentelemetry-sdk/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
Expand Down

0 comments on commit a1903c9

Please sign in to comment.