-
Notifications
You must be signed in to change notification settings - Fork 827
feat(rest-api): add type-aware attribute filter to GET /v1/spans #12524
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
c8cd236
5044594
478d92d
b350fb7
c3e9fc0
30c0be6
363a17b
14b21fc
c555997
0b53342
ba0603e
fa80fff
64e3c53
031473f
5524941
1852e08
fcd72bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,37 @@ | |
|
|
||
| DEFAULT_SPAN_LIMIT = 1000 | ||
|
|
||
|
|
||
| def _parse_attribute_filter(filter_str: str) -> sa.ColumnElement[bool]: | ||
| """Parse an ``attribute_filter`` query-param value into a SQLAlchemy | ||
|
anticorrelator marked this conversation as resolved.
Outdated
|
||
| filter clause. | ||
|
|
||
| The expected format is ``key:value`` where *key* is a dot-separated | ||
| attribute path (e.g. ``llm.model_name``) and *value* is the exact string | ||
| to match. The split is performed on the **first** ``:`` only, so values | ||
| may contain additional colons. | ||
|
|
||
| Returns ``models.Span.attributes[key_parts].as_string() == value``. | ||
|
|
||
| Raises :class:`HTTPException` (422) when the separator is missing or | ||
| the key portion is empty. | ||
| """ | ||
| if ":" not in filter_str: | ||
| raise HTTPException( | ||
| status_code=422, | ||
| detail=f"Invalid attribute_filter '{filter_str}': expected format 'key:value'", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to think about URI encoding a bit here I think |
||
| ) | ||
| key, value = filter_str.split(":", 1) | ||
| if not key: | ||
| raise HTTPException( | ||
| status_code=422, | ||
| detail="Invalid attribute_filter: key must not be empty", | ||
| ) | ||
| key_parts = key.split(".") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should think though how complex the first part should be. Can you come up with some reasonably complicated things you might want to filter for here |
||
| clause: sa.ColumnElement[bool] = models.Span.attributes[key_parts].as_string() == value | ||
| return clause | ||
|
|
||
|
|
||
| router = APIRouter(tags=["spans"]) | ||
|
|
||
|
|
||
|
|
@@ -624,6 +655,13 @@ async def span_search_otlpv1( | |
| default=None, | ||
| description="Filter by status code(s). Values: OK, ERROR, UNSET", | ||
| ), | ||
| attribute_filter: Optional[list[str]] = Query( | ||
| default=None, | ||
| description=( | ||
| "Filter by attribute key:value pairs (dot-separated keys, " | ||
| "e.g. llm.model_name:gpt-4). Multiple filters are ANDed." | ||
| ), | ||
| ), | ||
| ) -> OtlpSpansResponseBody: | ||
| """Search spans with minimal filters instead of the old SpanQuery DSL.""" | ||
|
|
||
|
|
@@ -664,6 +702,9 @@ async def span_search_otlpv1( | |
| ) | ||
| ) | ||
| ) | ||
| if attribute_filter: | ||
| for af in attribute_filter: | ||
| stmt = stmt.where(_parse_attribute_filter(af)) | ||
|
|
||
| if cursor: | ||
| try: | ||
|
|
@@ -801,6 +842,13 @@ async def span_search( | |
| default=None, | ||
| description="Filter by status code(s). Values: OK, ERROR, UNSET", | ||
| ), | ||
| attribute_filter: Optional[list[str]] = Query( | ||
| default=None, | ||
| description=( | ||
| "Filter by attribute key:value pairs (dot-separated keys, " | ||
| "e.g. llm.model_name:gpt-4). Multiple filters are ANDed." | ||
| ), | ||
| ), | ||
| ) -> SpansResponseBody: | ||
| async with request.app.state.db() as session: | ||
| project = await get_project_by_identifier(session, project_identifier) | ||
|
|
@@ -847,6 +895,9 @@ async def span_search( | |
| ) | ||
| ) | ||
| ) | ||
| if attribute_filter: | ||
| for af in attribute_filter: | ||
| stmt = stmt.where(_parse_attribute_filter(af)) | ||
|
|
||
| if cursor: | ||
| try: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.