Skip to content
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

[TraceQL] quantile_over_time 2 of 2 - engine #3633

Merged
merged 18 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
50a6984
Add lang/parser support for quantile_over_time, fix missing stringify…
mdisibio Apr 24, 2024
7307d4c
First working draft of quantile_over_time implementation
mdisibio Apr 24, 2024
589d83f
Validate the query in the frontend
mdisibio Apr 25, 2024
6abee43
Histogram accumulate jobs by bucket as they come in instead of at the…
mdisibio Apr 25, 2024
923d7d8
Fix language definition to allow both floats or ints for quantiles
mdisibio Apr 25, 2024
2ccc75f
Remove layer of proto->seriesset conversion. Fix roundtrip of __bucke…
mdisibio Apr 25, 2024
e320bbb
Rename to SimpleAdditionCombiner, slight interval calc cleanup
mdisibio Apr 25, 2024
b845743
Fix p0 returning 1 instead of minimum value, comments cleanup
mdisibio Apr 25, 2024
ee0e5bc
Rename frontend param and fix handling of ints
mdisibio Apr 26, 2024
4c1db95
Fix pre-existing bug in metrics optimization when asserting multiple …
mdisibio Apr 26, 2024
656f525
Fix to support 3 flavors of the metrics pipeline: query-frontend, acr…
mdisibio Apr 26, 2024
81deb1b
Merge branch 'main' into quantile-engine
mdisibio Apr 29, 2024
cb31ed0
Update query_range frontend test for new behavior
mdisibio Apr 29, 2024
b49d994
Consolidate histogram code between traceql and traceqlmetrics. quanti…
mdisibio Apr 30, 2024
05c3af4
lint
mdisibio May 2, 2024
dbe0a18
changelog
mdisibio May 2, 2024
5c79dc2
Redo histograms to set __bucket label to the actual value instead of …
mdisibio May 3, 2024
3c8507f
Revert all changes to traceqlmetrics package, was getting too noisy
mdisibio May 3, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## main / unreleased

