Move a couple of the encoding/decoding functions into codec.go

This commit is contained in:
Tom Wilkie 2017-10-23 16:56:47 +01:00
parent 08b7328669
commit 56820726fa
3 changed files with 30 additions and 30 deletions

View file

@ -1,4 +1,4 @@
// Copyright 2016 The Prometheus Authors // Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
@ -17,6 +17,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"sort"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/golang/snappy" "github.com/golang/snappy"
@ -69,7 +70,7 @@ func ToWriteRequest(samples []*model.Sample) *prompb.WriteRequest {
for _, s := range samples { for _, s := range samples {
ts := prompb.TimeSeries{ ts := prompb.TimeSeries{
Labels: ToLabelPairs(s.Metric), Labels: MetricToLabelProtos(s.Metric),
Samples: []*prompb.Sample{ Samples: []*prompb.Sample{
{ {
Value: float64(s.Value), Value: float64(s.Value),
@ -113,7 +114,7 @@ func ToQueryResult(matrix model.Matrix) *prompb.QueryResult {
resp := &prompb.QueryResult{} resp := &prompb.QueryResult{}
for _, ss := range matrix { for _, ss := range matrix {
ts := prompb.TimeSeries{ ts := prompb.TimeSeries{
Labels: ToLabelPairs(ss.Metric), Labels: MetricToLabelProtos(ss.Metric),
Samples: make([]*prompb.Sample, 0, len(ss.Values)), Samples: make([]*prompb.Sample, 0, len(ss.Values)),
} }
for _, s := range ss.Values { for _, s := range ss.Values {
@ -132,7 +133,7 @@ func FromQueryResult(resp *prompb.QueryResult) model.Matrix {
m := make(model.Matrix, 0, len(resp.Timeseries)) m := make(model.Matrix, 0, len(resp.Timeseries))
for _, ts := range resp.Timeseries { for _, ts := range resp.Timeseries {
var ss model.SampleStream var ss model.SampleStream
ss.Metric = FromLabelPairs(ts.Labels) ss.Metric = LabelProtosToMetric(ts.Labels)
ss.Values = make([]model.SamplePair, 0, len(ts.Samples)) ss.Values = make([]model.SamplePair, 0, len(ts.Samples))
for _, s := range ts.Samples { for _, s := range ts.Samples {
ss.Values = append(ss.Values, model.SamplePair{ ss.Values = append(ss.Values, model.SamplePair{
@ -196,8 +197,8 @@ func fromLabelMatchers(matchers []*prompb.LabelMatcher) ([]*labels.Matcher, erro
return result, nil return result, nil
} }
// ToLabelPairs builds a []LabelPair from a model.Metric // MetricToLabelProtos builds a []*prompb.Label from a model.Metric
func ToLabelPairs(metric model.Metric) []*prompb.Label { func MetricToLabelProtos(metric model.Metric) []*prompb.Label {
labelPairs := make([]*prompb.Label, 0, len(metric)) labelPairs := make([]*prompb.Label, 0, len(metric))
for k, v := range metric { for k, v := range metric {
labelPairs = append(labelPairs, &prompb.Label{ labelPairs = append(labelPairs, &prompb.Label{
@ -208,11 +209,31 @@ func ToLabelPairs(metric model.Metric) []*prompb.Label {
return labelPairs return labelPairs
} }
// FromLabelPairs unpack a []LabelPair to a model.Metric // LabelProtosToMetric unpack a []*prompb.Label to a model.Metric
func FromLabelPairs(labelPairs []*prompb.Label) model.Metric { func LabelProtosToMetric(labelPairs []*prompb.Label) model.Metric {
metric := make(model.Metric, len(labelPairs)) metric := make(model.Metric, len(labelPairs))
for _, l := range labelPairs { for _, l := range labelPairs {
metric[model.LabelName(l.Name)] = model.LabelValue(l.Value) metric[model.LabelName(l.Name)] = model.LabelValue(l.Value)
} }
return metric return metric
} }
func labelProtosToLabels(labelPairs []*prompb.Label) labels.Labels {
result := make(labels.Labels, 0, len(labelPairs))
for _, l := range labelPairs {
result = append(result, labels.Label{
Name: l.Name,
Value: l.Value,
})
}
sort.Sort(result)
return result
}
func labelsToMetric(ls labels.Labels) model.Metric {
metric := make(model.Metric, len(ls))
for _, l := range ls {
metric[model.LabelName(l.Name)] = model.LabelValue(l.Value)
}
return metric
}

View file

@ -76,7 +76,7 @@ func (q *querier) Select(matchers ...*labels.Matcher) storage.SeriesSet {
series := make([]storage.Series, 0, len(res)) series := make([]storage.Series, 0, len(res))
for _, ts := range res { for _, ts := range res {
labels := labelPairsToLabels(ts.Labels) labels := labelProtosToLabels(ts.Labels)
removeLabels(&labels, added) removeLabels(&labels, added)
if err := validateLabelsAndMetricName(labels); err != nil { if err := validateLabelsAndMetricName(labels); err != nil {
return errSeriesSet{err: err} return errSeriesSet{err: err}
@ -93,18 +93,6 @@ func (q *querier) Select(matchers ...*labels.Matcher) storage.SeriesSet {
} }
} }
func labelPairsToLabels(labelPairs []*prompb.Label) labels.Labels {
result := make(labels.Labels, 0, len(labelPairs))
for _, l := range labelPairs {
result = append(result, labels.Label{
Name: l.Name,
Value: l.Value,
})
}
sort.Sort(result)
return result
}
type byLabel []storage.Series type byLabel []storage.Series
func (a byLabel) Len() int { return len(a) } func (a byLabel) Len() int { return len(a) }

View file

@ -38,15 +38,6 @@ func (s *Storage) Add(l labels.Labels, t int64, v float64) (uint64, error) {
return 0, nil return 0, nil
} }
func labelsToMetric(ls labels.Labels) model.Metric {
metric := make(model.Metric, len(ls))
for _, l := range ls {
metric[model.LabelName(l.Name)] = model.LabelValue(l.Value)
}
return metric
}
// AddFast implements storage.Appender.
func (s *Storage) AddFast(l labels.Labels, _ uint64, t int64, v float64) error { func (s *Storage) AddFast(l labels.Labels, _ uint64, t int64, v float64) error {
_, err := s.Add(l, t, v) _, err := s.Add(l, t, v)
return err return err