Skip to content
Merged
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
130 changes: 105 additions & 25 deletions src/sentry/api/event_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
aggregate_rel_date_filter = negation? aggregate_key sep operator? rel_date_format

# has filter for not null type checks
has_filter = negation? &"has:" search_key sep (search_key / search_value)
has_filter = negation? &"has:" search_key sep (text_key / search_value)

# is filter. Specific to issue search
is_filter = negation? &"is:" search_key sep search_value
Expand All @@ -135,13 +135,25 @@
text_filter = negation? text_key sep operator? search_value

key = ~r"[a-zA-Z0-9_.-]+"
quoted_key = '"' ~r"[a-zA-Z0-9_.:-]+" '"'
explicit_flag_key = "flags" open_bracket search_key closed_bracket
explicit_string_flag_key = "flags" open_bracket search_key spaces comma spaces "string" closed_bracket
explicit_number_flag_key = "flags" open_bracket search_key spaces comma spaces "number" closed_bracket
explicit_tag_key = "tags" open_bracket search_key closed_bracket
explicit_string_tag_key = "tags" open_bracket search_key spaces comma spaces "string" closed_bracket
explicit_number_tag_key = "tags" open_bracket search_key spaces comma spaces "number" closed_bracket
escaped_key = ~r"[a-zA-Z0-9_.:-]+"
quoted_key = '"' escaped_key '"'

# the quoted variant is here to for backwards compatibility,
# and can be removed once we're sure it's no longer in use
explicit_flag_key_unquoted = "flags" open_bracket escaped_key closed_bracket
explicit_flag_key_quoted = "flags" open_bracket quoted_key closed_bracket
explicit_flag_key = explicit_flag_key_unquoted / explicit_flag_key_quoted
explicit_string_flag_key_unquoted = "flags" open_bracket escaped_key spaces comma spaces "string" closed_bracket
explicit_string_flag_key_quoted = "flags" open_bracket quoted_key spaces comma spaces "string" closed_bracket
explicit_string_flag_key = explicit_string_flag_key_unquoted / explicit_string_flag_key_quoted
explicit_number_flag_key_unquoted = "flags" open_bracket escaped_key spaces comma spaces "number" closed_bracket
explicit_number_flag_key_quoted = "flags" open_bracket quoted_key spaces comma spaces "number" closed_bracket
explicit_number_flag_key = explicit_number_flag_key_unquoted / explicit_number_flag_key_quoted

explicit_tag_key = "tags" open_bracket escaped_key closed_bracket
explicit_string_tag_key = "tags" open_bracket escaped_key spaces comma spaces "string" closed_bracket
explicit_number_tag_key = "tags" open_bracket escaped_key spaces comma spaces "number" closed_bracket

