mirror of
https://github.com/SigNoz/signoz.git
synced 2026-11-23 08:40:42 +00:00
Compare commits
5 Commits
refactor/s
...
fix/raw-cu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1aa3f14fd0 | ||
|
|
8bb340d428 | ||
|
|
7f624ca8d1 | ||
|
|
8b8bc0f76d | ||
|
|
ed862f1fad |
@@ -4068,9 +4068,10 @@ components:
|
||||
the query they draw.
|
||||
cursor:
|
||||
description: The opaque nextCursor value from a previous raw result, used
|
||||
to fetch the next page. Honored only by raw requests whose order starts
|
||||
with timestamp, for logs followed by id in the same direction; every other
|
||||
request ignores it.
|
||||
to fetch the next page. Cannot be combined with offset. Honored only by
|
||||
raw requests whose order starts with timestamp, for logs followed by id
|
||||
in the same direction; any other raw request rejects it, and other request
|
||||
types ignore it.
|
||||
type: string
|
||||
disabled:
|
||||
description: When true, the result is excluded from the response; the query
|
||||
@@ -4125,9 +4126,8 @@ components:
|
||||
offset:
|
||||
description: The number of rows for raw and trace requests, or of ordered
|
||||
series or rows for scalar and time_series requests, to skip before the
|
||||
page starts. Requires limit. On raw requests the skip counts from where
|
||||
a cursor resumes, and the skipped rows are still read, so prefer cursor
|
||||
for paging.
|
||||
page starts. Requires limit, and cannot be combined with cursor. On raw
|
||||
requests the skipped rows are still read, so prefer cursor for paging.
|
||||
type: integer
|
||||
order:
|
||||
description: The sort order of the result. Order by a group-by field, an
|
||||
@@ -10276,9 +10276,10 @@ components:
|
||||
the query they draw.
|
||||
cursor:
|
||||
description: The opaque nextCursor value from a previous raw result, used
|
||||
to fetch the next page. Honored only by raw requests whose order starts
|
||||
with timestamp, for logs followed by id in the same direction; every other
|
||||
request ignores it.
|
||||
to fetch the next page. Cannot be combined with offset. Honored only by
|
||||
raw requests whose order starts with timestamp, for logs followed by id
|
||||
in the same direction; any other raw request rejects it, and other request
|
||||
types ignore it.
|
||||
type: string
|
||||
disabled:
|
||||
description: When true, the result is excluded from the response; the query
|
||||
@@ -10332,9 +10333,8 @@ components:
|
||||
offset:
|
||||
description: The number of rows for raw and trace requests, or of ordered
|
||||
series or rows for scalar and time_series requests, to skip before the
|
||||
page starts. Requires limit. On raw requests the skip counts from where
|
||||
a cursor resumes, and the skipped rows are still read, so prefer cursor
|
||||
for paging.
|
||||
page starts. Requires limit, and cannot be combined with cursor. On raw
|
||||
requests the skipped rows are still read, so prefer cursor for paging.
|
||||
type: integer
|
||||
order:
|
||||
description: The sort order of the result. Order by a group-by field, an
|
||||
@@ -10396,9 +10396,10 @@ components:
|
||||
the query they draw.
|
||||
cursor:
|
||||
description: The opaque nextCursor value from a previous raw result, used
|
||||
to fetch the next page. Honored only by raw requests whose order starts
|
||||
with timestamp, for logs followed by id in the same direction; every other
|
||||
request ignores it.
|
||||
to fetch the next page. Cannot be combined with offset. Honored only by
|
||||
raw requests whose order starts with timestamp, for logs followed by id
|
||||
in the same direction; any other raw request rejects it, and other request
|
||||
types ignore it.
|
||||
type: string
|
||||
disabled:
|
||||
description: When true, the result is excluded from the response; the query
|
||||
@@ -10453,9 +10454,8 @@ components:
|
||||
offset:
|
||||
description: The number of rows for raw and trace requests, or of ordered
|
||||
series or rows for scalar and time_series requests, to skip before the
|
||||
page starts. Requires limit. On raw requests the skip counts from where
|
||||
a cursor resumes, and the skipped rows are still read, so prefer cursor
|
||||
for paging.
|
||||
page starts. Requires limit, and cannot be combined with cursor. On raw
|
||||
requests the skipped rows are still read, so prefer cursor for paging.
|
||||
type: integer
|
||||
order:
|
||||
description: The sort order of the result. Order by a group-by field, an
|
||||
@@ -10517,9 +10517,10 @@ components:
|
||||
the query they draw.
|
||||
cursor:
|
||||
description: The opaque nextCursor value from a previous raw result, used
|
||||
to fetch the next page. Honored only by raw requests whose order starts
|
||||
with timestamp, for logs followed by id in the same direction; every other
|
||||
request ignores it.
|
||||
to fetch the next page. Cannot be combined with offset. Honored only by
|
||||
raw requests whose order starts with timestamp, for logs followed by id
|
||||
in the same direction; any other raw request rejects it, and other request
|
||||
types ignore it.
|
||||
type: string
|
||||
disabled:
|
||||
description: When true, the result is excluded from the response; the query
|
||||
@@ -10574,9 +10575,8 @@ components:
|
||||
offset:
|
||||
description: The number of rows for raw and trace requests, or of ordered
|
||||
series or rows for scalar and time_series requests, to skip before the
|
||||
page starts. Requires limit. On raw requests the skip counts from where
|
||||
a cursor resumes, and the skipped rows are still read, so prefer cursor
|
||||
for paging.
|
||||
page starts. Requires limit, and cannot be combined with cursor. On raw
|
||||
requests the skipped rows are still read, so prefer cursor for paging.
|
||||
type: integer
|
||||
order:
|
||||
description: The sort order of the result. Order by a group-by field, an
|
||||
|
||||
@@ -5305,7 +5305,7 @@ export interface DashboardtypesAIBuilderQuerySpecDTO {
|
||||
bucketOptions?: Querybuildertypesv5BucketOptionsDTO;
|
||||
/**
|
||||
* @type string
|
||||
* @description The opaque nextCursor value from a previous raw result, used to fetch the next page. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; every other request ignores it.
|
||||
* @description The opaque nextCursor value from a previous raw result, used to fetch the next page. Cannot be combined with offset. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; any other raw request rejects it, and other request types ignore it.
|
||||
*/
|
||||
cursor?: string;
|
||||
/**
|
||||
@@ -5352,7 +5352,7 @@ export interface DashboardtypesAIBuilderQuerySpecDTO {
|
||||
name?: string;
|
||||
/**
|
||||
* @type integer
|
||||
* @description The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit. On raw requests the skip counts from where a cursor resumes, and the skipped rows are still read, so prefer cursor for paging.
|
||||
* @description The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit, and cannot be combined with cursor. On raw requests the skipped rows are still read, so prefer cursor for paging.
|
||||
*/
|
||||
offset?: number;
|
||||
/**
|
||||
@@ -5697,7 +5697,7 @@ export interface Querybuildertypesv5QueryBuilderQueryGithubComSigNozSignozPkgTyp
|
||||
bucketOptions?: Querybuildertypesv5BucketOptionsDTO;
|
||||
/**
|
||||
* @type string
|
||||
* @description The opaque nextCursor value from a previous raw result, used to fetch the next page. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; every other request ignores it.
|
||||
* @description The opaque nextCursor value from a previous raw result, used to fetch the next page. Cannot be combined with offset. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; any other raw request rejects it, and other request types ignore it.
|
||||
*/
|
||||
cursor?: string;
|
||||
/**
|
||||
@@ -5744,7 +5744,7 @@ export interface Querybuildertypesv5QueryBuilderQueryGithubComSigNozSignozPkgTyp
|
||||
name?: string;
|
||||
/**
|
||||
* @type integer
|
||||
* @description The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit. On raw requests the skip counts from where a cursor resumes, and the skipped rows are still read, so prefer cursor for paging.
|
||||
* @description The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit, and cannot be combined with cursor. On raw requests the skipped rows are still read, so prefer cursor for paging.
|
||||
*/
|
||||
offset?: number;
|
||||
/**
|
||||
@@ -5877,7 +5877,7 @@ export interface Querybuildertypesv5QueryBuilderQueryGithubComSigNozSignozPkgTyp
|
||||
bucketOptions?: Querybuildertypesv5BucketOptionsDTO;
|
||||
/**
|
||||
* @type string
|
||||
* @description The opaque nextCursor value from a previous raw result, used to fetch the next page. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; every other request ignores it.
|
||||
* @description The opaque nextCursor value from a previous raw result, used to fetch the next page. Cannot be combined with offset. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; any other raw request rejects it, and other request types ignore it.
|
||||
*/
|
||||
cursor?: string;
|
||||
/**
|
||||
@@ -5924,7 +5924,7 @@ export interface Querybuildertypesv5QueryBuilderQueryGithubComSigNozSignozPkgTyp
|
||||
name?: string;
|
||||
/**
|
||||
* @type integer
|
||||
* @description The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit. On raw requests the skip counts from where a cursor resumes, and the skipped rows are still read, so prefer cursor for paging.
|
||||
* @description The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit, and cannot be combined with cursor. On raw requests the skipped rows are still read, so prefer cursor for paging.
|
||||
*/
|
||||
offset?: number;
|
||||
/**
|
||||
@@ -5976,7 +5976,7 @@ export interface Querybuildertypesv5QueryBuilderQueryGithubComSigNozSignozPkgTyp
|
||||
bucketOptions?: Querybuildertypesv5BucketOptionsDTO;
|
||||
/**
|
||||
* @type string
|
||||
* @description The opaque nextCursor value from a previous raw result, used to fetch the next page. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; every other request ignores it.
|
||||
* @description The opaque nextCursor value from a previous raw result, used to fetch the next page. Cannot be combined with offset. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; any other raw request rejects it, and other request types ignore it.
|
||||
*/
|
||||
cursor?: string;
|
||||
/**
|
||||
@@ -6023,7 +6023,7 @@ export interface Querybuildertypesv5QueryBuilderQueryGithubComSigNozSignozPkgTyp
|
||||
name?: string;
|
||||
/**
|
||||
* @type integer
|
||||
* @description The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit. On raw requests the skip counts from where a cursor resumes, and the skipped rows are still read, so prefer cursor for paging.
|
||||
* @description The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit, and cannot be combined with cursor. On raw requests the skipped rows are still read, so prefer cursor for paging.
|
||||
*/
|
||||
offset?: number;
|
||||
/**
|
||||
|
||||
@@ -3,8 +3,10 @@ package querier
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -262,6 +264,10 @@ func (q *builderQuery[T]) Execute(ctx context.Context) (*qbtypes.Result, error)
|
||||
if q.kind == qbtypes.RequestTypeRaw && q.isWindowList() {
|
||||
return q.executeWindowList(ctx)
|
||||
}
|
||||
if q.kind == qbtypes.RequestTypeRaw && strings.TrimSpace(q.spec.Cursor) != "" {
|
||||
return nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "cursor needs a raw query ordered by timestamp first").
|
||||
WithSuggestions("order logs by timestamp then id, both in one direction, and traces by timestamp")
|
||||
}
|
||||
|
||||
fromMS, toMS := q.fromMS, q.toMS
|
||||
if q.spec.Signal == telemetrytypes.SignalTraces || q.spec.Signal == telemetrytypes.SignalLogs {
|
||||
@@ -524,20 +530,53 @@ func (q *builderQuery[T]) executeWindowList(ctx context.Context) (*qbtypes.Resul
|
||||
isAsc := len(q.spec.Order) > 0 &&
|
||||
strings.ToLower(string(q.spec.Order[0].Direction.StringValue())) == "asc"
|
||||
|
||||
// A cursor names one row only when the order ends in the signal's tie-break
|
||||
// column. Logs already order by timestamp then id. A traces order of
|
||||
// timestamp alone gets span_id, 8 random bytes, so spans at one instant differ.
|
||||
// An attribute named span_id is not the column the cursor compares.
|
||||
order := q.spec.Order
|
||||
rowKeyed := q.spec.Signal == telemetrytypes.SignalLogs ||
|
||||
(len(order) == 2 && order[1].Key.Name == tracestelemetryschema.SpanSpanIDColumn && order[1].Direction == order[0].Direction &&
|
||||
(order[1].Key.FieldContext == telemetrytypes.FieldContextUnspecified || order[1].Key.FieldContext == telemetrytypes.FieldContextSpan))
|
||||
if q.spec.Signal == telemetrytypes.SignalTraces && len(order) == 1 {
|
||||
q.spec.Order = append(slices.Clip(order),
|
||||
qbtypes.OrderBy{Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: tracestelemetryschema.SpanSpanIDColumn, FieldContext: telemetrytypes.FieldContextSpan}}, Direction: order[0].Direction},
|
||||
)
|
||||
rowKeyed = true
|
||||
}
|
||||
|
||||
fromMS, toMS := q.fromMS, q.toMS
|
||||
|
||||
// Adjust [fromMS,toMS] window if a cursor was supplied
|
||||
if cur := strings.TrimSpace(q.spec.Cursor); cur != "" {
|
||||
if ts, err := decodeCursor(cur); err == nil {
|
||||
after, legacyMS, err := decodeCursor(cur)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch {
|
||||
case after == nil:
|
||||
// a cursor issued before the row key moves the window edge as it always did
|
||||
if isAsc {
|
||||
if uint64(ts) >= fromMS {
|
||||
fromMS = uint64(ts + 1)
|
||||
if uint64(legacyMS) >= fromMS {
|
||||
fromMS = uint64(legacyMS + 1)
|
||||
}
|
||||
} else { // DESC
|
||||
if uint64(ts) <= toMS {
|
||||
toMS = uint64(ts - 1)
|
||||
if uint64(legacyMS) <= toMS {
|
||||
toMS = uint64(legacyMS - 1)
|
||||
}
|
||||
}
|
||||
case !rowKeyed || (q.spec.Signal == telemetrytypes.SignalLogs && after.ID == "") || (q.spec.Signal == telemetrytypes.SignalTraces && after.SpanID == ""):
|
||||
return nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "cursor does not belong to this query").
|
||||
WithSuggestions("send the nextCursor with the same signal and order as the query that returned it")
|
||||
default:
|
||||
q.spec.StartAfter = after
|
||||
// the window keeps the whole boundary millisecond; StartAfter cuts inside it
|
||||
boundaryMS := after.TimeUnixNano / uint64(time.Millisecond)
|
||||
if isAsc {
|
||||
fromMS = max(fromMS, boundaryMS)
|
||||
} else {
|
||||
toMS = min(toMS, boundaryMS+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -644,8 +683,21 @@ func (q *builderQuery[T]) executeWindowList(ctx context.Context) (*qbtypes.Resul
|
||||
|
||||
nextCursor := ""
|
||||
if len(rows) == reqLimit {
|
||||
lastTS := rows[len(rows)-1].Timestamp.UnixMilli()
|
||||
nextCursor = encodeCursor(lastTS)
|
||||
last := rows[len(rows)-1]
|
||||
if rowKeyed {
|
||||
// the list builders always select the tie-break column, as a string
|
||||
key := qbtypes.RawRowKey{TimeUnixNano: uint64(last.Timestamp.UnixNano())}
|
||||
if q.spec.Signal == telemetrytypes.SignalLogs {
|
||||
key.ID, _ = last.Data["id"].(string)
|
||||
} else {
|
||||
key.SpanID, _ = last.Data["span_id"].(string)
|
||||
}
|
||||
// a number and strings always encode
|
||||
data, _ := json.Marshal(key)
|
||||
nextCursor = base64.RawURLEncoding.EncodeToString(data)
|
||||
} else {
|
||||
nextCursor = encodeCursor(last.Timestamp.UnixMilli())
|
||||
}
|
||||
}
|
||||
|
||||
return &qbtypes.Result{
|
||||
@@ -669,10 +721,23 @@ func encodeCursor(tsMilli int64) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(strconv.FormatInt(tsMilli, 10)))
|
||||
}
|
||||
|
||||
func decodeCursor(cur string) (int64, error) {
|
||||
b, err := base64.StdEncoding.DecodeString(cur)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
// decodeCursor reads a raw nextCursor. A cursor issued before the row key
|
||||
// holds only the last row's millisecond; it comes back as legacyMS.
|
||||
func decodeCursor(cursor string) (*qbtypes.RawRowKey, int64, error) {
|
||||
if data, err := base64.StdEncoding.DecodeString(cursor); err == nil {
|
||||
if legacyMS, err := strconv.ParseInt(string(data), 10, 64); err == nil {
|
||||
return nil, legacyMS, nil
|
||||
}
|
||||
}
|
||||
return strconv.ParseInt(string(b), 10, 64)
|
||||
|
||||
after := &qbtypes.RawRowKey{}
|
||||
data, err := base64.RawURLEncoding.DecodeString(cursor)
|
||||
if err == nil {
|
||||
err = json.Unmarshal(data, after)
|
||||
}
|
||||
if err != nil || after.TimeUnixNano == 0 {
|
||||
return nil, 0, errors.NewInvalidInputf(errors.CodeInvalidInput, "invalid cursor %q", cursor).
|
||||
WithSuggestions("send the nextCursor of the previous page unchanged")
|
||||
}
|
||||
return after, 0, nil
|
||||
}
|
||||
|
||||
@@ -280,6 +280,19 @@ func (b *auditQueryStatementBuilder) buildListQuery(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if after := query.StartAfter; after != nil {
|
||||
// an OR, not a tuple comparison, so ClickHouse can use it to skip granules
|
||||
compare := sb.G
|
||||
if query.Order[0].Direction == qbtypes.OrderDirectionDesc {
|
||||
compare = sb.L
|
||||
}
|
||||
timestamp := fmt.Sprintf("%d", after.TimeUnixNano)
|
||||
sb.Where(sb.Or(
|
||||
compare(audittelemetryschema.TimestampColumn, timestamp),
|
||||
sb.And(sb.E(audittelemetryschema.TimestampColumn, timestamp), compare(audittelemetryschema.IDColumn, after.ID)),
|
||||
))
|
||||
}
|
||||
|
||||
for _, orderBy := range query.Order {
|
||||
colExpr, keyWarnings, err := querybuilder.ResolveColumn(ctx, info, b.storage, &orderBy.Key.TelemetryFieldKey, telemetrytypes.FieldDataTypeUnspecified, keys)
|
||||
if err != nil {
|
||||
|
||||
@@ -164,6 +164,28 @@ func TestStatementBuilder(t *testing.T) {
|
||||
Args: []any{"service_account", "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 100},
|
||||
},
|
||||
},
|
||||
// List: the next page of failed actions continues strictly after the cursor row
|
||||
{
|
||||
name: "ListStartAfterCursorRow",
|
||||
requestType: qbtypes.RequestTypeRaw,
|
||||
query: qbtypes.QueryBuilderQuery[qbtypes.LogAggregation]{
|
||||
Signal: telemetrytypes.SignalLogs,
|
||||
Source: telemetrytypes.SourceAudit,
|
||||
Filter: &qbtypes.Filter{
|
||||
Expression: "signoz.audit.outcome = 'failure'",
|
||||
},
|
||||
Order: []qbtypes.OrderBy{
|
||||
{Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "timestamp"}}, Direction: qbtypes.OrderDirectionDesc},
|
||||
{Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "id"}}, Direction: qbtypes.OrderDirectionDesc},
|
||||
},
|
||||
Limit: 2,
|
||||
StartAfter: &qbtypes.RawRowKey{TimeUnixNano: 1747983000000000500, ID: "019a-cursor-row"},
|
||||
},
|
||||
expected: qbtypes.Statement{
|
||||
Query: "SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, event_name, attributes_string, attributes_number, attributes_bool, resource, scope_string FROM signoz_audit.distributed_logs WHERE (`attribute_string_signoz$$audit$$outcome` = ? AND `attribute_string_signoz$$audit$$outcome_exists` = true) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? AND (timestamp < ? OR (timestamp = ? AND id < ?)) ORDER BY timestamp desc, id desc LIMIT ?",
|
||||
Args: []any{"failure", "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), "1747983000000000500", "1747983000000000500", "019a-cursor-row", 2},
|
||||
},
|
||||
},
|
||||
// Scalar: alert — count forbidden errors (outcome + action AND)
|
||||
{
|
||||
name: "ScalarCountByOutcomeAndAction",
|
||||
|
||||
@@ -395,6 +395,19 @@ func (b *logQueryStatementBuilder) buildListQuery(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if after := query.StartAfter; after != nil {
|
||||
// an OR, not a tuple comparison, so ClickHouse can use it to skip granules
|
||||
compare := sb.G
|
||||
if query.Order[0].Direction == qbtypes.OrderDirectionDesc {
|
||||
compare = sb.L
|
||||
}
|
||||
timestamp := fmt.Sprintf("%d", after.TimeUnixNano)
|
||||
sb.Where(sb.Or(
|
||||
compare(logstelemetryschema.LogsV2TimestampColumn, timestamp),
|
||||
sb.And(sb.E(logstelemetryschema.LogsV2TimestampColumn, timestamp), compare(logstelemetryschema.LogsV2IDColumn, after.ID)),
|
||||
))
|
||||
}
|
||||
|
||||
// Add order by
|
||||
for _, orderBy := range query.Order {
|
||||
|
||||
|
||||
@@ -406,6 +406,19 @@ func (b *traceQueryStatementBuilder) buildListQuery(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if after := query.StartAfter; after != nil {
|
||||
// an OR, not a tuple comparison, so ClickHouse can use it to skip granules
|
||||
compare := sb.G
|
||||
if query.Order[0].Direction == qbtypes.OrderDirectionDesc {
|
||||
compare = sb.L
|
||||
}
|
||||
timestamp := fmt.Sprintf("%d", after.TimeUnixNano)
|
||||
sb.Where(sb.Or(
|
||||
compare(tracestelemetryschema.SpanTimestampColumn, timestamp),
|
||||
sb.And(sb.E(tracestelemetryschema.SpanTimestampColumn, timestamp), compare(tracestelemetryschema.SpanSpanIDColumn, after.SpanID)),
|
||||
))
|
||||
}
|
||||
|
||||
// Add order by
|
||||
for _, orderBy := range query.Order {
|
||||
expr, keyWarnings, err := querybuilder.ResolveColumn(ctx, info, b.storage, &orderBy.Key.TelemetryFieldKey, telemetrytypes.FieldDataTypeUnspecified, keys)
|
||||
|
||||
@@ -38,9 +38,9 @@ type QueryBuilderQuery[T any] struct {
|
||||
|
||||
LimitBy *LimitBy `json:"limitBy,omitempty" description:"Reserved for applying limit within each combination of the given group-by keys. Accepted but currently ignored."`
|
||||
|
||||
Offset int `json:"offset,omitempty" description:"The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit. On raw requests the skip counts from where a cursor resumes, and the skipped rows are still read, so prefer cursor for paging."`
|
||||
Offset int `json:"offset,omitempty" description:"The number of rows for raw and trace requests, or of ordered series or rows for scalar and time_series requests, to skip before the page starts. Requires limit, and cannot be combined with cursor. On raw requests the skipped rows are still read, so prefer cursor for paging."`
|
||||
|
||||
Cursor string `json:"cursor,omitempty" description:"The opaque nextCursor value from a previous raw result, used to fetch the next page. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; every other request ignores it."`
|
||||
Cursor string `json:"cursor,omitempty" description:"The opaque nextCursor value from a previous raw result, used to fetch the next page. Cannot be combined with offset. Honored only by raw requests whose order starts with timestamp, for logs followed by id in the same direction; any other raw request rejects it, and other request types ignore it."`
|
||||
|
||||
Having *Having `json:"having,omitempty" description:"A filter applied to aggregation results, referencing aliases or expressions, for example count() > 100."`
|
||||
|
||||
@@ -53,6 +53,19 @@ type QueryBuilderQuery[T any] struct {
|
||||
// ShiftBy is extracted from timeShift function for internal use
|
||||
// This field is not serialized to JSON
|
||||
ShiftBy int64 `json:"-"`
|
||||
|
||||
// StartAfter is the row a raw page continues from, decoded from Cursor.
|
||||
// The page holds only rows strictly after it in the requested order.
|
||||
StartAfter *RawRowKey `json:"-"`
|
||||
}
|
||||
|
||||
// RawRowKey is the full order key of one raw row: the timestamp, then the
|
||||
// signal's tie-break column, id for logs and span_id for traces.
|
||||
// It is the content of a raw nextCursor.
|
||||
type RawRowKey struct {
|
||||
TimeUnixNano uint64 `json:"timeUnixNano"`
|
||||
ID string `json:"id,omitempty"`
|
||||
SpanID string `json:"spanId,omitempty"`
|
||||
}
|
||||
|
||||
// PrepareJSONSchema pins `signal` to the single value implied by the aggregation
|
||||
@@ -143,6 +156,11 @@ func (q QueryBuilderQuery[T]) Copy() QueryBuilderQuery[T] {
|
||||
c.BucketOptions = &bucketOptionsCopy
|
||||
}
|
||||
|
||||
if q.StartAfter != nil {
|
||||
startAfterCopy := *q.StartAfter
|
||||
c.StartAfter = &startAfterCopy
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
@@ -441,6 +441,13 @@ func (q *QueryBuilderQuery[T]) validateLimitAndPagination(cfg validationConfig)
|
||||
return errors.NewInvalidInputf(errors.CodeInvalidInput, "offset requires a limit")
|
||||
}
|
||||
|
||||
// the cursor already starts after the last row returned, so an offset
|
||||
// would skip rows nobody has seen
|
||||
if q.Cursor != "" && q.Offset > 0 {
|
||||
return errors.NewInvalidInputf(errors.CodeInvalidInput, "offset cannot be combined with cursor, got offset %d", q.Offset).
|
||||
WithSuggestions("send offset 0 with the cursor; the cursor continues after the last row of the previous page")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
3
tests/fixtures/querier.py
vendored
3
tests/fixtures/querier.py
vendored
@@ -84,6 +84,7 @@ class BuilderQuery:
|
||||
query_type: str = "builder_query"
|
||||
limit: int | None = None
|
||||
offset: int | None = None
|
||||
cursor: str | None = None
|
||||
filter_expression: str | None = None
|
||||
having_expression: str | None = None
|
||||
select_fields: list[TelemetryFieldKey] | None = None
|
||||
@@ -103,6 +104,8 @@ class BuilderQuery:
|
||||
spec["limit"] = self.limit
|
||||
if self.offset is not None:
|
||||
spec["offset"] = self.offset
|
||||
if self.cursor:
|
||||
spec["cursor"] = self.cursor
|
||||
if self.filter_expression:
|
||||
spec["filter"] = {"expression": self.filter_expression}
|
||||
if self.having_expression:
|
||||
|
||||
@@ -9,7 +9,12 @@ from fixtures import types
|
||||
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
|
||||
from fixtures.logs import Logs
|
||||
from fixtures.querier import (
|
||||
BuilderQuery,
|
||||
OrderBy,
|
||||
RequestType,
|
||||
TelemetryFieldKey,
|
||||
assert_identical_query_response,
|
||||
get_rows,
|
||||
make_query_request,
|
||||
)
|
||||
|
||||
@@ -498,3 +503,103 @@ def test_logs_list_with_order_by(
|
||||
ids = [row["data"]["attributes_string"].get("id", "") for row in rows]
|
||||
|
||||
assert ids == expected_order
|
||||
|
||||
|
||||
@pytest.mark.parametrize("direction", ["desc", "asc"])
|
||||
def test_logs_list_cursor_pages_every_row_once(
|
||||
signoz: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
insert_logs: Callable[[list[Logs]], None],
|
||||
direction: str,
|
||||
) -> None:
|
||||
"""
|
||||
Setup:
|
||||
7 logs: one alone in its millisecond at each end, 5 inside one millisecond,
|
||||
3 of them at the same instant.
|
||||
|
||||
Tests:
|
||||
Paging by nextCursor with limit 2 returns every log once, ordered by
|
||||
timestamp then id, although page boundaries fall inside the shared
|
||||
millisecond and inside the shared instant.
|
||||
"""
|
||||
now = datetime.now(tz=UTC).replace(microsecond=0)
|
||||
shared = now - timedelta(seconds=5) + timedelta(microseconds=300)
|
||||
instants = [
|
||||
now - timedelta(seconds=6),
|
||||
shared,
|
||||
shared + timedelta(microseconds=1),
|
||||
shared + timedelta(microseconds=2),
|
||||
shared + timedelta(microseconds=2),
|
||||
shared + timedelta(microseconds=2),
|
||||
now - timedelta(seconds=4),
|
||||
]
|
||||
logs = [Logs(timestamp=instant, resources={"service.name": "cursor-service"}, body=f"log-{i}") for i, instant in enumerate(instants)]
|
||||
insert_logs(logs)
|
||||
expected = [log.id for log in sorted(logs, key=lambda log: (int(log.timestamp), log.id), reverse=direction == "desc")]
|
||||
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
seen: list[str] = []
|
||||
cursor = None
|
||||
for _ in range(len(logs) + 1):
|
||||
response = make_query_request(
|
||||
signoz,
|
||||
token,
|
||||
start_ms=int((now - timedelta(minutes=1)).timestamp() * 1000),
|
||||
end_ms=int((now + timedelta(seconds=1)).timestamp() * 1000),
|
||||
request_type=RequestType.RAW,
|
||||
queries=[
|
||||
BuilderQuery(
|
||||
signal="logs",
|
||||
limit=2,
|
||||
cursor=cursor,
|
||||
filter_expression="service.name = 'cursor-service'",
|
||||
order=[OrderBy(TelemetryFieldKey("timestamp"), direction), OrderBy(TelemetryFieldKey("id"), direction)],
|
||||
).to_dict()
|
||||
],
|
||||
)
|
||||
assert response.status_code == HTTPStatus.OK, response.text
|
||||
seen += [row["data"]["id"] for row in get_rows(response)]
|
||||
cursor = response.json()["data"]["data"]["results"][0].get("nextCursor")
|
||||
if not cursor:
|
||||
break
|
||||
|
||||
assert seen == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"order,offset,cursor,message",
|
||||
[
|
||||
pytest.param(["timestamp", "id"], 2, "MTc5MzA0MDgzNjc5NQ==", "offset cannot be combined with cursor, got offset 2", id="offset_with_cursor"),
|
||||
pytest.param(["timestamp"], 0, "MTc5MzA0MDgzNjc5NQ==", "cursor needs a raw query ordered by timestamp first", id="order_without_id"),
|
||||
pytest.param(["timestamp", "id"], 0, "not-a-cursor", 'invalid cursor "not-a-cursor"', id="invalid_cursor"),
|
||||
],
|
||||
)
|
||||
def test_logs_list_cursor_rejections(
|
||||
signoz: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
order: list[str],
|
||||
offset: int,
|
||||
cursor: str,
|
||||
message: str,
|
||||
) -> None:
|
||||
now = datetime.now(tz=UTC)
|
||||
response = make_query_request(
|
||||
signoz,
|
||||
get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD),
|
||||
start_ms=int((now - timedelta(minutes=5)).timestamp() * 1000),
|
||||
end_ms=int(now.timestamp() * 1000),
|
||||
request_type=RequestType.RAW,
|
||||
queries=[
|
||||
BuilderQuery(
|
||||
signal="logs",
|
||||
limit=2,
|
||||
offset=offset,
|
||||
cursor=cursor,
|
||||
order=[OrderBy(TelemetryFieldKey(name), "desc") for name in order],
|
||||
).to_dict()
|
||||
],
|
||||
)
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST, response.text
|
||||
assert message in response.json()["error"]["message"], response.text
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import base64
|
||||
from collections.abc import Callable
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from http import HTTPStatus
|
||||
@@ -917,6 +918,139 @@ def test_traces_list_pagination(
|
||||
assert [row["data"]["span_id"] for row in rows] == expected_ids
|
||||
|
||||
|
||||
@pytest.mark.parametrize("direction", ["desc", "asc"])
|
||||
@pytest.mark.parametrize("order", [["timestamp"], ["timestamp", "span_id"]], ids=["timestamp", "timestamp_span_id"])
|
||||
def test_traces_list_cursor_pages_every_row_once(
|
||||
signoz: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
insert_traces: Callable[[list[Traces]], None],
|
||||
direction: str,
|
||||
order: list[str],
|
||||
) -> None:
|
||||
"""
|
||||
Setup:
|
||||
7 spans: one alone in its millisecond at each end, 5 inside one millisecond,
|
||||
3 of them at the same instant.
|
||||
|
||||
Tests:
|
||||
Paging by nextCursor with limit 2 returns every span once, ordered by
|
||||
timestamp then span_id, although page boundaries fall inside the shared
|
||||
millisecond and inside the shared instant. An order of timestamp alone and
|
||||
an explicit order of timestamp then span_id page the same way.
|
||||
"""
|
||||
now = datetime.now(tz=UTC).replace(microsecond=0)
|
||||
shared = now - timedelta(seconds=5) + timedelta(microseconds=300)
|
||||
instants = [
|
||||
now - timedelta(seconds=6),
|
||||
shared,
|
||||
shared + timedelta(microseconds=1),
|
||||
shared + timedelta(microseconds=2),
|
||||
shared + timedelta(microseconds=2),
|
||||
shared + timedelta(microseconds=2),
|
||||
now - timedelta(seconds=4),
|
||||
]
|
||||
spans = [
|
||||
Traces(
|
||||
timestamp=instant,
|
||||
trace_id=TraceIdGenerator.trace_id(),
|
||||
span_id=TraceIdGenerator.span_id(),
|
||||
name=f"span-{i}",
|
||||
resources={"service.name": "cursor-service"},
|
||||
)
|
||||
for i, instant in enumerate(instants)
|
||||
]
|
||||
insert_traces(spans)
|
||||
expected = [span.span_id for span in sorted(spans, key=lambda span: (span.timestamp, span.span_id), reverse=direction == "desc")]
|
||||
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
seen: list[str] = []
|
||||
cursor = None
|
||||
for _ in range(len(spans) + 1):
|
||||
response = make_query_request(
|
||||
signoz,
|
||||
token,
|
||||
start_ms=int((now - timedelta(minutes=1)).timestamp() * 1000),
|
||||
end_ms=int((now + timedelta(seconds=1)).timestamp() * 1000),
|
||||
request_type=RequestType.RAW,
|
||||
queries=[
|
||||
BuilderQuery(
|
||||
signal="traces",
|
||||
limit=2,
|
||||
cursor=cursor,
|
||||
filter_expression="service.name = 'cursor-service'",
|
||||
order=[OrderBy(TelemetryFieldKey(name), direction) for name in order],
|
||||
).to_dict()
|
||||
],
|
||||
)
|
||||
assert response.status_code == HTTPStatus.OK, response.text
|
||||
seen += [row["data"]["span_id"] for row in get_rows(response)]
|
||||
cursor = response.json()["data"]["data"]["results"][0].get("nextCursor")
|
||||
if not cursor:
|
||||
break
|
||||
|
||||
assert seen == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"direction,boundary,expected",
|
||||
[
|
||||
pytest.param("desc", 2, [1, 0], id="desc"),
|
||||
pytest.param("asc", 1, [2, 3], id="asc"),
|
||||
],
|
||||
)
|
||||
def test_traces_list_millisecond_cursor_still_pages(
|
||||
signoz: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
insert_traces: Callable[[list[Traces]], None],
|
||||
direction: str,
|
||||
boundary: int,
|
||||
expected: list[int],
|
||||
) -> None:
|
||||
"""
|
||||
Setup:
|
||||
4 spans 10 ms apart.
|
||||
|
||||
Tests:
|
||||
A cursor in the earlier format, the base64 of a millisecond, still continues
|
||||
from that millisecond as it did, so a page a client holds before an upgrade
|
||||
keeps working.
|
||||
"""
|
||||
now = datetime.now(tz=UTC).replace(microsecond=0)
|
||||
spans = [
|
||||
Traces(
|
||||
timestamp=now - timedelta(seconds=5) + timedelta(milliseconds=10 * i),
|
||||
trace_id=TraceIdGenerator.trace_id(),
|
||||
span_id=TraceIdGenerator.span_id(),
|
||||
name=f"span-{i}",
|
||||
resources={"service.name": "legacy-cursor-service"},
|
||||
)
|
||||
for i in range(4)
|
||||
]
|
||||
insert_traces(spans)
|
||||
cursor = base64.b64encode(str(int(spans[boundary].timestamp.timestamp() * 1000)).encode()).decode()
|
||||
|
||||
response = make_query_request(
|
||||
signoz,
|
||||
get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD),
|
||||
start_ms=int((now - timedelta(minutes=1)).timestamp() * 1000),
|
||||
end_ms=int((now + timedelta(seconds=1)).timestamp() * 1000),
|
||||
request_type=RequestType.RAW,
|
||||
queries=[
|
||||
BuilderQuery(
|
||||
signal="traces",
|
||||
limit=10,
|
||||
cursor=cursor,
|
||||
filter_expression="service.name = 'legacy-cursor-service'",
|
||||
order=[OrderBy(TelemetryFieldKey("timestamp"), direction)],
|
||||
).to_dict()
|
||||
],
|
||||
)
|
||||
assert response.status_code == HTTPStatus.OK, response.text
|
||||
assert [row["data"]["span_id"] for row in get_rows(response)] == [spans[i].span_id for i in expected]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Time window
|
||||
# ============================================================================
|
||||
|
||||
Reference in New Issue
Block a user