From b5661f4cd16f3a3ebd0abd7ce1b48a13594d8dd4 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Sun, 18 Jul 2021 20:09:16 -0600 Subject: [PATCH] Make opentelemetry.environment_variables a single namespace Fixes #1968 --- CHANGELOG.md | 2 + opentelemetry-api/setup.cfg | 2 + .../src/opentelemetry/context/__init__.py | 7 +- .../environment_variables/__init__.py | 69 +++++++++++-------- .../environment_variables.py | 38 ++++++++++ .../src/opentelemetry/propagate/__init__.py | 7 +- .../src/opentelemetry/trace/__init__.py | 9 ++- .../src/opentelemetry/distro/__init__.py | 3 +- opentelemetry-distro/tests/test_distro.py | 1 + opentelemetry-instrumentation/setup.cfg | 2 + .../auto_instrumentation/__init__.py | 1 + .../auto_instrumentation/sitecustomize.py | 1 + .../instrumentation/environment_variables.py | 18 +++++ .../tests/test_distro.py | 1 + .../tests/test_run.py | 1 + opentelemetry-sdk/setup.cfg | 2 + .../__init__.py => environment_variables.py} | 0 opentelemetry-sdk/tests/conftest.py | 1 + opentelemetry-sdk/tests/test_configurator.py | 1 + 19 files changed, 129 insertions(+), 37 deletions(-) create mode 100644 opentelemetry-api/src/opentelemetry/environment_variables/environment_variables.py create mode 100644 opentelemetry-instrumentation/src/opentelemetry/instrumentation/environment_variables.py rename opentelemetry-sdk/src/opentelemetry/sdk/{environment_variables/__init__.py => environment_variables.py} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcc8a126320..557f66838a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ 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.4.0-0.23b0...HEAD) +- Automatically load OTEL environment variables as options for `opentelemetry-instrument` + ([#1969](https://github.com/open-telemetry/opentelemetry-python/pull/1969)) - Fix documentation on well known exporters and variable OTEL_TRACES_EXPORTER which were misnamed ([#2023](https://github.com/open-telemetry/opentelemetry-python/pull/2023)) - `opentelemetry-sdk` `get_aggregated_resource()` returns default resource and service name diff --git a/opentelemetry-api/setup.cfg b/opentelemetry-api/setup.cfg index 99d52838fb1..5a2951e923e 100644 --- a/opentelemetry-api/setup.cfg +++ b/opentelemetry-api/setup.cfg @@ -56,6 +56,8 @@ opentelemetry_tracer_provider = opentelemetry_propagator = tracecontext = opentelemetry.trace.propagation.tracecontext:TraceContextTextMapPropagator baggage = opentelemetry.baggage.propagation:W3CBaggagePropagator +opentelemetry_environment_variables = + api = opentelemetry.environment_variables.environment_variables [options.extras_require] test = diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index a8dd39c53ab..167ad8f337c 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# pylint: disable=no-name-in-module import logging import threading @@ -22,7 +23,9 @@ from pkg_resources import iter_entry_points from opentelemetry.context.context import Context, _RuntimeContext -from opentelemetry.environment_variables import OTEL_PYTHON_CONTEXT +from opentelemetry.environment_variables import ( # type: ignore + OTEL_PYTHON_CONTEXT, +) logger = logging.getLogger(__name__) _RUNTIME_CONTEXT = None # type: typing.Optional[_RuntimeContext] @@ -52,7 +55,7 @@ def wrapper( # type: ignore[misc] default_context = "contextvars_context" configured_context = environ.get( - OTEL_PYTHON_CONTEXT, default_context + OTEL_PYTHON_CONTEXT, default_context # type: ignore ) # type: str try: _RUNTIME_CONTEXT = next( diff --git a/opentelemetry-api/src/opentelemetry/environment_variables/__init__.py b/opentelemetry-api/src/opentelemetry/environment_variables/__init__.py index 6077115b013..a119f9ab9df 100644 --- a/opentelemetry-api/src/opentelemetry/environment_variables/__init__.py +++ b/opentelemetry-api/src/opentelemetry/environment_variables/__init__.py @@ -11,33 +11,44 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# type: ignore -OTEL_PROPAGATORS = "OTEL_PROPAGATORS" -""" -.. envvar:: OTEL_PROPAGATORS -""" - -OTEL_PYTHON_CONTEXT = "OTEL_PYTHON_CONTEXT" -""" -.. envvar:: OTEL_PYTHON_CONTEXT -""" - -OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "OTEL_PYTHON_DISABLED_INSTRUMENTATIONS" -""" -.. envvar:: OTEL_PYTHON_DISABLED_INSTRUMENTATIONS -""" - -OTEL_PYTHON_ID_GENERATOR = "OTEL_PYTHON_ID_GENERATOR" -""" -.. envvar:: OTEL_PYTHON_ID_GENERATOR -""" - -OTEL_TRACES_EXPORTER = "OTEL_TRACES_EXPORTER" -""" -.. envvar:: OTEL_TRACES_EXPORTER -""" - -OTEL_PYTHON_TRACER_PROVIDER = "OTEL_PYTHON_TRACER_PROVIDER" -""" -.. envvar:: OTEL_PYTHON_TRACER_PROVIDER -""" +from logging import getLogger +from sys import modules + +from pkg_resources import iter_entry_points + +_logger = getLogger(__name__) + +_loaded = False + +_current_module = modules[__name__] +_current_module_attributes = set(dir(_current_module)) + +if not _loaded: + for entry_point in iter_entry_points( + "opentelemetry_environment_variables" + ): + + other_module = entry_point.load() + + for attribute in dir(other_module): + + if attribute.startswith("OTEL_"): + + value = getattr(other_module, attribute) + + if attribute in _current_module_attributes: + # pylint: disable=logging-not-lazy + # pylint: disable=logging-format-interpolation + _logger.warning( + "Overriding value of {} with {}".format( + attribute, value + ) + ) + + setattr(_current_module, attribute, value) + + _current_module_attributes.add(attribute) + +_loaded = True diff --git a/opentelemetry-api/src/opentelemetry/environment_variables/environment_variables.py b/opentelemetry-api/src/opentelemetry/environment_variables/environment_variables.py new file mode 100644 index 00000000000..83ec67149dd --- /dev/null +++ b/opentelemetry-api/src/opentelemetry/environment_variables/environment_variables.py @@ -0,0 +1,38 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +OTEL_PROPAGATORS = "OTEL_PROPAGATORS" +""" +.. envvar:: OTEL_PROPAGATORS +""" + +OTEL_PYTHON_CONTEXT = "OTEL_PYTHON_CONTEXT" +""" +.. envvar:: OTEL_PYTHON_CONTEXT +""" + +OTEL_PYTHON_ID_GENERATOR = "OTEL_PYTHON_ID_GENERATOR" +""" +.. envvar:: OTEL_PYTHON_ID_GENERATOR +""" + +OTEL_TRACES_EXPORTER = "OTEL_TRACES_EXPORTER" +""" +.. envvar:: OTEL_TRACES_EXPORTER +""" + +OTEL_PYTHON_TRACER_PROVIDER = "OTEL_PYTHON_TRACER_PROVIDER" +""" +.. envvar:: OTEL_PYTHON_TRACER_PROVIDER +""" diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 3ad1ad544ab..20f19d1f355 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# pylint: disable=no-name-in-module """ API for propagation of context. @@ -75,7 +76,9 @@ def example_route(): from pkg_resources import iter_entry_points from opentelemetry.context.context import Context -from opentelemetry.environment_variables import OTEL_PROPAGATORS +from opentelemetry.environment_variables import ( # type: ignore + OTEL_PROPAGATORS, +) from opentelemetry.propagators import composite, textmap logger = getLogger(__name__) @@ -127,7 +130,7 @@ def inject( # Single use variable here to hack black and make lint pass environ_propagators = environ.get( - OTEL_PROPAGATORS, + OTEL_PROPAGATORS, # type: ignore "tracecontext,baggage", ) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 58d75bbea88..55a39f8fe0b 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# pylint: disable=no-name-in-module """ The OpenTelemetry tracing API describes the classes used to generate @@ -84,7 +85,9 @@ from opentelemetry import context as context_api from opentelemetry.attributes import BoundedAttributes # type: ignore from opentelemetry.context.context import Context -from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER +from opentelemetry.environment_variables import ( # type: ignore + OTEL_PYTHON_TRACER_PROVIDER, +) from opentelemetry.trace.propagation import ( _SPAN_KEY, get_current_span, @@ -486,14 +489,14 @@ def get_tracer_provider() -> TracerProvider: if _TRACER_PROVIDER is None: # if a global tracer provider has not been set either via code or env # vars, return a proxy tracer provider - if OTEL_PYTHON_TRACER_PROVIDER not in os.environ: + if OTEL_PYTHON_TRACER_PROVIDER not in os.environ: # type: ignore if not _PROXY_TRACER_PROVIDER: _PROXY_TRACER_PROVIDER = ProxyTracerProvider() return _PROXY_TRACER_PROVIDER _TRACER_PROVIDER = cast( # type: ignore "TracerProvider", - _load_provider(OTEL_PYTHON_TRACER_PROVIDER, "tracer_provider"), + _load_provider(OTEL_PYTHON_TRACER_PROVIDER, "tracer_provider"), # type: ignore ) return _TRACER_PROVIDER diff --git a/opentelemetry-distro/src/opentelemetry/distro/__init__.py b/opentelemetry-distro/src/opentelemetry/distro/__init__.py index e70cb67335d..c38d853c5ec 100644 --- a/opentelemetry-distro/src/opentelemetry/distro/__init__.py +++ b/opentelemetry-distro/src/opentelemetry/distro/__init__.py @@ -11,7 +11,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# +# pylint: disable=no-name-in-module + import os from opentelemetry.environment_variables import OTEL_TRACES_EXPORTER diff --git a/opentelemetry-distro/tests/test_distro.py b/opentelemetry-distro/tests/test_distro.py index 2e42ed904a9..9798b4637ce 100644 --- a/opentelemetry-distro/tests/test_distro.py +++ b/opentelemetry-distro/tests/test_distro.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # type: ignore +# pylint: disable=no-name-in-module import os from unittest import TestCase diff --git a/opentelemetry-instrumentation/setup.cfg b/opentelemetry-instrumentation/setup.cfg index 72714c581bb..a416950fcf0 100644 --- a/opentelemetry-instrumentation/setup.cfg +++ b/opentelemetry-instrumentation/setup.cfg @@ -51,6 +51,8 @@ where = src console_scripts = opentelemetry-instrument = opentelemetry.instrumentation.auto_instrumentation:run opentelemetry-bootstrap = opentelemetry.instrumentation.bootstrap:run +opentelemetry_environment_variables = + instrumentation = opentelemetry.instrumentation.environment_variables [options.extras_require] test = diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py index 9f076b340e5..2654c0592e2 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py @@ -13,6 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# pylint: disable=no-name-in-module import argparse from logging import getLogger diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index d89b60ec56c..08871583204 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# pylint: disable=no-name-in-module import sys from logging import getLogger diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/environment_variables.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/environment_variables.py new file mode 100644 index 00000000000..ad28f068590 --- /dev/null +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/environment_variables.py @@ -0,0 +1,18 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "OTEL_PYTHON_DISABLED_INSTRUMENTATIONS" +""" +.. envvar:: OTEL_PYTHON_DISABLED_INSTRUMENTATIONS +""" diff --git a/opentelemetry-instrumentation/tests/test_distro.py b/opentelemetry-instrumentation/tests/test_distro.py index 399b3f8a654..cde6502bb8e 100644 --- a/opentelemetry-instrumentation/tests/test_distro.py +++ b/opentelemetry-instrumentation/tests/test_distro.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # type: ignore +# pylint: disable=no-name-in-module from unittest import TestCase diff --git a/opentelemetry-instrumentation/tests/test_run.py b/opentelemetry-instrumentation/tests/test_run.py index 01bd86ed32f..d60b5f465cb 100644 --- a/opentelemetry-instrumentation/tests/test_run.py +++ b/opentelemetry-instrumentation/tests/test_run.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # type: ignore +# pylint: disable=no-name-in-module from os import environ, getcwd from os.path import abspath, dirname, pathsep diff --git a/opentelemetry-sdk/setup.cfg b/opentelemetry-sdk/setup.cfg index e8cee4d6b7b..5838937d186 100644 --- a/opentelemetry-sdk/setup.cfg +++ b/opentelemetry-sdk/setup.cfg @@ -56,6 +56,8 @@ opentelemetry_exporter = console_span = opentelemetry.sdk.trace.export:ConsoleSpanExporter opentelemetry_id_generator = random = opentelemetry.sdk.trace.id_generator:RandomIdGenerator +opentelemetry_environment_variables = + sdk = opentelemetry.sdk.environment_variables [options.extras_require] test = diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py similarity index 100% rename from opentelemetry-sdk/src/opentelemetry/sdk/environment_variables/__init__.py rename to opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py diff --git a/opentelemetry-sdk/tests/conftest.py b/opentelemetry-sdk/tests/conftest.py index 92fd7a734de..6d380246f7b 100644 --- a/opentelemetry-sdk/tests/conftest.py +++ b/opentelemetry-sdk/tests/conftest.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# pylint: disable=no-name-in-module from os import environ diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index e7619d64dcc..fe77be77f25 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # type: ignore +# pylint: disable=no-name-in-module from os import environ from unittest import TestCase