Skip to content

Commit 2be6c6e

Browse files
committed
Add HTTP util
This moves the WSGI and ASGI instrumentations and some other functionality to a new package for HTTP-related functionality.
1 parent 55a5861 commit 2be6c6e

File tree

43 files changed

+160
-584
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+160
-584
lines changed

Diff for: .pylintrc

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ disable=missing-docstring,
7979
super-init-not-called, # temp-pylint-upgrade
8080
invalid-overridden-method, # temp-pylint-upgrade
8181
missing-module-docstring, # temp-pylint-upgrade
82+
import-error, # needed as a workaround as reported here: https://github.com/open-telemetry/opentelemetry-python-contrib/issues/290
8283

8384
# Enable the message, report, category or checker with the given id(s). You can
8485
# either give multiple identifier separated by comma (,) or put this option

Diff for: instrumentation/opentelemetry-instrumentation-django/setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ package_dir=
4040
packages=find_namespace:
4141
install_requires =
4242
django >= 1.10
43-
opentelemetry-instrumentation-wsgi == 0.18.dev0
43+
opentelemetry-util-http == 0.18.dev0
4444
opentelemetry-instrumentation == 0.18.dev0
4545
opentelemetry-api == 0.18.dev0
4646

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

+6-48
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@
1313
# limitations under the License.
1414

1515
from logging import getLogger
16-
from os import environ
17-
from re import compile as re_compile
18-
from re import search
1916
from time import time
2017

2118
from django.conf import settings
2219

