github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/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 "io/ioutil" 27 "time" 28 29 "github.com/m3db/m3/src/query/generated/proto/prompb" 30 31 "github.com/golang/protobuf/proto" 32 "github.com/golang/snappy" 33 "github.com/prometheus/common/model" 34 "github.com/stretchr/testify/require" 35 ) 36 37 // GeneratePromWriteRequest generates a Prometheus remote 38 // write request. 39 func GeneratePromWriteRequest() *prompb.WriteRequest { 40 req := &prompb.WriteRequest{ 41 Timeseries: []prompb.TimeSeries{ 42 { 43 Labels: []prompb.Label{ 44 {Name: []byte(model.MetricNameLabel), Value: []byte("first")}, 45 {Name: []byte("foo"), Value: []byte("bar")}, 46 {Name: []byte("biz"), Value: []byte("baz")}, 47 }, 48 Samples: []prompb.Sample{ 49 {Value: 1.0, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}, 50 {Value: 2.0, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}, 51 }, 52 }, 53 { 54 Labels: []prompb.Label{ 55 {Name: []byte(model.MetricNameLabel), Value: []byte("second")}, 56 {Name: []byte("foo"), Value: []byte("qux")}, 57 {Name: []byte("bar"), Value: []byte("baz")}, 58 }, 59 Samples: []prompb.Sample{ 60 {Value: 3.0, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}, 61 {Value: 4.0, Timestamp: time.Now().UnixNano() / int64(time.Millisecond)}, 62 }, 63 }, 64 }, 65 } 66 return req 67 } 68 69 // GeneratePromWriteRequestBody generates a Prometheus remote 70 // write request body. 71 func GeneratePromWriteRequestBody( 72 t require.TestingT, 73 req *prompb.WriteRequest, 74 ) io.Reader { 75 return bytes.NewReader(GeneratePromWriteRequestBodyBytes(t, req)) 76 } 77 78 // GeneratePromWriteRequestBodyBytes generates a Prometheus remote 79 // write request body. 80 func GeneratePromWriteRequestBodyBytes( 81 t require.TestingT, 82 req *prompb.WriteRequest, 83 ) []byte { 84 data, err := proto.Marshal(req) 85 require.NoError(t, err) 86 87 compressed := snappy.Encode(nil, data) 88 return compressed 89 } 90 91 // ReadPromWriteRequestBody reads a Prometheus remote 92 // write request body. 93 func ReadPromWriteRequestBody( 94 t require.TestingT, 95 body io.Reader, 96 ) *prompb.WriteRequest { 97 require.NotNil(t, body) 98 99 data, err := ioutil.ReadAll(body) 100 require.NoError(t, err) 101 102 decoded, err := snappy.Decode(nil, data) 103 require.NoError(t, err) 104 105 req := &prompb.WriteRequest{} 106 require.NoError(t, proto.Unmarshal(decoded, req)) 107 108 return req 109 }