* [FEATURE] Add TLS support for Memcached Client [#3585](https://github.com/grafana/tempo/pull/3585) (@sonisr)
* [FEATURE] TraceQL metrics queries: add quantile_over_time [#3605](https://github.com/grafana/tempo/pull/3605) [#3633](https://github.com/grafana/tempo/pull/3633) (@mdisibio)
* [ENHANCEMENT] Add querier metrics for requests executed [#3524](https://github.com/grafana/tempo/pull/3524) (@electron0zero)
* [FEATURE] Added gRPC streaming endpoints for Tempo APIs.
* Added gRPC streaming endpoints for all tag queries. [#3460](https://github.com/grafana/tempo/pull/3460) (@joe-elliott)
Expand Down
18 changes: 13 additions & 5 deletions modules/frontend/combiner/metrics_query_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
var _ GRPCCombiner[*tempopb.QueryRangeResponse] = (*genericCombiner[*tempopb.QueryRangeResponse])(nil)

// NewQueryRange returns a query range combiner.
func NewQueryRange() Combiner {
combiner := traceql.QueryRangeCombiner{}
func NewQueryRange(req *tempopb.QueryRangeRequest) (Combiner, error) {
combiner, err := traceql.QueryRangeCombinerFor(req, traceql.AggregateModeFinal)
if err != nil {
return nil, err
}

return &genericCombiner[*tempopb.QueryRangeResponse]{
httpStatusCode: 200,
Expand Down Expand Up @@ -65,11 +68,16 @@ func NewQueryRange() Combiner {
sortResponse(resp)
return resp, nil
},
}
}, nil
}

func NewTypedQueryRange() GRPCCombiner[*tempopb.QueryRangeResponse] {
return NewQueryRange().(GRPCCombiner[*tempopb.QueryRangeResponse])
func NewTypedQueryRange(req *tempopb.QueryRangeRequest) (GRPCCombiner[*tempopb.QueryRangeResponse], error) {
c, err := NewQueryRange(req)
if err != nil {
return nil, err
}

return c.(GRPCCombiner[*tempopb.QueryRangeResponse]), nil
}

func sortResponse(res *tempopb.QueryRangeResponse) {
Expand Down
18 changes: 15 additions & 3 deletions modules/frontend/metrics_query_range_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ func newQueryRangeStreamingGRPCHandler(cfg Config, next pipeline.AsyncRoundTripp
start := time.Now()

var finalResponse *tempopb.QueryRangeResponse
c := combiner.NewTypedQueryRange()
c, err := combiner.NewTypedQueryRange(req)
if err != nil {
return err
}

collector := pipeline.NewGRPCCollector(next, c, func(qrr *tempopb.QueryRangeResponse) error {
finalResponse = qrr // sadly we can't pass srv.Send directly into the collector. we need bytesProcessed for the SLO calculations
return srv.Send(qrr)
})

logQueryRangeRequest(logger, tenant, req)
err := collector.RoundTrip(httpReq)
err = collector.RoundTrip(httpReq)

duration := time.Since(start)
bytesProcessed := uint64(0)
Expand Down Expand Up @@ -80,7 +84,15 @@ func newMetricsQueryRangeHTTPHandler(cfg Config, next pipeline.AsyncRoundTripper
logQueryRangeRequest(logger, tenant, queryRangeReq)

// build and use roundtripper
combiner := combiner.NewTypedQueryRange()
combiner, err := combiner.NewTypedQueryRange(queryRangeReq)
if err != nil {
level.Error(logger).Log("msg", "query range: query range combiner failed", "err", err)
return &http.Response{
StatusCode: http.StatusInternalServerError,
joe-elliott marked this conversation as resolved.
Show resolved Hide resolved
Status: http.StatusText(http.StatusInternalServerError),
Body: io.NopCloser(strings.NewReader(err.Error())),
}, nil
}
rt := pipeline.NewHTTPCollector(next, combiner)

resp, err := rt.RoundTrip(req)
Expand Down
74 changes: 45 additions & 29 deletions modules/frontend/metrics_query_range_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/grafana/dskit/user"
"github.com/grafana/tempo/pkg/api"
"github.com/grafana/tempo/pkg/tempopb"
v1 "github.com/grafana/tempo/pkg/tempopb/common/v1"
"github.com/stretchr/testify/require"
)

Expand All @@ -22,13 +23,16 @@ func TestQueryRangeHandlerSucceeds(t *testing.T) {
Series: []*tempopb.TimeSeries{
{
PromLabels: "foo",
Labels: []v1.KeyValue{
{Key: "foo", Value: &v1.AnyValue{Value: &v1.AnyValue_StringValue{StringValue: "bar"}}},
},
Samples: []tempopb.Sample{
{
TimestampMs: 2,
TimestampMs: 1200_000,
Value: 2,
},
{
TimestampMs: 1,
TimestampMs: 1100_000,
Value: 1,
},
},
Expand All @@ -40,15 +44,17 @@ func TestQueryRangeHandlerSucceeds(t *testing.T) {
responseFn: func() proto.Message {
return resp
},
}, nil, nil, nil)
}, nil, nil, nil, func(c *Config) {
c.Metrics.Sharder.Interval = time.Hour
})
tenant := "foo"

httpReq := httptest.NewRequest("GET", api.PathMetricsQueryRange, nil)
httpReq = api.BuildQueryRangeRequest(httpReq, &tempopb.QueryRangeRequest{
Query: "{} | rate()",
Start: 1,
End: uint64(10000 * time.Second),
Step: uint64(1 * time.Second),
Start: uint64(1100 * time.Second),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test changes here are due to a change in response combining. Previously the combiner did simple addition by timestamp, so only the timestamps populated by jobs were returned. Now the new SimpleAdditionAggregator and HistogramAggregator start with a zero-slice for all expected time slots in the final request range. The tests had to be restricted to match the test blocks in the test module here, so the output was still readable (not 10,000 zeros). I think this change overall is good but open to discuss. The question boils down to: If you rate() over a single span in the last hour, should it return 1 data point, or fill in the zeros for the whole response?

End: uint64(1200 * time.Second),
Step: uint64(100 * time.Second),
})

ctx := user.InjectOrgID(httpReq.Context(), tenant)
Expand All @@ -63,24 +69,27 @@ func TestQueryRangeHandlerSucceeds(t *testing.T) {
// for reasons I don't understand, this query turns into 408 jobs.
expectedResp := &tempopb.QueryRangeResponse{
Metrics: &tempopb.SearchMetrics{
CompletedJobs: 408,
InspectedTraces: 408,
InspectedBytes: 408,
TotalJobs: 408,
CompletedJobs: 4,
InspectedTraces: 4,
InspectedBytes: 4,
TotalJobs: 4,
TotalBlocks: 2,
TotalBlockBytes: 419430400,
},
Series: []*tempopb.TimeSeries{
{
PromLabels: "foo",
Labels: []v1.KeyValue{
{Key: "foo", Value: &v1.AnyValue{Value: &v1.AnyValue_StringValue{StringValue: "bar"}}},
},
Samples: []tempopb.Sample{
{
TimestampMs: 1,
Value: 408,
TimestampMs: 1100_000,
Value: 4,
},
{
TimestampMs: 2,
Value: 816,
TimestampMs: 1200_000,
Value: 8,
},
},
},
Expand All @@ -102,13 +111,16 @@ func TestQueryRangeHandlerRespectsSamplingRate(t *testing.T) {
Series: []*tempopb.TimeSeries{
{
PromLabels: "foo",
Labels: []v1.KeyValue{
{Key: "foo", Value: &v1.AnyValue{Value: &v1.AnyValue_StringValue{StringValue: "bar"}}},
},
Samples: []tempopb.Sample{
{
TimestampMs: 2,
TimestampMs: 1200_000,
Value: 2,
},
{
TimestampMs: 1,
TimestampMs: 1100_000,
Value: 1,
},
},
Expand All @@ -120,15 +132,17 @@ func TestQueryRangeHandlerRespectsSamplingRate(t *testing.T) {
responseFn: func() proto.Message {
return resp
},
}, nil, nil, nil)
}, nil, nil, nil, func(c *Config) {
c.Metrics.Sharder.Interval = time.Hour
})
tenant := "foo"

httpReq := httptest.NewRequest("GET", api.PathMetricsQueryRange, nil)
httpReq = api.BuildQueryRangeRequest(httpReq, &tempopb.QueryRangeRequest{
Query: "{} | rate() with (sample=.2)",
Start: 1,
End: uint64(10000 * time.Second),
Step: uint64(1 * time.Second),
Start: uint64(1100 * time.Second),
End: uint64(1200 * time.Second),
Step: uint64(100 * time.Second),
})

ctx := user.InjectOrgID(httpReq.Context(), tenant)
Expand All @@ -140,27 +154,29 @@ func TestQueryRangeHandlerRespectsSamplingRate(t *testing.T) {

require.Equal(t, 200, httpResp.Code)

// for reasons I don't understand, this query turns into 408 jobs.
expectedResp := &tempopb.QueryRangeResponse{
Metrics: &tempopb.SearchMetrics{
CompletedJobs: 102,
InspectedTraces: 102,
InspectedBytes: 102,
TotalJobs: 102,
CompletedJobs: 1,
InspectedTraces: 1,
InspectedBytes: 1,
TotalJobs: 1,
TotalBlocks: 2,
TotalBlockBytes: 419430400,
},
Series: []*tempopb.TimeSeries{
{
PromLabels: "foo",
Labels: []v1.KeyValue{
{Key: "foo", Value: &v1.AnyValue{Value: &v1.AnyValue_StringValue{StringValue: "bar"}}},
},
Samples: []tempopb.Sample{
{
TimestampMs: 1,
Value: 510,
TimestampMs: 1100_000,
Value: 5,
},
{
TimestampMs: 2,
Value: 1020,
TimestampMs: 1200_000,
Value: 10,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion modules/frontend/metrics_query_range_sharder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (s queryRangeSharder) RoundTrip(r *http.Request) (pipeline.Responses[combin
return pipeline.NewBadRequest(err), nil
}

expr, err := traceql.Parse(req.Query)
expr, _, _, _, err := traceql.NewEngine().Compile(req.Query)
if err != nil {
return pipeline.NewBadRequest(err), nil
}
Expand Down
8 changes: 7 additions & 1 deletion modules/frontend/search_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,9 @@ func cacheResponsesEqual(t *testing.T, cacheResponse *tempopb.SearchResponse, pi

// frontendWithSettings returns a new frontend with the given settings. any nil options
// are given "happy path" defaults
func frontendWithSettings(t *testing.T, next http.RoundTripper, rdr tempodb.Reader, cfg *Config, cacheProvider cache.Provider) *QueryFrontend {
func frontendWithSettings(t *testing.T, next http.RoundTripper, rdr tempodb.Reader, cfg *Config, cacheProvider cache.Provider,
opts ...func(*Config),
) *QueryFrontend {
if next == nil {
next = &mockRoundTripper{
responseFn: func() proto.Message {
Expand Down Expand Up @@ -721,6 +723,10 @@ func frontendWithSettings(t *testing.T, next http.RoundTripper, rdr tempodb.Read
}
}

for _, o := range opts {
o(cfg)
}

o, err := overrides.NewOverrides(overrides.Config{}, nil, prometheus.DefaultRegisterer)
require.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion modules/generator/processor/localblocks/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ func (p *Processor) QueryRange(ctx context.Context, req *tempopb.QueryRangeReque
concurrency = uint(v)
}

// Compile the sharded version of the query
eval, err := traceql.NewEngine().CompileMetricsQueryRange(req, false, timeOverlapCutoff, unsafe)
if err != nil {
return nil, err
Expand Down Expand Up @@ -519,7 +520,7 @@ func (p *Processor) QueryRange(ctx context.Context, req *tempopb.QueryRangeReque
return nil, err
}

return eval.Results()
return eval.Results(), nil
}

func (p *Processor) metricsCacheGet(key string) *traceqlmetrics.MetricsResults {
Expand Down
12 changes: 7 additions & 5 deletions modules/querier/querier_query_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func (q *Querier) queryRangeRecent(ctx context.Context, req *tempopb.QueryRangeR
return nil, fmt.Errorf("error querying generators in Querier.MetricsQueryRange: %w", err)
}

c := traceql.QueryRangeCombiner{}
c, err := traceql.QueryRangeCombinerFor(req, traceql.AggregateModeSum)
if err != nil {
return nil, err
}

for _, result := range lookupResults {
c.Combine(result.response.(*tempopb.QueryRangeResponse))
}
Expand Down Expand Up @@ -98,6 +102,7 @@ func (q *Querier) queryBackend(ctx context.Context, req *tempopb.QueryRangeReque
concurrency = v
}

// Compile the sharded version of the query
eval, err := traceql.NewEngine().CompileMetricsQueryRange(req, dedupe, timeOverlapCutoff, unsafe)
if err != nil {
return nil, err
Expand Down Expand Up @@ -139,10 +144,7 @@ func (q *Querier) queryBackend(ctx context.Context, req *tempopb.QueryRangeReque
return nil, err
}

res, err := eval.Results()
if err != nil {
return nil, err
}
res := eval.Results()

inspectedBytes, spansTotal, _ := eval.Metrics()

Expand Down
Loading
Loading