|
| 1 | +// Copyright 2016 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package remote |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "io/ioutil" |
| 19 | + "net/http" |
| 20 | + |
| 21 | + "github.com/gogo/protobuf/proto" |
| 22 | + "github.com/golang/snappy" |
| 23 | + "github.com/prometheus/common/model" |
| 24 | + |
| 25 | + "github.com/prometheus/prometheus/storage/metric" |
| 26 | +) |
| 27 | + |
| 28 | +// DecodeReadRequest reads a remote.Request from a http.Request. |
| 29 | +func DecodeReadRequest(r *http.Request) (*ReadRequest, error) { |
| 30 | + compressed, err := ioutil.ReadAll(r.Body) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + reqBuf, err := snappy.Decode(nil, compressed) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + var req ReadRequest |
| 41 | + if err := proto.Unmarshal(reqBuf, &req); err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + return &req, nil |
| 46 | +} |
| 47 | + |
| 48 | +// EncodReadResponse writes a remote.Response to a http.ResponseWriter. |
| 49 | +func EncodReadResponse(resp *ReadResponse, w http.ResponseWriter) error { |
| 50 | + data, err := proto.Marshal(resp) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + w.Header().Set("Content-Type", "application/x-protobuf") |
| 56 | + w.Header().Set("Content-Encoding", "snappy") |
| 57 | + |
| 58 | + compressed := snappy.Encode(nil, data) |
| 59 | + _, err = w.Write(compressed) |
| 60 | + return err |
| 61 | +} |
| 62 | + |
| 63 | +// ToWriteRequest converts an array of samples into a WriteRequest proto. |
| 64 | +func ToWriteRequest(samples []*model.Sample) *WriteRequest { |
| 65 | + req := &WriteRequest{ |
| 66 | + Timeseries: make([]*TimeSeries, 0, len(samples)), |
| 67 | + } |
| 68 | + |
| 69 | + for _, s := range samples { |
| 70 | + ts := TimeSeries{ |
| 71 | + Labels: ToLabelPairs(s.Metric), |
| 72 | + Samples: []*Sample{ |
| 73 | + { |
| 74 | + Value: float64(s.Value), |
| 75 | + TimestampMs: int64(s.Timestamp), |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + req.Timeseries = append(req.Timeseries, &ts) |
| 80 | + } |
| 81 | + |
| 82 | + return req |
| 83 | +} |
| 84 | + |
| 85 | +// ToQuery builds a Query proto. |
| 86 | +func ToQuery(from, to model.Time, matchers []*metric.LabelMatcher) (*Query, error) { |
| 87 | + ms, err := toLabelMatchers(matchers) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + return &Query{ |
| 93 | + StartTimestampMs: int64(from), |
| 94 | + EndTimestampMs: int64(to), |
| 95 | + Matchers: ms, |
| 96 | + }, nil |
| 97 | +} |
| 98 | + |
| 99 | +// FromQuery unpacks a Query proto. |
| 100 | +func FromQuery(req *Query) (model.Time, model.Time, []*metric.LabelMatcher, error) { |
| 101 | + matchers, err := fromLabelMatchers(req.Matchers) |
| 102 | + if err != nil { |
| 103 | + return 0, 0, nil, err |
| 104 | + } |
| 105 | + from := model.Time(req.StartTimestampMs) |
| 106 | + to := model.Time(req.EndTimestampMs) |
| 107 | + return from, to, matchers, nil |
| 108 | +} |
| 109 | + |
| 110 | +// ToQueryResult builds a QueryResult proto. |
| 111 | +func ToQueryResult(matrix model.Matrix) *QueryResult { |
| 112 | + resp := &QueryResult{} |
| 113 | + for _, ss := range matrix { |
| 114 | + ts := TimeSeries{ |
| 115 | + Labels: ToLabelPairs(ss.Metric), |
| 116 | + Samples: make([]*Sample, 0, len(ss.Values)), |
| 117 | + } |
| 118 | + for _, s := range ss.Values { |
| 119 | + ts.Samples = append(ts.Samples, &Sample{ |
| 120 | + Value: float64(s.Value), |
| 121 | + TimestampMs: int64(s.Timestamp), |
| 122 | + }) |
| 123 | + } |
| 124 | + resp.Timeseries = append(resp.Timeseries, &ts) |
| 125 | + } |
| 126 | + return resp |
| 127 | +} |
| 128 | + |
| 129 | +// FromQueryResult unpacks a QueryResult proto. |
| 130 | +func FromQueryResult(resp *QueryResult) model.Matrix { |
| 131 | + m := make(model.Matrix, 0, len(resp.Timeseries)) |
| 132 | + for _, ts := range resp.Timeseries { |
| 133 | + var ss model.SampleStream |
| 134 | + ss.Metric = FromLabelPairs(ts.Labels) |
| 135 | + ss.Values = make([]model.SamplePair, 0, len(ts.Samples)) |
| 136 | + for _, s := range ts.Samples { |
| 137 | + ss.Values = append(ss.Values, model.SamplePair{ |
| 138 | + Value: model.SampleValue(s.Value), |
| 139 | + Timestamp: model.Time(s.TimestampMs), |
| 140 | + }) |
| 141 | + } |
| 142 | + m = append(m, &ss) |
| 143 | + } |
| 144 | + |
| 145 | + return m |
| 146 | +} |
| 147 | + |
| 148 | +func toLabelMatchers(matchers []*metric.LabelMatcher) ([]*LabelMatcher, error) { |
| 149 | + result := make([]*LabelMatcher, 0, len(matchers)) |
| 150 | + for _, matcher := range matchers { |
| 151 | + var mType MatchType |
| 152 | + switch matcher.Type { |
| 153 | + case metric.Equal: |
| 154 | + mType = MatchType_EQUAL |
| 155 | + case metric.NotEqual: |
| 156 | + mType = MatchType_NOT_EQUAL |
| 157 | + case metric.RegexMatch: |
| 158 | + mType = MatchType_REGEX_MATCH |
| 159 | + case metric.RegexNoMatch: |
| 160 | + mType = MatchType_REGEX_NO_MATCH |
| 161 | + default: |
| 162 | + return nil, fmt.Errorf("invalid matcher type") |
| 163 | + } |
| 164 | + result = append(result, &LabelMatcher{ |
| 165 | + Type: mType, |
| 166 | + Name: string(matcher.Name), |
| 167 | + Value: string(matcher.Value), |
| 168 | + }) |
| 169 | + } |
| 170 | + return result, nil |
| 171 | +} |
| 172 | + |
| 173 | +func fromLabelMatchers(matchers []*LabelMatcher) ([]*metric.LabelMatcher, error) { |
| 174 | + result := make(metric.LabelMatchers, 0, len(matchers)) |
| 175 | + for _, matcher := range matchers { |
| 176 | + var mtype metric.MatchType |
| 177 | + switch matcher.Type { |
| 178 | + case MatchType_EQUAL: |
| 179 | + mtype = metric.Equal |
| 180 | + case MatchType_NOT_EQUAL: |
| 181 | + mtype = metric.NotEqual |
| 182 | + case MatchType_REGEX_MATCH: |
| 183 | + mtype = metric.RegexMatch |
| 184 | + case MatchType_REGEX_NO_MATCH: |
| 185 | + mtype = metric.RegexNoMatch |
| 186 | + default: |
| 187 | + return nil, fmt.Errorf("invalid matcher type") |
| 188 | + } |
| 189 | + matcher, err := metric.NewLabelMatcher(mtype, model.LabelName(matcher.Name), model.LabelValue(matcher.Value)) |
| 190 | + if err != nil { |
| 191 | + return nil, err |
| 192 | + } |
| 193 | + result = append(result, matcher) |
| 194 | + } |
| 195 | + return result, nil |
| 196 | +} |
| 197 | + |
| 198 | +// ToLabelPairs builds a []LabelPair from a model.Metric |
| 199 | +func ToLabelPairs(metric model.Metric) []*LabelPair { |
| 200 | + labelPairs := make([]*LabelPair, 0, len(metric)) |
| 201 | + for k, v := range metric { |
| 202 | + labelPairs = append(labelPairs, &LabelPair{ |
| 203 | + Name: string(k), |
| 204 | + Value: string(v), |
| 205 | + }) |
| 206 | + } |
| 207 | + return labelPairs |
| 208 | +} |
| 209 | + |
| 210 | +// FromLabelPairs unpack a []LabelPair to a model.Metric |
| 211 | +func FromLabelPairs(labelPairs []*LabelPair) model.Metric { |
| 212 | + metric := make(model.Metric, len(labelPairs)) |
| 213 | + for _, l := range labelPairs { |
| 214 | + metric[model.LabelName(l.Name)] = model.LabelValue(l.Value) |
| 215 | + } |
| 216 | + return metric |
| 217 | +} |
0 commit comments