Sentry 查询基本逻辑

查询api的基本查询调用逻辑如下:

查询api()

=> prep_search() [执行查询]

=> build_query_params_from_request() : { projects, sort_by: sort, limit, cursor, search_filters: query} [构建查询参数]

=> parse_search_query() : search_filters [解析查询字符串]

=> search.query() :result [查询结果]

=> snuba_sdk.query.Query() : [执行具体查询]

search_filters是一个由 query 参数格式化来的查询器集合。一切未在接口定义中明确定义且符合sentry 查询格式的参数均可以通过这个查询器的校验,并执行查询。

根据时间范围查询

parse_search_query方法根据语法解析查询字符串,判断查询条件是否正确,关于时间的查询器的定义如下:

1
2
3
4
5
6
7
8
9
# filter for dates
date_filter = search_key sep operator iso_8601_date_format

key = ~r"[a-zA-Z0-9_.-]+"
quoted_key = '"' ~r"[a-zA-Z0-9_.:-]+" '"'
search_key = key / quoted_key
iso_8601_date_format = date_format time_format? ("Z" / tz_format)? &end_value
sep = ":"
operator = ">=" / "<=" / ">" / "<" / "=" / "!="

search.query方法会将search_filters中的date和另两个参数date_from、date_to比较,得到最终的查询时间范围。

1
2
3
end_params = [_f for _f in [date_to, get_search_filter(search_filters, "date", "<")] if _f]
# other code
start_params = [date_from, retention_date, get_search_filter(search_filters, "date", ">")]

其中 get_search_filter 方法根据查询参数名和操作符获取查询参数值,如果找到多个值,则返回最严格的值。定义如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def get_search_filter(search_filters: Sequence[SearchFilter], name: str, operator: str) -> Any:
"""
Finds the value of a search filter with the passed name and operator. If
multiple values are found, returns the most restrictive value
:param search_filters: collection of `SearchFilter` objects
:param name: Name of the field to find
:param operator: '<', '>' or '='
:return: The value of the field if found, else None
"""
if not search_filters:
return None
assert operator in ("<", ">", "=", "IN")
comparator = max if operator.startswith(">") else min
found_val = None
for search_filter in search_filters:
# Note that we check operator with `startswith` here so that we handle
# <, <=, >, >=
if search_filter.key.name == name and search_filter.operator.startswith(operator):
val = search_filter.value.raw_value
found_val = comparator(val, found_val) if found_val else val
return found_val

由上得知,

结论:search_filters中的date形如:date:>2021-12-20T16:00:00

分页查询

参考 sentry api学习笔记

如果这个项目的event不止100条, sentry还会返回下一页的url, 要获得下一页events的话需要从返回结果的响应头 response.headers.Link 中获取下一页请求地址,其中的cursor指定了下一页的查询
response.headers.X-Hits 表示总数