diff --git a/opencensus/trace/utils.py b/opencensus/trace/utils.py index 5b9be3473..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. @@ -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..7b25ef2a8 100644 --- a/tests/unit/trace/test_ext_utils.py +++ b/tests/unit/trace/test_ext_utils.py @@ -58,6 +58,59 @@ 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_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'