Skip to content

Commit

Permalink
fix: use regex search op to filter http requests (#634)
Browse files Browse the repository at this point in the history
* fix: use regex search op to filter http requests
* test: search regex with anything at start and end should still match
  • Loading branch information
sagivoululumigo authored Jan 11, 2024
1 parent 148c084 commit 60b3d94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/lumigo_opentelemetry/libs/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def does_endpoint_match_env_var_filtering_regex(
if not regexes:
return False

return any(does_match_regex_safe(regex, endpoint) for regex in regexes)
return any(does_search_regex_find_safe(regex, endpoint) for regex in regexes)


def does_endpoint_match_client_filtering_regexes(endpoint: str) -> bool:
Expand All @@ -124,12 +124,12 @@ def does_endpoint_match_filtering_regexes(endpoint: str) -> bool:
)


def does_match_regex_safe(regex: str, value: str) -> bool:
def does_search_regex_find_safe(regex: str, value: str) -> bool:
if not regex or value is None:
return False

try:
return re.match(regex, value) is not None
return re.search(regex, value) is not None
except Exception:
logger.warning(f"Invalid regex: {regex}")
return False
Expand Down
15 changes: 12 additions & 3 deletions src/test/unit/libs/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lumigo_opentelemetry.libs.sampling import (
_extract_endpoint,
_get_string_list_from_env_var,
does_match_regex_safe,
does_search_regex_find_safe,
does_endpoint_match_filtering_regexes,
AttributeSampler,
)
Expand Down Expand Up @@ -85,6 +85,15 @@ def test_does_endpoint_match_filtering_regexes(
("a", "", False),
("a", "a", True),
("a", "b", False),
# Make sure that the regex is searched and not matched
("word", "search for one word in this sentence", True),
(".*word.*", "search for one word in this sentence", True),
(r"^word$", "search for one word in this sentence", False),
(
r"^search for one word in this sentence$",
"search for one word in this sentence",
True,
),
# This is an invalid regex, but we want to make sure it doesn't crash
("[", "", False),
("[", "[", False),
Expand All @@ -94,8 +103,8 @@ def test_does_endpoint_match_filtering_regexes(
("", None, False),
],
)
def test_does_match_regex_safe(regex, value, should_match):
assert does_match_regex_safe(regex, value) == should_match
def test_does_search_regex_find_safe(regex, value, should_match):
assert does_search_regex_find_safe(regex, value) == should_match


@pytest.mark.parametrize(
Expand Down

0 comments on commit 60b3d94

Please sign in to comment.