Skip to content

Commit 432ae70

Browse files
committed
Remove Configuration
1 parent d12f67f commit 432ae70

File tree

19 files changed

+289
-73
lines changed

19 files changed

+289
-73
lines changed

Diff for: CHANGELOG.md

+4
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-contrib/compare/v0.16b1...HEAD)
88

99
### Added
10+
- Remove Configuration
11+
([#285](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/285))
1012
- `opentelemetry-instrumentation-sqlalchemy` Ensure spans have kind set to "CLIENT"
1113
([#278](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/278))
1214
- `opentelemetry-instrumentation-celery` Add support for Celery version 5.x
@@ -41,6 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4143
([#273](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/273))
4244

4345
### Changed
46+
- Remove Configuration
47+
([#285](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/285))
4448
- `opentelemetry-instrumentation-asgi`, `opentelemetry-instrumentation-wsgi` Return `None` for `CarrierGetter` if key not found
4549
([#1374](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/233))
4650
- `opentelemetry-instrumentation-grpc` Comply with updated spec, rework tests

Diff for: instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def _instrument(self, **kwargs):
8484
# For exemple EC2 uses AWSQueryConnection and S3 uses
8585
# AWSAuthConnection
8686

87-
# FIXME should the tracer provider be accessed via Configuration
88-
# instead?
8987
# pylint: disable=attribute-defined-outside-init
9088
self._tracer = get_tracer(
9189
__name__, __version__, kwargs.get("tracer_provider")

Diff for: instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/__init__.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
from logging import getLogger
16+
from os import environ
1617

1718
from django.conf import settings
1819

19-
from opentelemetry.configuration import Configuration
2020
from opentelemetry.instrumentation.django.middleware import _DjangoMiddleware
2121
from opentelemetry.instrumentation.django.version import __version__
2222
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -43,11 +43,7 @@ def _instrument(self, **kwargs):
4343

4444
# FIXME this is probably a pattern that will show up in the rest of the
4545
# ext. Find a better way of implementing this.
46-
# FIXME Probably the evaluation of strings into boolean values can be
47-
# built inside the Configuration class itself with the magic method
48-
# __bool__
49-
50-
if Configuration().DJANGO_INSTRUMENT is False:
46+
if environ.get("OTEL_PYTHON_DJANGO_INSTRUMENT") == "False":
5147
return
5248

5349
# This can not be solved, but is an inherent problem of this approach:

Diff for: instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

+47-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import time
15+
from time import time
1616
from logging import getLogger
17+
from os import environ
18+
from re import compile as re_compile, search
1719

1820
from django.conf import settings
1921

20-
from opentelemetry.configuration import Configuration
2122
from opentelemetry.context import attach, detach
2223
from opentelemetry.instrumentation.django.version import __version__
2324
from opentelemetry.instrumentation.utils import extract_attributes_from_object
@@ -51,6 +52,46 @@
5152
]
5253

5354

55+
class _ExcludeList:
56+
"""Class to exclude certain paths (given as a list of regexes) from tracing requests"""
57+
58+
def __init__(self, excluded_urls):
59+
self._excluded_urls = excluded_urls
60+
if self._excluded_urls:
61+
self._regex = re_compile("|".join(excluded_urls))
62+
63+
def url_disabled(self, url: str) -> bool:
64+
return bool(self._excluded_urls and search(self._regex, url))
65+
66+
_root = r"OTEL_PYTHON_{}"
67+
68+
69+
def _get_traced_request_attrs():
70+
traced_request_attrs = environ.get(
71+
_root.format("DJANGO_TRACED_REQUEST_ATTRS"), []
72+
)
73+
74+
if traced_request_attrs:
75+
traced_request_attrs = [
76+
traced_request_attr.strip()
77+
for traced_request_attr in traced_request_attrs.split(",")
78+
]
79+
80+
return traced_request_attrs
81+
82+
83+
def _get_excluded_urls():
84+
excluded_urls = environ.get(_root.format("DJANGO_EXCLUDED_URLS"), [])
85+
86+
if excluded_urls:
87+
excluded_urls = [
88+
excluded_url.strip()
89+
for excluded_url in excluded_urls.split(",")
90+
]
91+
92+
return _ExcludeList(excluded_urls)
93+
94+
5495
class _DjangoMiddleware(MiddlewareMixin):
5596
"""Django Middleware for OpenTelemetry"""
5697

@@ -61,9 +102,8 @@ class _DjangoMiddleware(MiddlewareMixin):
61102
_environ_span_key = "opentelemetry-instrumentor-django.span_key"
62103
_environ_exception_key = "opentelemetry-instrumentor-django.exception_key"
63104

64-
_excluded_urls = Configuration()._excluded_urls("django")
65-
66-
_traced_request_attrs = Configuration()._traced_request_attrs("django")
105+
_traced_request_attrs = _get_traced_request_attrs()
106+
_excluded_urls = _get_excluded_urls()
67107

68108
@staticmethod
69109
def _get_span_name(request):
@@ -111,7 +151,7 @@ def process_request(self, request):
111151
return
112152

113153
# pylint:disable=W0212
114-
request._otel_start_time = time.time()
154+
request._otel_start_time = time()
115155

116156
environ = request.META
117157

@@ -215,7 +255,7 @@ def process_response(self, request, response):
215255
if metric_recorder is not None:
216256
# pylint:disable=W0212
217257
metric_recorder.record_server_duration_range(
218-
request._otel_start_time, time.time(), request._otel_labels
258+
request._otel_start_time, time(), request._otel_labels
219259
)
220260
except Exception as ex: # pylint: disable=W0703
221261
_logger.warning("Error recording duration metrics: %s", ex)

Diff for: instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
from django.test import Client
2222
from django.test.utils import setup_test_environment, teardown_test_environment
2323

24-
from opentelemetry.configuration import Configuration
2524
from opentelemetry.instrumentation.django import DjangoInstrumentor
25+
from opentelemetry.instrumentation.django.middleware import (
26+
_get_excluded_urls, _get_traced_request_attrs
27+
)
2628
from opentelemetry.sdk.util import get_dict_as_key
2729
from opentelemetry.test.test_base import TestBase
2830
from opentelemetry.test.wsgitestutil import WsgiTestBase
@@ -64,7 +66,6 @@ def setUp(self):
6466
super().setUp()
6567
setup_test_environment()
6668
_django_instrumentor.instrument()
67-
Configuration._reset() # pylint: disable=protected-access
6869
self.env_patch = patch.dict(
6970
"os.environ",
7071
{
@@ -75,11 +76,11 @@ def setUp(self):
7576
self.env_patch.start()
7677
self.exclude_patch = patch(
7778
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._excluded_urls",
78-
Configuration()._excluded_urls("django"),
79+
_get_excluded_urls(),
7980
)
8081
self.traced_patch = patch(
8182
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
82-
Configuration()._traced_request_attrs("django"),
83+
_get_traced_request_attrs(),
8384
)
8485
self.exclude_patch.start()
8586
self.traced_patch.start()

Diff for: instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py

+49-7
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ def on_get(self, req, resp):
4343
---
4444
"""
4545

46-
import sys
46+
from sys import exc_info
4747
from logging import getLogger
48+
from re import compile as re_compile, search
49+
from os import environ
4850

4951
import falcon
5052

5153
import opentelemetry.instrumentation.wsgi as otel_wsgi
52-
from opentelemetry import configuration, context, propagators, trace
53-
from opentelemetry.configuration import Configuration
54+
from opentelemetry import context, propagators, trace
5455
from opentelemetry.instrumentation.falcon.version import __version__
5556
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
5657
from opentelemetry.instrumentation.utils import (
@@ -68,8 +69,49 @@ def on_get(self, req, resp):
6869
_ENVIRON_TOKEN = "opentelemetry-falcon.token"
6970
_ENVIRON_EXC = "opentelemetry-falcon.exc"
7071

71-
cfg = configuration.Configuration()
72-
_excluded_urls = cfg._excluded_urls("falcon")
72+
73+
class _ExcludeList:
74+
"""Class to exclude certain paths (given as a list of regexes) from tracing requests"""
75+
76+
def __init__(self, excluded_urls):
77+
self._excluded_urls = excluded_urls
78+
if self._excluded_urls:
79+
self._regex = re_compile("|".join(excluded_urls))
80+
81+
def url_disabled(self, url: str) -> bool:
82+
return bool(self._excluded_urls and search(self._regex, url))
83+
84+
_root = r"OTEL_PYTHON_{}"
85+
86+
87+
def _get_traced_request_attrs():
88+
traced_request_attrs = environ.get(
89+
_root.format("FALCON_TRACED_REQUEST_ATTRS"), []
90+
)
91+
92+
if traced_request_attrs:
93+
traced_request_attrs = [
94+
traced_request_attr.strip()
95+
for traced_request_attr in traced_request_attrs.split(",")
96+
]
97+
98+
return traced_request_attrs
99+
100+
101+
def _get_excluded_urls():
102+
excluded_urls = environ.get(_root.format("FALCON_EXCLUDED_URLS"), [])
103+
104+
if excluded_urls:
105+
excluded_urls = [
106+
excluded_url.strip()
107+
for excluded_url in excluded_urls.split(",")
108+
]
109+
110+
return _ExcludeList(excluded_urls)
111+
112+
113+
_excluded_urls = _get_excluded_urls()
114+
_traced_request_attrs = _get_traced_request_attrs()
73115

74116

75117
class FalconInstrumentor(BaseInstrumentor):
@@ -149,7 +191,7 @@ class _TraceMiddleware:
149191

150192
def __init__(self, tracer=None, traced_request_attrs=None):
151193
self.tracer = tracer
152-
self._traced_request_attrs = cfg._traced_request_attrs("falcon")
194+
self._traced_request_attrs = _traced_request_attrs
153195

154196
def process_request(self, req, resp):
155197
span = req.env.get(_ENVIRON_SPAN_KEY)
@@ -186,7 +228,7 @@ def process_response(
186228
status = "404"
187229
reason = "NotFound"
188230

189-
exc_type, exc, _ = sys.exc_info()
231+
exc_type, exc, _ = exc_info()
190232
if exc_type and not req_succeeded:
191233
if "HTTPNotFound" in exc_type.__name__:
192234
status = "404"

Diff for: instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
from falcon import testing
1818

19-
from opentelemetry.configuration import Configuration
20-
from opentelemetry.instrumentation.falcon import FalconInstrumentor
19+
from opentelemetry.instrumentation.falcon import (
20+
FalconInstrumentor, _get_excluded_urls, _get_traced_request_attrs
21+
)
2122
from opentelemetry.test.test_base import TestBase
2223
from opentelemetry.trace.status import StatusCode
2324

@@ -30,7 +31,6 @@ def setUp(self):
3031
FalconInstrumentor().instrument()
3132
self.app = make_app()
3233
# pylint: disable=protected-access
33-
Configuration()._reset()
3434
self.env_patch = patch.dict(
3535
"os.environ",
3636
{
@@ -41,15 +41,15 @@ def setUp(self):
4141
self.env_patch.start()
4242
self.exclude_patch = patch(
4343
"opentelemetry.instrumentation.falcon._excluded_urls",
44-
Configuration()._excluded_urls("falcon"),
44+
_get_excluded_urls(),
4545
)
4646
middleware = self.app._middleware[0][ # pylint:disable=W0212
4747
0
4848
].__self__
4949
self.traced_patch = patch.object(
5050
middleware,
5151
"_traced_request_attrs",
52-
Configuration()._traced_request_attrs("falcon"),
52+
_get_traced_request_attrs(),
5353
)
5454
self.exclude_patch.start()
5555
self.traced_patch.start()

Diff for: instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py

+30-4
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,43 @@
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-
from typing import Optional
1514

16-
import fastapi
15+
from re import compile as re_compile, search
16+
from os import environ
17+
1718
from starlette.routing import Match
19+
import fastapi
1820

19-
from opentelemetry.configuration import Configuration
2021
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
2122
from opentelemetry.instrumentation.fastapi.version import __version__ # noqa
2223
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
2324

24-
_excluded_urls = Configuration()._excluded_urls("fastapi")
25+
26+
class _ExcludeList:
27+
"""Class to exclude certain paths (given as a list of regexes) from tracing requests"""
28+
29+
def __init__(self, excluded_urls):
30+
self._excluded_urls = excluded_urls
31+
if self._excluded_urls:
32+
self._regex = re_compile("|".join(excluded_urls))
33+
34+
def url_disabled(self, url: str) -> bool:
35+
return bool(self._excluded_urls and search(self._regex, url))
36+
37+
38+
def _get_excluded_urls():
39+
excluded_urls = environ.get("OTEL_PYTHON_FASTAPI_EXCLUDED_URLS", [])
40+
41+
if excluded_urls:
42+
excluded_urls = [
43+
excluded_url.strip()
44+
for excluded_url in excluded_urls.split(",")
45+
]
46+
47+
return _ExcludeList(excluded_urls)
48+
49+
50+
_excluded_urls = _get_excluded_urls()
2551

2652

2753
class FastAPIInstrumentor(BaseInstrumentor):

Diff for: instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from fastapi.testclient import TestClient
2020

2121
import opentelemetry.instrumentation.fastapi as otel_fastapi
22-
from opentelemetry.configuration import Configuration
2322
from opentelemetry.test.test_base import TestBase
2423

2524

@@ -31,15 +30,14 @@ def _create_app(self):
3130

3231
def setUp(self):
3332
super().setUp()
34-
Configuration()._reset()
3533
self.env_patch = patch.dict(
3634
"os.environ",
3735
{"OTEL_PYTHON_FASTAPI_EXCLUDED_URLS": "/exclude/123,healthzz"},
3836
)
3937
self.env_patch.start()
4038
self.exclude_patch = patch(
4139
"opentelemetry.instrumentation.fastapi._excluded_urls",
42-
Configuration()._excluded_urls("fastapi"),
40+
otel_fastapi._get_excluded_urls(),
4341
)
4442
self.exclude_patch.start()
4543
self._instrumentor = otel_fastapi.FastAPIInstrumentor()

0 commit comments

Comments
 (0)