aggregate_key = key open_paren spaces function_args? spaces closed_paren
function_args = aggregate_param (spaces comma spaces !comma aggregate_param?)*
aggregate_param = quoted_aggregate_param / raw_aggregate_param
Expand Down Expand Up @@ -1344,96 +1356,164 @@ def visit_text_filter(
def visit_key(self, node: Node, children: object) -> str:
return node.text

def visit_quoted_key(self, node: Node, children: tuple[Node, Node, Node]) -> str:
return children[1].text
def visit_escaped_key(self, node: Node, children: object) -> str:
return node.text

def visit_quoted_key(self, node: Node, children: tuple[Node, str, Node]) -> str:
return children[1]

def visit_explicit_tag_key(
self,
node: Node,
children: tuple[
Node, # "tags"
str, # '['
SearchKey,
str, # escaped_key
str, # ']'
],
) -> SearchKey:
return SearchKey(f"tags[{children[2].name}]")
return SearchKey(f"tags[{children[2]}]")

def visit_explicit_string_tag_key(
self,
node: Node,
children: tuple[
Node, # "tags"
str, # '['
SearchKey,
str, # escaped_key
str, # ' '
Node, # ','
str, # ' '
Node, # "string"
str, # ']'
],
) -> SearchKey:
return SearchKey(f"tags[{children[2].name},string]")
return SearchKey(f"tags[{children[2]},string]")

def visit_explicit_number_tag_key(
self,
node: Node,
children: tuple[
Node, # "tags"
str, # '['
SearchKey,
str, # escaped_key
str, # ' '
Node, # ','
str, # ' '
Node, # "number"
str, # ']'
],
) -> SearchKey:
return SearchKey(f"tags[{children[2].name},number]")
return SearchKey(f"tags[{children[2]},number]")

def visit_explicit_flag_key(
def visit_explicit_flag_key_unquoted(
self,
node: Node,
children: tuple[
Node, # "flags"
str, # [
SearchKey,
str, # escaped_key
str, # ]
],
) -> SearchKey:
return SearchKey(f"flags[{children[2].name}]")
return SearchKey(f"flags[{children[2]}]")

def visit_explicit_string_flag_key(
def visit_explicit_flag_key_quoted(
self,
node: Node,
children: tuple[
Node, # "flags"
str, # [
str, # escaped_key
str, # ]
],
) -> SearchKey:
return SearchKey(f"flags[{children[2]}]")

def visit_explicit_flag_key(
self,
node: Node,
children: tuple[SearchKey],
) -> SearchKey:
return children[0]

def visit_explicit_string_flag_key_unquoted(
self,
node: Node,
children: tuple[
Node, # "flags"
str, # '['
SearchKey,
str, # escaped_key
str, # ' '
Node, # ','
str, # ' '
Node, # "string"
str, # ']'
],
) -> SearchKey:
return SearchKey(f"flags[{children[2].name},string]")
return SearchKey(f"flags[{children[2]},string]")

def visit_explicit_number_flag_key(
def visit_explicit_string_flag_key_quoted(
self,
node: Node,
children: tuple[
Node, # "flags"
str, # '['
SearchKey,
str, # escaped_key
str, # ' '
Node, # ','
str, # ' '
Node, # "string"
str, # ']'
],
) -> SearchKey:
return SearchKey(f"flags[{children[2]},string]")

def visit_explicit_string_flag_key(
self,
node: Node,
children: tuple[SearchKey],
) -> SearchKey:
return children[0]

def visit_explicit_number_flag_key_unquoted(
self,
node: Node,
children: tuple[
Node, # "flags"
str, # '['
str, # escaped_key
str, # ' '
Node, # ','
str, # ' '
Node, # "number"
str, # ']'
],
) -> SearchKey:
return SearchKey(f"flags[{children[2]},number]")

def visit_explicit_number_flag_key_quoted(
self,
node: Node,
children: tuple[
Node, # "flags"
str, # '['
str, # escaped_key
str, # ' '
Node, # ','
str, # ' '
Node, # "number"
str, # ']'
],
) -> SearchKey:
return SearchKey(f"flags[{children[2].name},number]")
return SearchKey(f"flags[{children[2]},number]")

def visit_explicit_number_flag_key(
self,
node: Node,
children: tuple[SearchKey],
) -> SearchKey:
return children[0]

def visit_aggregate_key(
self,
Expand Down
52 changes: 39 additions & 13 deletions static/app/components/searchSyntax/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -220,42 +220,68 @@ text_filter

// Filter keys
key
= value:[a-zA-Z0-9_.-]+ {
return tc.tokenKeySimple(value.join(''), false);
= key:[a-zA-Z0-9_.-]+ {
return tc.tokenKeySimple(key.join(''), false);
}

escaped_key
= key:[a-zA-Z0-9_.:-]+ {
return tc.tokenKeySimple(key.join(''), false);
}

quoted_key
= '"' key:[a-zA-Z0-9_.:-]+ '"' {
return tc.tokenKeySimple(key.join(''), true);
= '"' key:escaped_key '"' {
return tc.tokenKeySimple(key.value, true);
}

explicit_flag_key
= prefix:"flags" open_bracket key:search_key closed_bracket {
explicit_flag_key_unquoted
= prefix:"flags" open_bracket key:escaped_key closed_bracket {
return tc.tokenKeyExplicitFlag(prefix, key);
}

explicit_string_flag_key
= prefix:"flags" open_bracket key:search_key spaces comma spaces 'string' closed_bracket {
explicit_flag_key_quoted
= prefix:"flags" open_bracket key:quoted_key closed_bracket {
return tc.tokenKeyExplicitFlag(prefix, key);
}

explicit_flag_key = explicit_flag_key_unquoted / explicit_flag_key_quoted

explicit_string_flag_key_unquoted
= prefix:"flags" open_bracket key:escaped_key spaces comma spaces 'string' closed_bracket {
return tc.tokenKeyExplicitStringFlag(prefix, key)
}

explicit_string_flag_key_quoted
= prefix:"flags" open_bracket key:quoted_key spaces comma spaces 'string' closed_bracket {
return tc.tokenKeyExplicitStringFlag(prefix, key)
}

explicit_number_flag_key
= prefix:"flags" open_bracket key:search_key spaces comma spaces 'number' closed_bracket {
explicit_string_flag_key = explicit_string_flag_key_unquoted / explicit_string_flag_key_quoted

explicit_number_flag_key_unquoted
= prefix:"flags" open_bracket key:escaped_key spaces comma spaces 'number' closed_bracket {
return tc.tokenKeyExplicitNumberFlag(prefix, key)
}

explicit_number_flag_key_quoted
= prefix:"flags" open_bracket key:quoted_key spaces comma spaces 'number' closed_bracket {
return tc.tokenKeyExplicitNumberFlag(prefix, key)
}

explicit_number_flag_key = explicit_number_flag_key_unquoted / explicit_number_flag_key_quoted

explicit_tag_key
= prefix:"tags" open_bracket key:search_key closed_bracket {
= prefix:"tags" open_bracket key:escaped_key closed_bracket {
return tc.tokenKeyExplicitTag(prefix, key);
}

explicit_string_tag_key
= prefix:"tags" open_bracket key:search_key spaces comma spaces 'string' closed_bracket {
= prefix:"tags" open_bracket key:escaped_key spaces comma spaces 'string' closed_bracket {
return tc.tokenKeyExplicitStringTag(prefix, key)
}

explicit_number_tag_key
= prefix:"tags" open_bracket key:search_key spaces comma spaces 'number' closed_bracket {
= prefix:"tags" open_bracket key:escaped_key spaces comma spaces 'number' closed_bracket {
return tc.tokenKeyExplicitNumberTag(prefix, key)
}

Expand Down
50 changes: 50 additions & 0 deletions tests/sentry/api/test_event_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,3 +1055,53 @@ def test_translate_wildcard_as_clickhouse_pattern(pattern, clickhouse):
def test_invalid_translate_wildcard_as_clickhouse_pattern(pattern):
with pytest.raises(InvalidSearchQuery):
assert translate_wildcard_as_clickhouse_pattern(pattern)


@pytest.mark.parametrize(
["query", "key", "value"],
[
pytest.param("tags[foo/bar]:baz", "message", "tags[foo/bar]:baz"),
pytest.param("flags[foo/bar]:baz", "message", "flags[foo/bar]:baz"),
pytest.param("tags[foo]:true", "tags[foo]", "true"),
pytest.param("tags[foo,string]:true", "tags[foo,string]", "true"),
pytest.param("tags[foo:bar,string]:true", "tags[foo:bar,string]", "true"),
pytest.param("tags[foo,number]:0", "tags[foo,number]", "0"),
pytest.param("tags[foo:bar,number]:0", "tags[foo:bar,number]", "0"),
pytest.param("flags[foo]:true", "flags[foo]", "true"),
pytest.param("flags[foo,string]:true", "flags[foo,string]", "true"),
pytest.param("flags[foo:bar,string]:true", "flags[foo:bar,string]", "true"),
pytest.param("flags[foo,number]:0", "flags[foo,number]", "0"),
pytest.param("flags[foo:bar,number]:0", "flags[foo:bar,number]", "0"),
# the quoted versions are supported for flags for backwards compatibility
pytest.param('flags["foo"]:true', "flags[foo]", "true"),
pytest.param('flags["foo",string]:true', "flags[foo,string]", "true"),
pytest.param('flags["foo:bar",string]:true', "flags[foo:bar,string]", "true"),
pytest.param('flags["foo",number]:0', "flags[foo,number]", "0"),
pytest.param('flags["foo:bar",number]:0', "flags[foo:bar,number]", "0"),
],
)
def test_handles_special_character_in_tags_and_flags(query, key, value):
parsed = parse_search_query(query)
assert parsed == [SearchFilter(SearchKey(key), "=", SearchValue(value))]


@pytest.mark.parametrize(
["query", "key"],
[
pytest.param("has:tags[foo]", "tags[foo]"),
pytest.param("has:tags[foo,string]", "tags[foo,string]"),
pytest.param("has:tags[foo,number]", "tags[foo,number]"),
pytest.param("has:tags[foo:bar]", "tags[foo:bar]"),
pytest.param("has:tags[foo:bar,string]", "tags[foo:bar,string]"),
pytest.param("has:tags[foo:bar,number]", "tags[foo:bar,number]"),
pytest.param("has:flags[foo]", "flags[foo]"),
pytest.param("has:flags[foo,string]", "flags[foo,string]"),
pytest.param("has:flags[foo,number]", "flags[foo,number]"),
pytest.param("has:flags[foo:bar]", "flags[foo:bar]"),
pytest.param("has:flags[foo:bar,string]", "flags[foo:bar,string]"),
pytest.param("has:flags[foo:bar,number]", "flags[foo:bar,number]"),
],
)
def test_handles_has_tags_and_flags(query, key):
parsed = parse_search_query(query)
assert parsed == [SearchFilter(SearchKey(key), "!=", SearchValue(""))]