github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/prometheus/remote/test/write.go (about) 1 // Copyright (c) 2018 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 test 22 23 import ( 24 "bytes" 25 "io" 26 "time" 27 28 "github.com/m3db/m3/src/query/generated/proto/prompb" 29 30 "github.com/golang/protobuf/proto" 31 "github.com/golang/snappy" 32 "github.com/prometheus/common/model" 33 "github.com/stretchr/testify/require" 34 ) 35 36 // GeneratePromWriteRequest generates a Prometheus remote 37 // write request. 38 func GeneratePromWriteRequest() *prompb.WriteRequest { 39 req := &prompb.WriteRequest{ 40 Timeseries: []prompb.TimeSeries{{ 41 Labels: []prompb.Label{ 42 {Name: []byte(model.MetricNameLabel), Value: []byte("first")}, 43 {Name: []byte("foo"), Value: []byte("bar")}, 44 {Name: []byte("biz"), Value: []byte("baz")}, 45 }, 46 Samples: []prompb.Sample{ 47 {Value: 1.0, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}, 48 {Value: 2.0, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}, 49 }, 50 }, 51 { 52 Labels: []prompb.Label{ 53 {Name: []byte(model.MetricNameLabel), Value: []byte("second")}, 54 {Name: []byte("foo"), Value: []byte("qux")}, 55 {Name: []byte("bar"), Value: []byte("baz")}, 56 }, 57 Samples: []prompb.Sample{ 58 {Value: 3.0, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}, 59 {Value: 4.0, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}, 60 }, 61 }}, 62 } 63 return req 64 } 65 66 // GeneratePromWriteRequestBody generates a Prometheus remote 67 // write request body. 68 func GeneratePromWriteRequestBody( 69 t require.TestingT, 70 req *prompb.WriteRequest, 71 ) io.Reader { 72 return bytes.NewReader(GeneratePromWriteRequestBodyBytes(t, req)) 73 } 74 75 // GeneratePromWriteRequestBodyBytes generates a Prometheus remote 76 // write request body. 77 func GeneratePromWriteRequestBodyBytes( 78 t require.TestingT, 79 req *prompb.WriteRequest, 80 ) []byte { 81 data, err := proto.Marshal(req) 82 require.NoError(t, err) 83 84 compressed := snappy.Encode(nil, data) 85 return compressed 86 }