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

Remove mapPoints() utility #175

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions pkg/mimirpb/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/prometheus/prometheus/pkg/exemplar"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse"
"github.com/prometheus/prometheus/promql"

"github.com/grafana/mimir/pkg/util"
)
Expand Down Expand Up @@ -146,6 +147,11 @@ func FromExemplarProtosToExemplars(es []Exemplar) []exemplar.Exemplar {
return result
}

// FromPointsToSamples casts []promql.Point to []Sample. It uses unsafe.
func FromPointsToSamples(points []promql.Point) []Sample {
return *(*[]Sample)(unsafe.Pointer(&points))
}

type byLabel []LabelAdapter

func (s byLabel) Len() int { return len(s) }
Expand Down
8 changes: 8 additions & 0 deletions pkg/mimirpb/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
jsoniter "github.com/json-iterator/go"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse"
"github.com/prometheus/prometheus/promql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -142,3 +143,10 @@ func BenchmarkFromLabelAdaptersToLabelsWithCopy(b *testing.B) {
FromLabelAdaptersToLabelsWithCopy(input)
}
}

func TestFromPointsToSamples(t *testing.T) {
input := []promql.Point{{T: 1, V: 2}, {T: 3, V: 4}}
expected := []Sample{{TimestampMs: 1, Value: 2}, {TimestampMs: 3, Value: 4}}

assert.Equal(t, expected, FromPointsToSamples(input))
}
107 changes: 54 additions & 53 deletions pkg/mimirpb/mimir.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/mimirpb/mimir.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ message LabelPair {
}

message Sample {
double value = 1;
// Fields order MUST match promql.Point so that we can cast types between them.
int64 timestamp_ms = 2;
double value = 1;
}

message MetricMetadata {
Expand Down
22 changes: 6 additions & 16 deletions pkg/querier/queryrange/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ func FromResult(res *promql.Result) ([]SampleStream, error) {
res := make([]SampleStream, 0, len(v))
for _, sample := range v {
res = append(res, SampleStream{
Labels: mapLabels(sample.Metric),
Samples: mapPoints(sample.Point),
Labels: mapLabels(sample.Metric),
Samples: []mimirpb.Sample{{
TimestampMs: sample.Point.T,
Value: sample.Point.V,
}},
})
}
return res, nil
Expand All @@ -52,7 +55,7 @@ func FromResult(res *promql.Result) ([]SampleStream, error) {
for _, series := range v {
res = append(res, SampleStream{
Labels: mapLabels(series.Metric),
Samples: mapPoints(series.Points...),
Samples: mimirpb.FromPointsToSamples(series.Points),
})
}
return res, nil
Expand All @@ -71,19 +74,6 @@ func mapLabels(ls labels.Labels) []mimirpb.LabelAdapter {
return result
}

func mapPoints(pts ...promql.Point) []mimirpb.Sample {
result := make([]mimirpb.Sample, 0, len(pts))

for _, pt := range pts {
result = append(result, mimirpb.Sample{
Value: pt.V,
TimestampMs: pt.T,
})
}

return result
}

// ResponseToSamples is needed to map back from api response to the underlying series data
func ResponseToSamples(resp Response) ([]SampleStream, error) {
promRes, ok := resp.(*PrometheusResponse)
Expand Down