Skip to content

Commit 40dd073

Browse files
cnnradamsAlex Boten
authored and
Alex Boten
committed
Change exclude lists to just exclude_urls, add tests for flask and django (#872)
1 parent 9c5e520 commit 40dd073

File tree

15 files changed

+188
-99
lines changed

15 files changed

+188
-99
lines changed

Diff for: ext/opentelemetry-ext-django/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Use one general exclude list instead of two ([#872](https://github.com/open-telemetry/opentelemetry-python/pull/872))
6+
57
## 0.8b0
68

79
Released 2020-05-27

Diff for: ext/opentelemetry-ext-django/README.rst

+8-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ Configuration
2020

2121
Exclude lists
2222
*************
23-
Excludes certain hosts and paths from being tracked. Pass in comma delimited string into environment variables.
24-
Host refers to the entire url and path refers to the part of the url after the domain. Host matches the exact string that is given, where as path matches if the url starts with the given excluded path.
23+
To exclude certain URLs from being tracked, set the environment variable ``OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
2524

26-
Excluded hosts: OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_HOSTS
27-
Excluded paths: OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_PATHS
25+
For example,
26+
27+
::
28+
29+
export OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_URLS="client/.*/info,healthcheck"
30+
31+
will exclude requests such as ``https://site/client/123/info`` and ``https://site/xyz/healthcheck``.
2832

2933
References
3034
----------

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

+9-22
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
)
2525
from opentelemetry.propagators import extract
2626
from opentelemetry.trace import SpanKind, get_tracer
27-
from opentelemetry.util import disable_trace
27+
from opentelemetry.util import ExcludeList
2828

2929
try:
3030
from django.utils.deprecation import MiddlewareMixin
@@ -44,12 +44,11 @@ class _DjangoMiddleware(MiddlewareMixin):
4444
_environ_token = "opentelemetry-instrumentor-django.token"
4545
_environ_span_key = "opentelemetry-instrumentor-django.span_key"
4646

47-
_excluded_hosts = Configuration().DJANGO_EXCLUDED_HOSTS or []
48-
_excluded_paths = Configuration().DJANGO_EXCLUDED_PATHS or []
49-
if _excluded_hosts:
50-
_excluded_hosts = str.split(_excluded_hosts, ",")
51-
if _excluded_paths:
52-
_excluded_paths = str.split(_excluded_paths, ",")
47+
_excluded_urls = Configuration().DJANGO_EXCLUDED_URLS or []
48+
if _excluded_urls:
49+
_excluded_urls = ExcludeList(str.split(_excluded_urls, ","))
50+
else:
51+
_excluded_urls = ExcludeList(_excluded_urls)
5352

5453
def process_view(
5554
self, request, view_func, view_args, view_kwargs
@@ -62,11 +61,7 @@ def process_view(
6261
# key.lower().replace('_', '-').replace("http-", "", 1): value
6362
# for key, value in request.META.items()
6463
# }
65-
if disable_trace(
66-
request.build_absolute_uri("?"),
67-
self._excluded_hosts,
68-
self._excluded_paths,
69-
):
64+
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
7065
return
7166

7267
environ = request.META
@@ -97,11 +92,7 @@ def process_exception(self, request, exception):
9792
# Django can call this method and process_response later. In order
9893
# to avoid __exit__ and detach from being called twice then, the
9994
# respective keys are being removed here.
100-
if disable_trace(
101-
request.build_absolute_uri("?"),
102-
self._excluded_hosts,
103-
self._excluded_paths,
104-
):
95+
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
10596
return
10697

10798
if self._environ_activation_key in request.META.keys():
@@ -116,11 +107,7 @@ def process_exception(self, request, exception):
116107
request.META.pop(self._environ_token, None)
117108

118109
def process_response(self, request, response):
119-
if disable_trace(
120-
request.build_absolute_uri("?"),
121-
self._excluded_hosts,
122-
self._excluded_paths,
123-
):
110+
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
124111
return response
125112

126113
if (

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

+28-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from sys import modules
16+
from unittest.mock import patch
1617

1718
from django.conf import settings
1819
from django.conf.urls import url
@@ -24,15 +25,17 @@
2425
from opentelemetry.test.wsgitestutil import WsgiTestBase
2526
from opentelemetry.trace import SpanKind
2627
from opentelemetry.trace.status import StatusCanonicalCode
28+
from opentelemetry.util import ExcludeList
2729

2830
# pylint: disable=import-error
29-
from .views import error, excluded, excluded2, traced
31+
from .views import error, excluded, excluded_noarg, excluded_noarg2, traced
3032

3133
urlpatterns = [
3234
url(r"^traced/", traced),
3335
url(r"^error/", error),
34-
url(r"^excluded/", excluded),
35-
url(r"^excluded2/", excluded2),
36+
url(r"^excluded_arg/", excluded),
37+
url(r"^excluded_noarg/", excluded_noarg),
38+
url(r"^excluded_noarg2/", excluded_noarg2),
3639
]
3740
_django_instrumentor = DjangoInstrumentor()
3841

@@ -111,3 +114,25 @@ def test_error(self):
111114
span.attributes["http.url"], "http://testserver/error/"
112115
)
113116
self.assertEqual(span.attributes["http.scheme"], "http")
117+
118+
@patch(
119+
"opentelemetry.ext.django.middleware._DjangoMiddleware._excluded_urls",
120+
ExcludeList(["http://testserver/excluded_arg/123", "excluded_noarg"]),
121+
)
122+
def test_exclude_lists(self):
123+
client = Client()
124+
client.get("/excluded_arg/123")
125+
span_list = self.memory_exporter.get_finished_spans()
126+
self.assertEqual(len(span_list), 0)
127+
128+
client.get("/excluded_arg/125")
129+
span_list = self.memory_exporter.get_finished_spans()
130+
self.assertEqual(len(span_list), 1)
131+
132+
client.get("/excluded_noarg/")
133+
span_list = self.memory_exporter.get_finished_spans()
134+
self.assertEqual(len(span_list), 1)
135+
136+
client.get("/excluded_noarg2/")
137+
span_list = self.memory_exporter.get_finished_spans()
138+
self.assertEqual(len(span_list), 1)

Diff for: ext/opentelemetry-ext-django/tests/views.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ def excluded(request): # pylint: disable=unused-argument
1313
return HttpResponse()
1414

1515

16-
def excluded2(request): # pylint: disable=unused-argument
16+
def excluded_noarg(request): # pylint: disable=unused-argument
17+
return HttpResponse()
18+
19+
20+
def excluded_noarg2(request): # pylint: disable=unused-argument
1721
return HttpResponse()

Diff for: ext/opentelemetry-ext-flask/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Use one general exclude list instead of two ([#872](https://github.com/open-telemetry/opentelemetry-python/pull/872))
6+
57
## 0.7b1
68

79
Released 2020-05-12

Diff for: ext/opentelemetry-ext-flask/README.rst

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ Configuration
2121

2222
Exclude lists
2323
*************
24-
Excludes certain hosts and paths from being tracked. Pass in comma delimited string into environment variables.
25-
Host refers to the entire url and path refers to the part of the url after the domain. Host matches the exact string that is given, where as path matches if the url starts with the given excluded path.
24+
To exclude certain URLs from being tracked, set the environment variable ``OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
2625

27-
Excluded hosts: OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_HOSTS
28-
Excluded paths: OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_PATHS
26+
For example,
2927

28+
::
29+
30+
export OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_URLS="client/.*/info,healthcheck"
31+
32+
will exclude requests such as ``https://site/client/123/info`` and ``https://site/xyz/healthcheck``.
3033

3134
References
3235
----------

Diff for: ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def hello():
5555
from opentelemetry import configuration, context, propagators, trace
5656
from opentelemetry.ext.flask.version import __version__
5757
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
58-
from opentelemetry.util import disable_trace, time_ns
58+
from opentelemetry.util import ExcludeList, time_ns
5959

6060
_logger = getLogger(__name__)
6161

@@ -65,22 +65,14 @@ def hello():
6565
_ENVIRON_TOKEN = "opentelemetry-flask.token"
6666

6767

68-
def get_excluded_hosts():
69-
hosts = configuration.Configuration().FLASK_EXCLUDED_HOSTS or []
70-
if hosts:
71-
hosts = str.split(hosts, ",")
72-
return hosts
68+
def get_excluded_urls():
69+
urls = configuration.Configuration().FLASK_EXCLUDED_URLS or []
70+
if urls:
71+
urls = str.split(urls, ",")
72+
return ExcludeList(urls)
7373

7474

75-
def get_excluded_paths():
76-
paths = configuration.Configuration().FLASK_EXCLUDED_PATHS or []
77-
if paths:
78-
paths = str.split(paths, ",")
79-
return paths
80-
81-
82-
_excluded_hosts = get_excluded_hosts()
83-
_excluded_paths = get_excluded_paths()
75+
_excluded_urls = get_excluded_urls()
8476

8577

8678
def _rewrapped_app(wsgi_app):
@@ -92,9 +84,7 @@ def _wrapped_app(environ, start_response):
9284
environ[_ENVIRON_STARTTIME_KEY] = time_ns()
9385

9486
def _start_response(status, response_headers, *args, **kwargs):
95-
if not disable_trace(
96-
flask.request.url, _excluded_hosts, _excluded_paths
97-
):
87+
if not _excluded_urls.url_disabled(flask.request.url):
9888
span = flask.request.environ.get(_ENVIRON_SPAN_KEY)
9989

10090
if span:
@@ -116,7 +106,7 @@ def _start_response(status, response_headers, *args, **kwargs):
116106

117107

118108
def _before_request():
119-
if disable_trace(flask.request.url, _excluded_hosts, _excluded_paths):
109+
if _excluded_urls.url_disabled(flask.request.url):
120110
return
121111

122112
environ = flask.request.environ
@@ -148,7 +138,7 @@ def _before_request():
148138

149139

150140
def _teardown_request(exc):
151-
if disable_trace(flask.request.url, _excluded_hosts, _excluded_paths):
141+
if _excluded_urls.url_disabled(flask.request.url):
152142
return
153143

154144
activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY)

Diff for: ext/opentelemetry-ext-flask/tests/test_programmatic.py

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

15+
from unittest.mock import patch
16+
1517
from flask import Flask, request
1618

1719
from opentelemetry import trace
1820
from opentelemetry.ext.flask import FlaskInstrumentor
1921
from opentelemetry.test.test_base import TestBase
2022
from opentelemetry.test.wsgitestutil import WsgiTestBase
23+
from opentelemetry.util import ExcludeList
2124

2225
# pylint: disable=import-error
2326
from .base_test import InstrumentationTest
@@ -139,3 +142,24 @@ def test_internal_error(self):
139142
self.assertEqual(span_list[0].name, "_hello_endpoint")
140143
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
141144
self.assertEqual(span_list[0].attributes, expected_attrs)
145+
146+
@patch(
147+
"opentelemetry.ext.flask._excluded_urls",
148+
ExcludeList(["http://localhost/excluded_arg/123", "excluded_noarg"]),
149+
)
150+
def test_exclude_lists(self):
151+
self.client.get("/excluded_arg/123")
152+
span_list = self.memory_exporter.get_finished_spans()
153+
self.assertEqual(len(span_list), 0)
154+
155+
self.client.get("/excluded_arg/125")
156+
span_list = self.memory_exporter.get_finished_spans()
157+
self.assertEqual(len(span_list), 1)
158+
159+
self.client.get("/excluded_noarg")
160+
span_list = self.memory_exporter.get_finished_spans()
161+
self.assertEqual(len(span_list), 1)
162+
163+
self.client.get("/excluded_noarg2")
164+
span_list = self.memory_exporter.get_finished_spans()
165+
self.assertEqual(len(span_list), 1)

Diff for: ext/opentelemetry-ext-pyramid/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Use one general exclude list instead of two ([#872](https://github.com/open-telemetry/opentelemetry-python/pull/872))
6+
57
## 0.9b0
68

79
Released 2020-06-10

Diff for: ext/opentelemetry-ext-pyramid/README.rst

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ Installation
1313

1414
pip install opentelemetry-ext-pyramid
1515

16+
Exclude lists
17+
*************
18+
To exclude certain URLs from being tracked, set the environment variable ``OPENTELEMETRY_PYTHON_PYRAMID_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude.
19+
20+
For example,
21+
22+
::
23+
24+
export OPENTELEMETRY_PYTHON_PYRAMID_EXCLUDED_URLS="client/.*/info,healthcheck"
25+
26+
will exclude requests such as ``https://site/client/123/info`` and ``https://site/xyz/healthcheck``.
1627

1728
References
1829
----------

Diff for: ext/opentelemetry-ext-pyramid/src/opentelemetry/ext/pyramid/callbacks.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import opentelemetry.ext.wsgi as otel_wsgi
99
from opentelemetry import configuration, context, propagators, trace
1010
from opentelemetry.ext.pyramid.version import __version__
11-
from opentelemetry.util import disable_trace, time_ns
11+
from opentelemetry.util import ExcludeList, time_ns
1212

1313
TWEEN_NAME = "opentelemetry.ext.pyramid.trace_tween_factory"
1414
SETTING_TRACE_ENABLED = "opentelemetry-pyramid.trace_enabled"
@@ -22,22 +22,14 @@
2222
_logger = getLogger(__name__)
2323

2424

25-
def get_excluded_hosts():
26-
hosts = configuration.Configuration().PYRAMID_EXCLUDED_HOSTS or []
27-
if hosts:
28-
hosts = str.split(hosts, ",")
29-
return hosts
25+
def get_excluded_urls():
26+
urls = configuration.Configuration().PYRAMID_EXCLUDED_URLS or []
27+
if urls:
28+
urls = str.split(urls, ",")
29+
return ExcludeList(urls)
3030

3131

32-
def get_excluded_paths():
33-
paths = configuration.Configuration().PYRAMID_EXCLUDED_PATHS or []
34-
if paths:
35-
paths = str.split(paths, ",")
36-
return paths
37-
38-
39-
_excluded_hosts = get_excluded_hosts()
40-
_excluded_paths = get_excluded_paths()
32+
_excluded_urls = get_excluded_urls()
4133

4234

4335
def includeme(config):
@@ -119,7 +111,7 @@ def disabled_tween(request):
119111

120112
# make a request tracing function
121113
def trace_tween(request):
122-
if disable_trace(request.url, _excluded_hosts, _excluded_paths):
114+
if _excluded_urls.url_disabled(request.url):
123115
request.environ[_ENVIRON_ENABLED_KEY] = False
124116
# short-circuit when we don't want to trace anything
125117
return handler(request)

Diff for: ext/opentelemetry-ext-pyramid/tests/test_programmatic.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from opentelemetry.ext.pyramid import PyramidInstrumentor
2121
from opentelemetry.test.test_base import TestBase
2222
from opentelemetry.test.wsgitestutil import WsgiTestBase
23+
from opentelemetry.util import ExcludeList
2324

2425
# pylint: disable=import-error
2526
from .pyramid_base_test import InstrumentationTest
@@ -169,12 +170,8 @@ def test_warnings(self, mock_logger):
169170
self.assertEqual(mock_logger.warning.called, True)
170171

171172
@patch(
172-
"opentelemetry.ext.pyramid.callbacks._excluded_hosts",
173-
["http://localhost/excluded_arg/123"],
174-
)
175-
@patch(
176-
"opentelemetry.ext.pyramid.callbacks._excluded_paths",
177-
["excluded_noarg"],
173+
"opentelemetry.ext.pyramid.callbacks._excluded_urls",
174+
ExcludeList(["http://localhost/excluded_arg/123", "excluded_noarg"]),
178175
)
179176
def test_exclude_lists(self):
180177
self.client.get("/excluded_arg/123")

0 commit comments

Comments
 (0)