From 10ab476076b9f3a1641d40adb4cbac150990aa60 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 2 Feb 2021 18:57:22 -0600 Subject: [PATCH] Use environment variables for tracer and meter providers Fixes #1569 --- .../environment_variables/__init__.py | 2 ++ .../src/opentelemetry/util/__init__.py | 21 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/environment_variables/__init__.py b/opentelemetry-api/src/opentelemetry/environment_variables/__init__.py index 1bd5a6f46d2..810b24ca6de 100644 --- a/opentelemetry-api/src/opentelemetry/environment_variables/__init__.py +++ b/opentelemetry-api/src/opentelemetry/environment_variables/__init__.py @@ -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" diff --git a/opentelemetry-api/src/opentelemetry/util/__init__.py b/opentelemetry-api/src/opentelemetry/util/__init__.py index c49ba1b353c..bb07b2c515b 100644 --- a/opentelemetry-api/src/opentelemetry/util/__init__.py +++ b/opentelemetry-api/src/opentelemetry/util/__init__.py @@ -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 @@ -39,7 +44,9 @@ 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( @@ -47,7 +54,8 @@ def _load_provider(provider: str) -> Provider: name=cast( str, environ.get( - provider.upper(), "default_{}".format(provider), + provider_environment_variable, + "default_{}".format(provider), ), ), ) @@ -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), + )