-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Fixes instant queries in the frontend. #4091
Merged
cyriltovena
merged 2 commits into
grafana:main
from
cyriltovena:fixes-instant-query-frontend
Aug 3, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -10,7 +10,9 @@ import ( | |
jsoniter "github.com/json-iterator/go" | ||
"github.com/opentracing/opentracing-go" | ||
otlog "github.com/opentracing/opentracing-go/log" | ||
"github.com/prometheus/common/model" | ||
|
||
"github.com/grafana/loki/pkg/loghttp" | ||
"github.com/grafana/loki/pkg/logqlmodel/stats" | ||
) | ||
|
||
|
@@ -41,41 +43,91 @@ func (PrometheusExtractor) ResponseWithoutHeaders(resp queryrange.Response) quer | |
// encode encodes a Prometheus response and injects Loki stats. | ||
func (p *LokiPromResponse) encode(ctx context.Context) (*http.Response, error) { | ||
sp := opentracing.SpanFromContext(ctx) | ||
var ( | ||
b []byte | ||
err error | ||
) | ||
if p.Response.Data.ResultType == loghttp.ResultTypeVector { | ||
b, err = p.marshalVector() | ||
} else { | ||
b, err = p.marshalMatrix() | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if sp != nil { | ||
sp.LogFields(otlog.Int("bytes", len(b))) | ||
} | ||
|
||
resp := http.Response{ | ||
Header: http.Header{ | ||
"Content-Type": []string{"application/json"}, | ||
}, | ||
Body: ioutil.NopCloser(bytes.NewBuffer(b)), | ||
StatusCode: http.StatusOK, | ||
} | ||
return &resp, nil | ||
} | ||
|
||
func (p *LokiPromResponse) marshalVector() ([]byte, error) { | ||
vec := make(loghttp.Vector, len(p.Response.Data.Result)) | ||
for i, v := range p.Response.Data.Result { | ||
lbs := make(model.LabelSet, len(v.Labels)) | ||
for _, v := range v.Labels { | ||
lbs[model.LabelName(v.Name)] = model.LabelValue(v.Value) | ||
} | ||
vec[i] = model.Sample{ | ||
Metric: model.Metric(lbs), | ||
Timestamp: model.Time(v.Samples[0].TimestampMs), | ||
Value: model.SampleValue(v.Samples[0].Value), | ||
} | ||
} | ||
return jsonStd.Marshal(struct { | ||
Status string `json:"status"` | ||
Data struct { | ||
ResultType string `json:"resultType"` | ||
Result loghttp.Vector `json:"result"` | ||
Statistics stats.Result `json:"stats,omitempty"` | ||
} `json:"data,omitempty"` | ||
ErrorType string `json:"errorType,omitempty"` | ||
Error string `json:"error,omitempty"` | ||
}{ | ||
Error: p.Response.Error, | ||
Data: struct { | ||
ResultType string `json:"resultType"` | ||
Result loghttp.Vector `json:"result"` | ||
Statistics stats.Result `json:"stats,omitempty"` | ||
}{ | ||
ResultType: loghttp.ResultTypeVector, | ||
Result: vec, | ||
Statistics: p.Statistics, | ||
}, | ||
ErrorType: p.Response.ErrorType, | ||
Status: p.Response.Status, | ||
}) | ||
} | ||
|
||
func (p *LokiPromResponse) marshalMatrix() ([]byte, error) { | ||
// embed response and add statistics. | ||
b, err := jsonStd.Marshal(struct { | ||
return jsonStd.Marshal(struct { | ||
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. same as above |
||
Status string `json:"status"` | ||
Data struct { | ||
queryrange.PrometheusData | ||
Statistics stats.Result `json:"stats"` | ||
Statistics stats.Result `json:"stats,omitempty"` | ||
} `json:"data,omitempty"` | ||
ErrorType string `json:"errorType,omitempty"` | ||
Error string `json:"error,omitempty"` | ||
}{ | ||
Error: p.Response.Error, | ||
Data: struct { | ||
queryrange.PrometheusData | ||
Statistics stats.Result `json:"stats"` | ||
Statistics stats.Result `json:"stats,omitempty"` | ||
}{ | ||
PrometheusData: p.Response.Data, | ||
Statistics: p.Statistics, | ||
}, | ||
ErrorType: p.Response.ErrorType, | ||
Status: p.Response.Status, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if sp != nil { | ||
sp.LogFields(otlog.Int("bytes", len(b))) | ||
} | ||
|
||
resp := http.Response{ | ||
Header: http.Header{ | ||
"Content-Type": []string{"application/json"}, | ||
}, | ||
Body: ioutil.NopCloser(bytes.NewBuffer(b)), | ||
StatusCode: http.StatusOK, | ||
} | ||
return &resp, nil | ||
} |
This file contains 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,159 @@ | ||
package queryrange | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
|
||
"github.com/cortexproject/cortex/pkg/cortexpb" | ||
"github.com/cortexproject/cortex/pkg/querier/queryrange" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/loki/pkg/loghttp" | ||
) | ||
|
||
var emptyStats = `"stats": { | ||
"summary": { | ||
"bytesProcessedPerSecond": 0, | ||
"linesProcessedPerSecond": 0, | ||
"totalBytesProcessed": 0, | ||
"totalLinesProcessed": 0, | ||
"execTime": 0.0 | ||
}, | ||
"store": { | ||
"totalChunksRef": 0, | ||
"totalChunksDownloaded": 0, | ||
"chunksDownloadTime": 0, | ||
"headChunkBytes": 0, | ||
"headChunkLines": 0, | ||
"decompressedBytes": 0, | ||
"decompressedLines": 0, | ||
"compressedBytes": 0, | ||
"totalDuplicates": 0 | ||
}, | ||
"ingester": { | ||
"totalReached": 0, | ||
"totalChunksMatched": 0, | ||
"totalBatches": 0, | ||
"totalLinesSent": 0, | ||
"headChunkBytes": 0, | ||
"headChunkLines": 0, | ||
"decompressedBytes": 0, | ||
"decompressedLines": 0, | ||
"compressedBytes": 0, | ||
"totalDuplicates": 0 | ||
} | ||
}` | ||
|
||
func Test_encodePromResponse(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
resp *LokiPromResponse | ||
want string | ||
}{ | ||
{ | ||
"matrix", | ||
&LokiPromResponse{ | ||
Response: &queryrange.PrometheusResponse{ | ||
Status: string(queryrange.StatusSuccess), | ||
Data: queryrange.PrometheusData{ | ||
ResultType: loghttp.ResultTypeMatrix, | ||
Result: []queryrange.SampleStream{ | ||
{ | ||
Labels: []cortexpb.LabelAdapter{ | ||
{Name: "foo", Value: "bar"}, | ||
}, | ||
Samples: []cortexpb.Sample{ | ||
{Value: 1, TimestampMs: 1000}, | ||
{Value: 1, TimestampMs: 2000}, | ||
}, | ||
}, | ||
{ | ||
Labels: []cortexpb.LabelAdapter{ | ||
{Name: "foo", Value: "buzz"}, | ||
}, | ||
Samples: []cortexpb.Sample{ | ||
{Value: 4, TimestampMs: 1000}, | ||
{Value: 5, TimestampMs: 2000}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
`{ | ||
"status": "success", | ||
"data": { | ||
"resultType": "matrix", | ||
"result": [ | ||
{ | ||
"metric": {"foo": "bar"}, | ||
"values": [[1, "1"],[2, "1"]] | ||
}, | ||
{ | ||
"metric": {"foo": "buzz"}, | ||
"values": [[1, "4"],[2, "5"]] | ||
} | ||
], | ||
` + emptyStats + ` | ||
} | ||
}`, | ||
}, | ||
{ | ||
"vector", | ||
&LokiPromResponse{ | ||
Response: &queryrange.PrometheusResponse{ | ||
Status: string(queryrange.StatusSuccess), | ||
Data: queryrange.PrometheusData{ | ||
ResultType: loghttp.ResultTypeVector, | ||
Result: []queryrange.SampleStream{ | ||
{ | ||
Labels: []cortexpb.LabelAdapter{ | ||
{Name: "foo", Value: "bar"}, | ||
}, | ||
Samples: []cortexpb.Sample{ | ||
{Value: 1, TimestampMs: 1000}, | ||
}, | ||
}, | ||
{ | ||
Labels: []cortexpb.LabelAdapter{ | ||
{Name: "foo", Value: "buzz"}, | ||
}, | ||
Samples: []cortexpb.Sample{ | ||
{Value: 4, TimestampMs: 1000}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
`{ | ||
"status": "success", | ||
"data": { | ||
"resultType": "vector", | ||
"result": [ | ||
{ | ||
"metric": {"foo": "bar"}, | ||
"value": [1, "1"] | ||
}, | ||
{ | ||
"metric": {"foo": "buzz"}, | ||
"value": [1, "4"] | ||
} | ||
], | ||
` + emptyStats + ` | ||
} | ||
}`, | ||
}, | ||
} { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
r, err := tt.resp.encode(context.Background()) | ||
require.NoError(t, err) | ||
b, err := io.ReadAll(r.Body) | ||
require.NoError(t, err) | ||
got := string(b) | ||
require.JSONEq(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should define this struct as a type somewhere and use it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good will do later.