github.com/m3db/m3@v1.5.0/src/query/storage/promremote/query_coverter.go (about)

     1  // Copyright (c) 2021  Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package promremote
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/golang/snappy"
    27  	"github.com/pkg/errors"
    28  	"github.com/prometheus/prometheus/prompb"
    29  
    30  	"github.com/m3db/m3/src/query/storage"
    31  )
    32  
    33  var errNilQuery = errors.New("received nil query")
    34  
    35  func convertAndEncodeWriteQuery(query *storage.WriteQuery) ([]byte, error) {
    36  	if query == nil {
    37  		return []byte{}, errNilQuery
    38  	}
    39  	promQuery := convertWriteQuery(query)
    40  	data, err := promQuery.Marshal()
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	return snappy.Encode(nil, data), nil
    45  }
    46  
    47  func convertWriteQuery(query *storage.WriteQuery) *prompb.WriteRequest {
    48  	if query == nil {
    49  		return nil
    50  	}
    51  
    52  	ourLabels := storage.TagsToPromLabels(query.Tags())
    53  	labels := make([]prompb.Label, 0, len(ourLabels))
    54  	for _, tag := range ourLabels {
    55  		labels = append(labels, prompb.Label{
    56  			Name:  string(tag.Name),
    57  			Value: string(tag.Value),
    58  		})
    59  	}
    60  
    61  	samples := make([]prompb.Sample, 0, len(query.Datapoints()))
    62  	for _, dp := range query.Datapoints() {
    63  		samples = append(samples, prompb.Sample{
    64  			Value:     dp.Value,
    65  			Timestamp: dp.Timestamp.ToNormalizedTime(time.Millisecond),
    66  		})
    67  	}
    68  
    69  	return &prompb.WriteRequest{
    70  		Timeseries: []prompb.TimeSeries{
    71  			{
    72  				Labels:  labels,
    73  				Samples: samples,
    74  			},
    75  		},
    76  	}
    77  }