Skip to content

Commit

Permalink
Remove mapPoints() utility (#175)
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Pracucci <marco@pracucci.com>
  • Loading branch information
pracucci committed Aug 26, 2021
1 parent 21ab329 commit c0a7b23
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 70 deletions.
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

0 comments on commit c0a7b23

Please sign in to comment.