Skip to content

Commit

Permalink
Incorporate resource detectors to auto instrumentation
Browse files Browse the repository at this point in the history
This is an experimental feature.

Fixes #3172
  • Loading branch information
ocelotl committed Mar 13, 2023
1 parent 350e082 commit 27904a1
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 202 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ 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

- Incorporate resource detectors to auto instrumentation (experimental feature)
([#3172](https://github.com/open-telemetry/opentelemetry-python/pull/3172))
- PeriodicExportingMetricReader will continue if collection times out
([#3100](https://github.com/open-telemetry/opentelemetry-python/pull/3100))
- Fix formatting of ConsoleMetricExporter.
([#3197](https://github.com/open-telemetry/opentelemetry-python/pull/3197))

- Add exponential histogram
([#2964](https://github.com/open-telemetry/opentelemetry-python/pull/2964))

Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-sdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ sdk_tracer_provider = "opentelemetry.sdk.trace:TracerProvider"
[project.entry-points.opentelemetry_traces_exporter]
console = "opentelemetry.sdk.trace.export:ConsoleSpanExporter"

[project.entry-points.opentelemetry_resource_detector]
otel = "opentelemetry.sdk.resources:OTELResourceDetector"
process = "opentelemetry.sdk.resources:ProcessResourceDetector"

[project.urls]
Homepage = "https://github.com/open-telemetry/opentelemetry-python/tree/main/opentelemetry-sdk"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,12 @@
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.
"""

_OTEL_RESOURCE_DETECTORS = "_OTEL_RESOURCE_DETECTORS"
"""
.. envvar:: _OTEL_RESOURCE_DETECTORS
The :envvar:`_OTEL_RESOURCE_DETECTORS` is a comma-separated string of names of
resource detectors. These names must be the same as the names of entry points
for the `opentelemetry-resource-detectors` entry point.
"""
29 changes: 22 additions & 7 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,20 @@
import abc
import concurrent.futures
import logging
import os
import sys
import typing
from json import dumps
from os import environ
from urllib import parse

from opentelemetry.attributes import BoundedAttributes
from opentelemetry.sdk.environment_variables import (
_OTEL_RESOURCE_DETECTORS,
OTEL_RESOURCE_ATTRIBUTES,
OTEL_SERVICE_NAME,
)
from opentelemetry.semconv.resource import ResourceAttributes
from opentelemetry.util._importlib_metadata import version
from opentelemetry.util._importlib_metadata import entry_points, version
from opentelemetry.util.types import AttributeValue

LabelValue = AttributeValue
Expand Down Expand Up @@ -165,9 +166,23 @@ def create(
"""
if not attributes:
attributes = {}
resource = _DEFAULT_RESOURCE.merge(
OTELResourceDetector().detect()
).merge(Resource(attributes, schema_url))

resource = _DEFAULT_RESOURCE

for resource_detector in environ.get(
_OTEL_RESOURCE_DETECTORS, "otel"
).split(","):

resource = resource.merge(
entry_points(
group="opentelemetry_resource_detector",
name=resource_detector,
)[0]
.load()()
.detect()
)

resource = resource.merge(Resource(attributes, schema_url))
if not resource.attributes.get(SERVICE_NAME, None):
default_service_name = "unknown_service"
process_executable_name = resource.attributes.get(
Expand Down Expand Up @@ -273,7 +288,7 @@ def detect(self) -> "Resource":
class OTELResourceDetector(ResourceDetector):
# pylint: disable=no-self-use
def detect(self) -> "Resource":
env_resources_items = os.environ.get(OTEL_RESOURCE_ATTRIBUTES)
env_resources_items = environ.get(OTEL_RESOURCE_ATTRIBUTES)
env_resource_map = {}

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

service_name = os.environ.get(OTEL_SERVICE_NAME)
service_name = environ.get(OTEL_SERVICE_NAME)
if service_name:
env_resource_map[SERVICE_NAME] = service_name
return Resource(env_resource_map)
Expand Down
Loading

0 comments on commit 27904a1

Please sign in to comment.