Skip to content

Commit f5f888f

Browse files
committed
Make opentelemetry.environment_variables a single namespace
Fixes #1968
1 parent e78c9c4 commit f5f888f

File tree

19 files changed

+133
-37
lines changed

19 files changed

+133
-37
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.3.0-0.22b0...HEAD)
88
- `opentelemetry-semantic-conventions` Generate semconv constants update for OTel Spec 1.5.0
99
([#1946](https://github.com/open-telemetry/opentelemetry-python/pull/1946))
10+
- Make all environment variables accessible from `opentelemetry.environment_variables`.
11+
([#1969](https://github.com/open-telemetry/opentelemetry-python/pull/1969))
1012

1113
### Added
1214
- Moved `opentelemetry-instrumentation` to core repository.

opentelemetry-api/setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ opentelemetry_tracer_provider =
5656
opentelemetry_propagator =
5757
tracecontext = opentelemetry.trace.propagation.tracecontext:TraceContextTextMapPropagator
5858
baggage = opentelemetry.baggage.propagation:W3CBaggagePropagator
59+
opentelemetry_environment_variables =
60+
api = opentelemetry.environment_variables.environment_variables
5961

6062
[options.extras_require]
6163
test =

opentelemetry-api/src/opentelemetry/context/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
# pylint: disable=no-name-in-module
1415

1516
import logging
1617
import threading
@@ -22,7 +23,9 @@
2223
from pkg_resources import iter_entry_points
2324

2425
from opentelemetry.context.context import Context, _RuntimeContext
25-
from opentelemetry.environment_variables import OTEL_PYTHON_CONTEXT
26+
from opentelemetry.environment_variables import ( # type: ignore
27+
OTEL_PYTHON_CONTEXT,
28+
)
2629

2730
logger = logging.getLogger(__name__)
2831
_RUNTIME_CONTEXT = None # type: typing.Optional[_RuntimeContext]
@@ -52,7 +55,7 @@ def wrapper( # type: ignore[misc]
5255
default_context = "contextvars_context"
5356

5457
configured_context = environ.get(
55-
OTEL_PYTHON_CONTEXT, default_context
58+
OTEL_PYTHON_CONTEXT, default_context # type: ignore
5659
) # type: str
5760
try:
5861
_RUNTIME_CONTEXT = next(

opentelemetry-api/src/opentelemetry/environment_variables/__init__.py

+44-29
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,48 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
# type: ignore
1415

15-
OTEL_PROPAGATORS = "OTEL_PROPAGATORS"
16-
"""
17-
.. envvar:: OTEL_PROPAGATORS
18-
"""
19-
20-
OTEL_PYTHON_CONTEXT = "OTEL_PYTHON_CONTEXT"
21-
"""
22-
.. envvar:: OTEL_PYTHON_CONTEXT
23-
"""
24-
25-
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"
26-
"""
27-
.. envvar:: OTEL_PYTHON_DISABLED_INSTRUMENTATIONS
28-
"""
29-
30-
OTEL_PYTHON_ID_GENERATOR = "OTEL_PYTHON_ID_GENERATOR"
31-
"""
32-
.. envvar:: OTEL_PYTHON_ID_GENERATOR
33-
"""
34-
35-
OTEL_TRACES_EXPORTER = "OTEL_TRACES_EXPORTER"
36-
"""
37-
.. envvar:: OTEL_TRACES_EXPORTER
38-
"""
39-
40-
OTEL_PYTHON_TRACER_PROVIDER = "OTEL_PYTHON_TRACER_PROVIDER"
41-
"""
42-
.. envvar:: OTEL_PYTHON_TRACER_PROVIDER
43-
"""
16+
from logging import getLogger
17+
from sys import modules
18+
19+
from pkg_resources import iter_entry_points
20+
21+
_logger = getLogger(__name__)
22+
23+
_loaded = False
24+
25+
_current_module = modules[__name__]
26+
_current_module_attributes = set(dir(_current_module))
27+
28+
if not _loaded:
29+
for entry_point in iter_entry_points(
30+
"opentelemetry_environment_variables"
31+
):
32+
33+
other_module = entry_point.load()
34+
35+
for attribute in dir(other_module):
36+
37+
if attribute.startswith("OTEL_"):
38+
39+
value = getattr(other_module, attribute)
40+
41+
if attribute in _current_module_attributes:
42+
# pylint: disable=logging-not-lazy
43+
# pylint: disable=logging-format-interpolation
44+
_logger.warning(
45+
"Overriding value of {} with {}".format(
46+
attribute, value
47+
)
48+
)
49+
50+
setattr(
51+
_current_module,
52+
attribute,
53+
value
54+
)
55+
56+
_current_module_attributes.add(attribute)
57+
58+
_loaded = True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
OTEL_PROPAGATORS = "OTEL_PROPAGATORS"
16+
"""
17+
.. envvar:: OTEL_PROPAGATORS
18+
"""
19+
20+
OTEL_PYTHON_CONTEXT = "OTEL_PYTHON_CONTEXT"
21+
"""
22+
.. envvar:: OTEL_PYTHON_CONTEXT
23+
"""
24+
25+
OTEL_PYTHON_ID_GENERATOR = "OTEL_PYTHON_ID_GENERATOR"
26+
"""
27+
.. envvar:: OTEL_PYTHON_ID_GENERATOR
28+
"""
29+
30+
OTEL_TRACES_EXPORTER = "OTEL_TRACES_EXPORTER"
31+
"""
32+
.. envvar:: OTEL_TRACES_EXPORTER
33+
"""
34+
35+
OTEL_PYTHON_TRACER_PROVIDER = "OTEL_PYTHON_TRACER_PROVIDER"
36+
"""
37+
.. envvar:: OTEL_PYTHON_TRACER_PROVIDER
38+
"""

opentelemetry-api/src/opentelemetry/propagate/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
# pylint: disable=no-name-in-module
1415

1516
"""
1617
API for propagation of context.
@@ -75,7 +76,9 @@ def example_route():
7576
from pkg_resources import iter_entry_points
7677

7778
from opentelemetry.context.context import Context
78-
from opentelemetry.environment_variables import OTEL_PROPAGATORS
79+
from opentelemetry.environment_variables import ( # type: ignore
80+
OTEL_PROPAGATORS,
81+
)
7982
from opentelemetry.propagators import composite, textmap
8083

8184
logger = getLogger(__name__)
@@ -127,7 +130,7 @@ def inject(
127130

128131
# Single use variable here to hack black and make lint pass
129132
environ_propagators = environ.get(
130-
OTEL_PROPAGATORS,
133+
OTEL_PROPAGATORS, # type: ignore
131134
"tracecontext,baggage",
132135
)
133136

opentelemetry-api/src/opentelemetry/trace/__init__.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
# pylint: disable=no-name-in-module
1415

1516
"""
1617
The OpenTelemetry tracing API describes the classes used to generate
@@ -84,7 +85,9 @@
8485
from opentelemetry import context as context_api
8586
from opentelemetry.attributes import BoundedAttributes # type: ignore
8687
from opentelemetry.context.context import Context
87-
from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER
88+
from opentelemetry.environment_variables import ( # type: ignore
89+
OTEL_PYTHON_TRACER_PROVIDER,
90+
)
8891
from opentelemetry.trace.propagation import (
8992
_SPAN_KEY,
9093
get_current_span,
@@ -486,14 +489,14 @@ def get_tracer_provider() -> TracerProvider:
486489
if _TRACER_PROVIDER is None:
487490
# if a global tracer provider has not been set either via code or env
488491
# vars, return a proxy tracer provider
489-
if OTEL_PYTHON_TRACER_PROVIDER not in os.environ:
492+
if OTEL_PYTHON_TRACER_PROVIDER not in os.environ: # type: ignore
490493
if not _PROXY_TRACER_PROVIDER:
491494
_PROXY_TRACER_PROVIDER = ProxyTracerProvider()
492495
return _PROXY_TRACER_PROVIDER
493496

494497
_TRACER_PROVIDER = cast( # type: ignore
495498
"TracerProvider",
496-
_load_provider(OTEL_PYTHON_TRACER_PROVIDER, "tracer_provider"),
499+
_load_provider(OTEL_PYTHON_TRACER_PROVIDER, "tracer_provider"), # type: ignore
497500
)
498501
return _TRACER_PROVIDER
499502

opentelemetry-distro/src/opentelemetry/distro/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
#
14+
# pylint: disable=no-name-in-module
15+
1516
import os
1617
from logging import getLogger
1718
from os import environ

opentelemetry-distro/tests/test_configurator.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# type: ignore
15+
# pylint: disable=no-name-in-module
1516

1617
from os import environ
1718
from unittest import TestCase

opentelemetry-distro/tests/test_distro.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# type: ignore
15+
# pylint: disable=no-name-in-module
1516

1617
import os
1718
from unittest import TestCase

opentelemetry-instrumentation/setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ where = src
5151
console_scripts =
5252
opentelemetry-instrument = opentelemetry.instrumentation.auto_instrumentation:run
5353
opentelemetry-bootstrap = opentelemetry.instrumentation.bootstrap:run
54+
opentelemetry_environment_variables =
55+
instrumentation = opentelemetry.instrumentation.environment_variables
5456

5557
[options.extras_require]
5658
test =

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
# pylint: disable=no-name-in-module
1617

1718
import argparse
1819
from logging import getLogger

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
# pylint: disable=no-name-in-module
1415

1516
import sys
1617
from logging import getLogger
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"
16+
"""
17+
.. envvar:: OTEL_PYTHON_DISABLED_INSTRUMENTATIONS
18+
"""

opentelemetry-instrumentation/tests/test_distro.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# type: ignore
15+
# pylint: disable=no-name-in-module
1516

1617
from unittest import TestCase
1718

opentelemetry-instrumentation/tests/test_run.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# type: ignore
15+
# pylint: disable=no-name-in-module
1516

1617
from os import environ, getcwd
1718
from os.path import abspath, dirname, pathsep

opentelemetry-sdk/setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ opentelemetry_exporter =
5656
console_span = opentelemetry.sdk.trace.export:ConsoleSpanExporter
5757
opentelemetry_id_generator =
5858
random = opentelemetry.sdk.trace.id_generator:RandomIdGenerator
59+
opentelemetry_environment_variables =
60+
sdk = opentelemetry.sdk.environment_variables
5961

6062
[options.extras_require]
6163
test =

opentelemetry-sdk/tests/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
# pylint: disable=no-name-in-module
1415

1516
from os import environ
1617

0 commit comments

Comments
 (0)