Skip to content

Commit de7ce9d

Browse files
committed
Only use generic variable when instrumentation-specific is not set
1 parent f0a3bce commit de7ce9d

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ def get_traced_request_attrs(instrumentation):
4949

5050

5151
def get_excluded_urls(instrumentation: str) -> ExcludeList:
52+
# Get instrumentation-specific excluded URLs. If not set, retrieve them
53+
# from generic variable.
5254
excluded_urls = environ.get(
53-
_root.format(f"{instrumentation}_EXCLUDED_URLS"), ""
55+
_root.format(f"{instrumentation}_EXCLUDED_URLS"),
56+
environ.get(_root.format("EXCLUDED_URLS"), ""),
5457
)
55-
if not excluded_urls:
56-
excluded_urls = environ.get(_root.format("EXCLUDED_URLS"), "")
5758

5859
return parse_excluded_urls(excluded_urls)
5960

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
from unittest.mock import patch
16+
17+
from opentelemetry.test.test_base import TestBase
18+
from opentelemetry.util.http import get_excluded_urls
19+
20+
21+
class TestGetExcludedUrls(TestBase):
22+
@patch.dict(
23+
"os.environ",
24+
{
25+
"OTEL_PYTHON_DJANGO_EXCLUDED_URLS": "excluded_arg/123,excluded_noarg"
26+
},
27+
)
28+
def test_config_from_instrumentation_env(self):
29+
exclude_list = get_excluded_urls("DJANGO")
30+
31+
self.assertTrue(exclude_list.url_disabled("/excluded_arg/123"))
32+
self.assertTrue(exclude_list.url_disabled("/excluded_noarg"))
33+
self.assertFalse(exclude_list.url_disabled("/excluded_arg/125"))
34+
35+
@patch.dict(
36+
"os.environ",
37+
{"OTEL_PYTHON_EXCLUDED_URLS": "excluded_arg/123,excluded_noarg"},
38+
)
39+
def test_config_from_generic_env(self):
40+
exclude_list = get_excluded_urls("DJANGO")
41+
42+
self.assertTrue(exclude_list.url_disabled("/excluded_arg/123"))
43+
self.assertTrue(exclude_list.url_disabled("/excluded_noarg"))
44+
self.assertFalse(exclude_list.url_disabled("/excluded_arg/125"))
45+
46+
@patch.dict(
47+
"os.environ",
48+
{
49+
"OTEL_PYTHON_DJANGO_EXCLUDED_URLS": "excluded_arg/123,excluded_noarg",
50+
"OTEL_PYTHON_EXCLUDED_URLS": "excluded_arg/125",
51+
},
52+
)
53+
def test_config_from_instrumentation_env_takes_precedence(self):
54+
exclude_list = get_excluded_urls("DJANGO")
55+
56+
self.assertTrue(exclude_list.url_disabled("/excluded_arg/123"))
57+
self.assertTrue(exclude_list.url_disabled("/excluded_noarg"))
58+
self.assertFalse(exclude_list.url_disabled("/excluded_arg/125"))
59+
60+
@patch.dict(
61+
"os.environ",
62+
{
63+
"OTEL_PYTHON_DJANGO_EXCLUDED_URLS": "",
64+
"OTEL_PYTHON_EXCLUDED_URLS": "excluded_arg/125",
65+
},
66+
)
67+
def test_config_from_instrumentation_env_empty(self):
68+
exclude_list = get_excluded_urls("DJANGO")
69+
70+
self.assertFalse(exclude_list.url_disabled("/excluded_arg/123"))
71+
self.assertFalse(exclude_list.url_disabled("/excluded_noarg"))
72+
self.assertFalse(exclude_list.url_disabled("/excluded_arg/125"))

0 commit comments

Comments
 (0)