Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check disabled tracing URLs based on regex #1106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions opencensus/trace/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions tests/unit/trace/test_ext_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down