From eef5dce0323c6bddd39d9eca0a49c4a41f5a4663 Mon Sep 17 00:00:00 2001 From: Jonas Miederer Date: Wed, 2 Mar 2022 14:32:17 +0100 Subject: [PATCH 1/3] check excluded tracing paths based on regex --- opencensus/trace/utils.py | 2 +- tests/unit/trace/test_ext_utils.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/opencensus/trace/utils.py b/opencensus/trace/utils.py index 5b9be3473..91503c849 100644 --- a/opencensus/trace/utils.py +++ b/opencensus/trace/utils.py @@ -64,7 +64,7 @@ def disable_tracing_url(url, excludelist_paths=None): url_path = url.split('/', 1)[1] for path in excludelist_paths: - if url_path.startswith(path): + if re.match(path, url_path): return True return False diff --git a/tests/unit/trace/test_ext_utils.py b/tests/unit/trace/test_ext_utils.py index e322158a7..372302463 100644 --- a/tests/unit/trace/test_ext_utils.py +++ b/tests/unit/trace/test_ext_utils.py @@ -58,6 +58,48 @@ def test_disable_tracing_url_explicit(self): disable_tracing = utils.disable_tracing_url(url, excludelist_paths) self.assertTrue(disable_tracing) + def test_disable_tracing_url_partial(self): + url = 'http://127.0.0.1:8080/test_no_tracing' + excludelist_paths = ['test'] + + disable_tracing = utils.disable_tracing_url(url, excludelist_paths) + self.assertTrue(disable_tracing) + + def test_disable_tracing_url_partial_regex_match(self): + url = 'http://127.0.0.1:8080/test_no_tracing' + excludelist_paths = ['^test'] + + disable_tracing = utils.disable_tracing_url(url, excludelist_paths) + self.assertTrue(disable_tracing) + + def test_disable_tracing_url_partial_regex_nomatch(self): + url = 'http://127.0.0.1:8080/test_no_tracing' + excludelist_paths = ['test$'] + + disable_tracing = utils.disable_tracing_url(url, excludelist_paths) + self.assertFalse(disable_tracing) + + def test_disable_tracing_url_root(self): + url = 'http://127.0.0.1:8080/' + excludelist_paths = [''] + + disable_tracing = utils.disable_tracing_url(url, excludelist_paths) + self.assertTrue(disable_tracing) + + def test_disable_tracing_url_root_regex(self): + url = 'http://127.0.0.1:8080/' + excludelist_paths = ['^$'] + + disable_tracing = utils.disable_tracing_url(url, excludelist_paths) + self.assertTrue(disable_tracing) + + def test_disable_tracing_url_root_empty_exclude(self): + url = 'http://127.0.0.1:8080/test_no_tracing' + excludelist_paths = [''] + + disable_tracing = utils.disable_tracing_url(url, excludelist_paths) + self.assertTrue(disable_tracing) + def test_disable_tracing_hostname_default(self): url = '127.0.0.1:8080' From 8988703a8d97d7e85cf59af4e07b13475cde000a Mon Sep 17 00:00:00 2001 From: Jonas Miederer Date: Wed, 2 Mar 2022 14:39:12 +0100 Subject: [PATCH 2/3] Updated method doc --- opencensus/trace/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opencensus/trace/utils.py b/opencensus/trace/utils.py index 91503c849..fb1be3282 100644 --- a/opencensus/trace/utils.py +++ b/opencensus/trace/utils.py @@ -44,9 +44,9 @@ def get_func_name(func): def disable_tracing_url(url, excludelist_paths=None): """Disable tracing on the provided excludelist paths, by default not tracing - the health check request. + the health check request. Paths can be provided as regex patterns. - If the url path starts with the excludelisted path, return True. + If the url path matches the excludelisted path, return True. :type excludelist_paths: list :param excludelist_paths: Paths that not tracing. From c80fb62106989d7281529e810299669f60a41575 Mon Sep 17 00:00:00 2001 From: Jonas Miederer Date: Wed, 2 Mar 2022 15:01:31 +0100 Subject: [PATCH 3/3] added wildcard test --- tests/unit/trace/test_ext_utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/unit/trace/test_ext_utils.py b/tests/unit/trace/test_ext_utils.py index 372302463..7b25ef2a8 100644 --- a/tests/unit/trace/test_ext_utils.py +++ b/tests/unit/trace/test_ext_utils.py @@ -100,6 +100,17 @@ def test_disable_tracing_url_root_empty_exclude(self): disable_tracing = utils.disable_tracing_url(url, excludelist_paths) self.assertTrue(disable_tracing) + def test_disable_tracing_url_wildcard(self): + excludelist_paths = [r'test/(\w+/)*tracing'] + + url = 'http://127.0.0.1:8080/test/no/tracing' + disable_tracing = utils.disable_tracing_url(url, excludelist_paths) + self.assertTrue(disable_tracing) + + url = 'http://127.0.0.1:8080/test/tracing' + disable_tracing = utils.disable_tracing_url(url, excludelist_paths) + self.assertTrue(disable_tracing) + def test_disable_tracing_hostname_default(self): url = '127.0.0.1:8080'