Skip to content

Commit d44d462

Browse files
committed
Incorporate resource detectors to auto instrumentation
This is an experimental feature. Fixes #3172
1 parent a9a96aa commit d44d462

File tree

5 files changed

+274
-201
lines changed

5 files changed

+274
-201
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## Unreleased
9+
10+
- Incorporate resource detectors to auto instrumentation (experimental feature)
11+
([#3172](https://github.com/open-telemetry/opentelemetry-python/pull/3172))
912
- PeriodicExportingMetricReader will continue if collection times out
1013
([#3100](https://github.com/open-telemetry/opentelemetry-python/pull/3100))
1114
- Fix formatting of ConsoleMetricExporter.
1215
([#3197](https://github.com/open-telemetry/opentelemetry-python/pull/3197))
1316

1417
## Version 1.16.0/0.37b0 (2023-02-17)
18+
1519
- Change ``__all__`` to be statically defined.
1620
([#3143](https://github.com/open-telemetry/opentelemetry-python/pull/3143))
1721
- Remove the ability to set a global metric prefix for Prometheus exporter

opentelemetry-sdk/pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ sdk_tracer_provider = "opentelemetry.sdk.trace:TracerProvider"
5959
[project.entry-points.opentelemetry_traces_exporter]
6060
console = "opentelemetry.sdk.trace.export:ConsoleSpanExporter"
6161

62+
[project.entry-points.opentelemetry_resource_detector]
63+
otel = "opentelemetry.sdk.resources:OTELResourceDetector"
64+
process = "opentelemetry.sdk.resources:ProcessResourceDetector"
65+
6266
[project.urls]
6367
Homepage = "https://github.com/open-telemetry/opentelemetry-python/tree/main/opentelemetry-sdk"
6468

opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,12 @@
633633
634634
The :envvar:`OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE` is the client certificate/chain trust for clients private key to use in mTLS communication in PEM format.
635635
"""
636+
637+
_OTEL_RESOURCE_DETECTORS = "_OTEL_RESOURCE_DETECTORS"
638+
"""
639+
.. envvar:: _OTEL_RESOURCE_DETECTORS
640+
641+
The :envvar:`_OTEL_RESOURCE_DETECTORS` is a comma-separated string of names of
642+
resource detectors. These names must be the same as the names of entry points
643+
for the `opentelemetry-resource-detectors` entry point.
644+
"""

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,20 @@
5858
import abc
5959
import concurrent.futures
6060
import logging
61-
import os
6261
import sys
6362
import typing
6463
from json import dumps
64+
from os import environ
6565
from urllib import parse
6666

6767
from opentelemetry.attributes import BoundedAttributes
6868
from opentelemetry.sdk.environment_variables import (
69+
_OTEL_RESOURCE_DETECTORS,
6970
OTEL_RESOURCE_ATTRIBUTES,
7071
OTEL_SERVICE_NAME,
7172
)
7273
from opentelemetry.semconv.resource import ResourceAttributes
73-
from opentelemetry.util._importlib_metadata import version
74+
from opentelemetry.util._importlib_metadata import entry_points, version
7475
from opentelemetry.util.types import AttributeValue
7576

7677
LabelValue = AttributeValue
@@ -165,9 +166,23 @@ def create(
165166
"""
166167
if not attributes:
167168
attributes = {}
168-
resource = _DEFAULT_RESOURCE.merge(
169-
OTELResourceDetector().detect()
170-
).merge(Resource(attributes, schema_url))
169+
170+
resource = _DEFAULT_RESOURCE
171+
172+
for resource_detector in environ.get(
173+
_OTEL_RESOURCE_DETECTORS, "otel"
174+
).split(","):
175+
176+
resource = resource.merge(
177+
entry_points(
178+
group="opentelemetry_resource_detector",
179+
name=resource_detector,
180+
)[0]
181+
.load()()
182+
.detect()
183+
)
184+
185+
resource = resource.merge(Resource(attributes, schema_url))
171186
if not resource.attributes.get(SERVICE_NAME, None):
172187
default_service_name = "unknown_service"
173188
process_executable_name = resource.attributes.get(
@@ -273,7 +288,7 @@ def detect(self) -> "Resource":
273288
class OTELResourceDetector(ResourceDetector):
274289
# pylint: disable=no-self-use
275290
def detect(self) -> "Resource":
276-
env_resources_items = os.environ.get(OTEL_RESOURCE_ATTRIBUTES)
291+
env_resources_items = environ.get(OTEL_RESOURCE_ATTRIBUTES)
277292
env_resource_map = {}
278293

279294
if env_resources_items:
@@ -290,7 +305,7 @@ def detect(self) -> "Resource":
290305
value_url_decoded = parse.unquote(value.strip())
291306
env_resource_map[key.strip()] = value_url_decoded
292307

293-
service_name = os.environ.get(OTEL_SERVICE_NAME)
308+
service_name = environ.get(OTEL_SERVICE_NAME)
294309
if service_name:
295310
env_resource_map[SERVICE_NAME] = service_name
296311
return Resource(env_resource_map)

0 commit comments

Comments
 (0)