2320
from opentelemetry.context import attach, detach
2421
from opentelemetry.instrumentation.django.version import __version__
2522
from opentelemetry.instrumentation.utils import extract_attributes_from_object
26-
from opentelemetry.instrumentation.wsgi import (
23+
from opentelemetry.propagators import extract
24+
from opentelemetry.trace import SpanKind, get_tracer
25+
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
26+
from opentelemetry.util.http.wsgi import (
2727
add_response_attributes,
2828
carrier_getter,
2929
collect_request_attributes,
3030
)
31-
from opentelemetry.propagators import extract
32-
from opentelemetry.trace import SpanKind, get_tracer
3331

3432
try:
3533
from django.core.urlresolvers import ( # pylint: disable=no-name-in-module
@@ -53,46 +51,6 @@
5351
]
5452

5553

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

@@ -103,8 +61,8 @@ class _DjangoMiddleware(MiddlewareMixin):
10361
_environ_span_key = "opentelemetry-instrumentor-django.span_key"
10462
_environ_exception_key = "opentelemetry-instrumentor-django.exception_key"
10563

106-
_traced_request_attrs = _get_traced_request_attrs()
107-
_excluded_urls = _get_excluded_urls()
64+
_traced_request_attrs = get_traced_request_attrs("DJANGO")
65+
_excluded_urls = get_excluded_urls("DJANGO")
10866

10967
@staticmethod
11068
def _get_span_name(request):

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@
2222
from django.test.utils import setup_test_environment, teardown_test_environment
2323

2424
from opentelemetry.instrumentation.django import DjangoInstrumentor
25-
from opentelemetry.instrumentation.django.middleware import (
26-
_get_excluded_urls,
27-
_get_traced_request_attrs,
28-
)
2925
from opentelemetry.sdk.util import get_dict_as_key
3026
from opentelemetry.test.test_base import TestBase
3127
from opentelemetry.test.wsgitestutil import WsgiTestBase
3228
from opentelemetry.trace import SpanKind
3329
from opentelemetry.trace.status import StatusCode
30+
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
3431

3532
# pylint: disable=import-error
3633
from .views import (
@@ -77,11 +74,11 @@ def setUp(self):
7774
self.env_patch.start()
7875
self.exclude_patch = patch(
7976
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._excluded_urls",
80-
_get_excluded_urls(),
77+
get_excluded_urls("DJANGO"),
8178
)
8279
self.traced_patch = patch(
8380
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
84-
_get_traced_request_attrs(),
81+
get_traced_request_attrs("DJANGO"),
8582
)
8683
self.exclude_patch.start()
8784
self.traced_patch.start()

Diff for: instrumentation/opentelemetry-instrumentation-falcon/setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ package_dir=
4141
packages=find_namespace:
4242
install_requires =
4343
falcon ~= 2.0
44-
opentelemetry-instrumentation-wsgi == 0.18.dev0
44+
opentelemetry-util-http == 0.18.dev0
4545
opentelemetry-instrumentation == 0.18.dev0
4646
opentelemetry-api == 0.18.dev0
4747

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

+5-47
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""
1616
This library builds on the OpenTelemetry WSGI middleware to track web requests
17-
in Falcon applications. In addition to opentelemetry-instrumentation-wsgi,
17+
in Falcon applications. In addition to opentelemetry-util-http,
1818
it supports falcon-specific features such as:
1919
2020
* The Falcon resource and method name is used as the Span name.
@@ -44,14 +44,11 @@ def on_get(self, req, resp):
4444
"""
4545

4646
from logging import getLogger
47-
from os import environ
48-
from re import compile as re_compile
49-
from re import search
5047
from sys import exc_info
5148

5249
import falcon
5350

54-
import opentelemetry.instrumentation.wsgi as otel_wsgi
51+
import opentelemetry.util.http.wsgi as otel_wsgi
5552
from opentelemetry import context, propagators, trace
5653
from opentelemetry.instrumentation.falcon.version import __version__
5754
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -61,6 +58,7 @@ def on_get(self, req, resp):
6158
)
6259
from opentelemetry.trace.status import Status
6360
from opentelemetry.util import time_ns
61+
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
6462

6563
_logger = getLogger(__name__)
6664

@@ -71,48 +69,8 @@ def on_get(self, req, resp):
7169
_ENVIRON_EXC = "opentelemetry-falcon.exc"
7270

7371

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

11775

11876
class FalconInstrumentor(BaseInstrumentor):

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
from falcon import testing
1818

19-
from opentelemetry.instrumentation.falcon import (
20-
FalconInstrumentor,
21-
_get_excluded_urls,
22-
_get_traced_request_attrs,
23-
)
19+
from opentelemetry.instrumentation.falcon import FalconInstrumentor
2420
from opentelemetry.test.test_base import TestBase
2521
from opentelemetry.trace.status import StatusCode
22+
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
2623

2724
from .app import make_app
2825

@@ -43,13 +40,15 @@ def setUp(self):
4340
self.env_patch.start()
4441
self.exclude_patch = patch(
4542
"opentelemetry.instrumentation.falcon._excluded_urls",
46-
_get_excluded_urls(),
43+
get_excluded_urls("FALCON"),
4744
)
4845
middleware = self.app._middleware[0][ # pylint:disable=W0212
4946
0
5047
].__self__
5148
self.traced_patch = patch.object(
52-
middleware, "_traced_request_attrs", _get_traced_request_attrs(),
49+
middleware,
50+
"_traced_request_attrs",
51+
get_traced_request_attrs("FALCON"),
5352
)
5453
self.exclude_patch.start()
5554
self.traced_patch.start()

Diff for: instrumentation/opentelemetry-instrumentation-fastapi/setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ package_dir=
3939
packages=find_namespace:
4040
install_requires =
4141
opentelemetry-api == 0.18.dev0
42-
opentelemetry-instrumentation-asgi == 0.18.dev0
42+
opentelemetry-util-http == 0.18.dev0
4343

4444
[options.entry_points]
4545
opentelemetry_instrumentor =

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

+3-31
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from os import environ
16-
from re import compile as re_compile
17-
from re import search
18-
1915
import fastapi
2016
from starlette.routing import Match
2117

22-
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
23-
from opentelemetry.instrumentation.fastapi.version import __version__ # noqa
2418
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
19+
from opentelemetry.util.http import get_excluded_urls
20+
from opentelemetry.util.http.asgi import OpenTelemetryMiddleware
2521

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

5224

5325
class FastAPIInstrumentor(BaseInstrumentor):

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import opentelemetry.instrumentation.fastapi as otel_fastapi
2222
from opentelemetry.test.test_base import TestBase
23+
from opentelemetry.util.http import get_excluded_urls
2324

2425

2526
class TestFastAPIManualInstrumentation(TestBase):
@@ -37,7 +38,7 @@ def setUp(self):
3738
self.env_patch.start()
3839
self.exclude_patch = patch(
3940
"opentelemetry.instrumentation.fastapi._excluded_urls",
40-
otel_fastapi._get_excluded_urls(),
41+
get_excluded_urls("FASTAPI"),
4142
)
4243
self.exclude_patch.start()
4344
self._instrumentor = otel_fastapi.FastAPIInstrumentor()

Diff for: instrumentation/opentelemetry-instrumentation-flask/setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ package_dir=
4040
packages=find_namespace:
4141
install_requires =
4242
flask ~= 1.0
43-
opentelemetry-instrumentation-wsgi == 0.18.dev0
43+
opentelemetry-util-http == 0.18.dev0
4444
opentelemetry-instrumentation == 0.18.dev0
4545
opentelemetry-api == 0.18.dev0
4646

0 commit comments

Comments
 (0)