Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate auto.version in Resource if using autoinstrumentation #2243

Merged
merged 6 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
codeboten marked this conversation as resolved.
Show resolved Hide resolved

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
codeboten marked this conversation as resolved.
Show resolved Hide resolved
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
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