-
Notifications
You must be signed in to change notification settings - Fork 692
Traceql instant query #3859
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
Merged
Merged
Traceql instant query #3859
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1fff188
first working draft
mdisibio 49b6322
Cleanup request time manipulation code
mdisibio 805b0c7
comment
mdisibio e25f5e8
Merge branch 'main' into traceql-instant-query
mdisibio fef0a2f
Update after merge, oops restore accidentally deleted query metrics
mdisibio ee1ba40
Fix request clone for logging
mdisibio 2b7cf80
tweak method signature
mdisibio 5d1be7c
changelog
mdisibio 9763378
Make TrimToOverlap aware of instant query, fix alignment issue on gen…
mdisibio 04ffdf4
Fix test
mdisibio c5334ad
Add streaming version of metrics query instant, add to cli, fix times…
mdisibio b0ae563
Fix typo in QueryInstantResponse name
mdisibio 2489f4d
lint
mdisibio 2777d7d
docs
mdisibio db10952
lint, rename
mdisibio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package frontend | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/go-kit/log" | ||
| "github.com/go-kit/log/level" | ||
| "github.com/gogo/protobuf/jsonpb" | ||
| "github.com/grafana/dskit/user" | ||
| "github.com/grafana/tempo/modules/frontend/combiner" | ||
| "github.com/grafana/tempo/modules/frontend/pipeline" | ||
| "github.com/grafana/tempo/pkg/api" | ||
| "github.com/grafana/tempo/pkg/tempopb" | ||
| ) | ||
|
|
||
| // newMetricsQueryInstantHTTPHandler handles instant queries. Internally these are rewritten as query_range with single step | ||
| // to make use of the existing pipeline. | ||
| func newMetricsQueryInstantHTTPHandler(cfg Config, next pipeline.AsyncRoundTripper[combiner.PipelineResponse], logger log.Logger) http.RoundTripper { | ||
| postSLOHook := metricsSLOPostHook(cfg.Metrics.SLO) | ||
|
|
||
| return pipeline.RoundTripperFunc(func(req *http.Request) (*http.Response, error) { | ||
| tenant, _ := user.ExtractOrgID(req.Context()) | ||
| start := time.Now() | ||
|
|
||
| // Parse request | ||
| i, err := api.ParseQueryInstantRequest(req) | ||
| if err != nil { | ||
| level.Error(logger).Log("msg", "query instant: parse search request failed", "err", err) | ||
| return &http.Response{ | ||
| StatusCode: http.StatusBadRequest, | ||
| Status: http.StatusText(http.StatusBadRequest), | ||
| Body: io.NopCloser(strings.NewReader(err.Error())), | ||
| }, nil | ||
| } | ||
|
|
||
| logQueryInstantRequest(logger, tenant, i) | ||
|
|
||
| // -------------------------------------------------- | ||
| // Rewrite into a query_range request. | ||
| // -------------------------------------------------- | ||
| qr := &tempopb.QueryRangeRequest{ | ||
| Query: i.Query, | ||
| Start: i.Start, | ||
| End: i.End, | ||
| Step: i.End - i.Start, | ||
| } | ||
|
|
||
| // Clone existing to keep it unaltered. | ||
| req = req.Clone(req.Context()) | ||
| req.URL.Path = strings.ReplaceAll(req.URL.Path, api.PathMetricsQueryInstant, api.PathMetricsQueryRange) | ||
| req = api.BuildQueryRangeRequest(req, qr) | ||
|
|
||
| combiner, err := combiner.NewTypedQueryRange(qr, false) | ||
| if err != nil { | ||
| level.Error(logger).Log("msg", "query instant: query range combiner failed", "err", err) | ||
| return &http.Response{ | ||
| StatusCode: http.StatusInternalServerError, | ||
| Status: http.StatusText(http.StatusInternalServerError), | ||
| Body: io.NopCloser(strings.NewReader(err.Error())), | ||
| }, nil | ||
| } | ||
| rt := pipeline.NewHTTPCollector(next, cfg.ResponseConsumers, combiner) | ||
|
|
||
| // Roundtrip the request and look for intermediate failures | ||
| innerResp, err := rt.RoundTrip(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if innerResp != nil && innerResp.StatusCode != http.StatusOK { | ||
| return innerResp, nil | ||
| } | ||
|
|
||
| // -------------------------------------------------- | ||
| // Get the final data and translate to instant. | ||
| // -------------------------------------------------- | ||
| qrResp, err := combiner.GRPCFinal() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| iResp := &tempopb.QueryInstantRespone{ | ||
| Metrics: qrResp.Metrics, | ||
| } | ||
| for _, series := range qrResp.Series { | ||
| if len(series.Samples) == 0 { | ||
| continue | ||
| } | ||
| // Use first value | ||
| iResp.Series = append(iResp.Series, &tempopb.InstantSeries{ | ||
| Labels: series.Labels, | ||
| PromLabels: series.PromLabels, | ||
| Value: series.Samples[0].Value, | ||
| }) | ||
| } | ||
|
|
||
| bodyString, err := new(jsonpb.Marshaler).MarshalToString(iResp) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error marshalling response body: %w", err) | ||
| } | ||
|
|
||
| resp := &http.Response{ | ||
| StatusCode: combiner.StatusCode(), | ||
| Header: http.Header{ | ||
| api.HeaderContentType: {api.HeaderAcceptJSON}, | ||
| }, | ||
| Body: io.NopCloser(strings.NewReader(bodyString)), | ||
| ContentLength: int64(len([]byte(bodyString))), | ||
| } | ||
|
|
||
| duration := time.Since(start) | ||
| var bytesProcessed uint64 | ||
| if iResp.Metrics != nil { | ||
| bytesProcessed = iResp.Metrics.InspectedBytes | ||
| } | ||
| postSLOHook(resp, tenant, bytesProcessed, duration, err) | ||
| logQueryInstantResult(logger, tenant, duration.Seconds(), i, iResp, err) | ||
|
|
||
| return resp, nil | ||
| }) | ||
| } | ||
|
|
||
| func logQueryInstantResult(logger log.Logger, tenantID string, durationSeconds float64, req *tempopb.QueryInstantRequest, resp *tempopb.QueryInstantRespone, err error) { | ||
| if resp == nil { | ||
| level.Info(logger).Log( | ||
| "msg", "query instant results - no resp", | ||
| "tenant", tenantID, | ||
| "duration_seconds", durationSeconds, | ||
| "error", err) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if resp.Metrics == nil { | ||
| level.Info(logger).Log( | ||
| "msg", "query instant results - no metrics", | ||
| "tenant", tenantID, | ||
| "query", req.Query, | ||
| "range_nanos", req.End-req.Start, | ||
| "duration_seconds", durationSeconds, | ||
| "error", err) | ||
| return | ||
| } | ||
|
|
||
| level.Info(logger).Log( | ||
| "msg", "query instant results", | ||
| "tenant", tenantID, | ||
| "query", req.Query, | ||
| "range_nanos", req.End-req.Start, | ||
| "duration_seconds", durationSeconds, | ||
| "request_throughput", float64(resp.Metrics.InspectedBytes)/durationSeconds, | ||
| "total_requests", resp.Metrics.TotalJobs, | ||
| "total_blockBytes", resp.Metrics.TotalBlockBytes, | ||
| "total_blocks", resp.Metrics.TotalBlocks, | ||
| "completed_requests", resp.Metrics.CompletedJobs, | ||
| "inspected_bytes", resp.Metrics.InspectedBytes, | ||
| "inspected_traces", resp.Metrics.InspectedTraces, | ||
| "inspected_spans", resp.Metrics.InspectedSpans, | ||
| "error", err) | ||
| } | ||
|
|
||
| func logQueryInstantRequest(logger log.Logger, tenantID string, req *tempopb.QueryInstantRequest) { | ||
| level.Info(logger).Log( | ||
| "msg", "query instant request", | ||
| "tenant", tenantID, | ||
| "query", req.Query, | ||
| "range_seconds", req.End-req.Start) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.