Skip to content

Commit

Permalink
Use environment variables for tracer and meter providers (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl authored Feb 4, 2021
1 parent 30bb6a7 commit b743ab1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v0.17b0...HEAD)

### Changed
- Tracer and Meter provider environment variables are now consistent with the rest
([#1571](https://github.com/open-telemetry/opentelemetry-python/pull/1571)])

### Added
- Added `end_on_exit` argument to `start_as_current_span`
([#1519](https://github.com/open-telemetry/opentelemetry-python/pull/1519)])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
OTEL_PYTHON_SERVICE_NAME = "OTEL_PYTHON_SERVICE_NAME"
OTEL_TRACE_EXPORTER = "OTEL_TRACE_EXPORTER"
OTEL_METRICS_EXPORTER = "OTEL_METRICS_EXPORTER"
OTEL_PYTHON_TRACER_PROVIDER = "OTEL_PYTHON_TRACER_PROVIDER"
OTEL_PYTHON_METER_PROVIDER = "OTEL_PYTHON_METER_PROVIDER"
21 changes: 17 additions & 4 deletions opentelemetry-api/src/opentelemetry/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

from pkg_resources import iter_entry_points

from opentelemetry.environment_variables import (
OTEL_PYTHON_METER_PROVIDER,
OTEL_PYTHON_TRACER_PROVIDER,
)

if TYPE_CHECKING:
from opentelemetry.metrics import MeterProvider
from opentelemetry.trace import TracerProvider
Expand All @@ -39,15 +44,18 @@ def time_ns() -> int:
return int(time.time() * 1e9)


def _load_provider(provider: str) -> Provider:
def _load_provider(
provider_environment_variable: str, provider: str
) -> Provider:
try:
entry_point = next(
iter_entry_points(
"opentelemetry_{}".format(provider),
name=cast(
str,
environ.get(
provider.upper(), "default_{}".format(provider),
provider_environment_variable,
"default_{}".format(provider),
),
),
)
Expand All @@ -59,8 +67,13 @@ def _load_provider(provider: str) -> Provider:


def _load_meter_provider(provider: str) -> "MeterProvider":
return cast("MeterProvider", _load_provider(provider))
return cast(
"MeterProvider", _load_provider(OTEL_PYTHON_METER_PROVIDER, provider),
)


def _load_trace_provider(provider: str) -> "TracerProvider":
return cast("TracerProvider", _load_provider(provider))
return cast(
"TracerProvider",
_load_provider(OTEL_PYTHON_TRACER_PROVIDER, provider),
)

0 comments on commit b743ab1

Please sign in to comment.