-
Notifications
You must be signed in to change notification settings - Fork 460
/
otlp_writer.go
81 lines (66 loc) · 2.07 KB
/
otlp_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: AGPL-3.0-only
package continuoustest
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"time"
"github.com/grafana/dskit/flagext"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/prompb"
"github.com/grafana/mimir/pkg/distributor"
)
type otlpHTTPWriter struct {
httpClient *http.Client
writeBaseEndpoint flagext.URLValue
writeBatchSize int
writeTimeout time.Duration
}
func (pw *otlpHTTPWriter) sendWriteRequest(ctx context.Context, req *prompb.WriteRequest) (int, error) {
metricRequest := distributor.TimeseriesToOTLPRequest(req.Timeseries, nil)
rawBytes, err := metricRequest.MarshalProto()
if err != nil {
return 0, err
}
compressedBytes, err := compress(rawBytes)
if err != nil {
return 0, err
}
ctx, cancel := context.WithTimeout(ctx, pw.writeTimeout)
defer cancel()
httpReq, err := http.NewRequestWithContext(ctx, "POST", pw.writeBaseEndpoint.String()+"/otlp/v1/metrics", bytes.NewReader(compressedBytes))
if err != nil {
// Errors from NewRequest are from unparseable URLs, so are not
// recoverable.
return 0, err
}
httpReq.Header.Add("Content-Encoding", "gzip")
httpReq.Header.Add("Accept-Encoding", "gzip")
httpReq.Header.Set("Content-Type", "application/x-protobuf")
httpResp, err := pw.httpClient.Do(httpReq)
if err != nil {
return 0, err
}
defer httpResp.Body.Close()
if httpResp.StatusCode/100 != 2 {
truncatedBody, err := io.ReadAll(io.LimitReader(httpResp.Body, maxErrMsgLen))
if err != nil {
return httpResp.StatusCode, errors.Wrapf(err, "server returned HTTP status %s and client failed to read response body", httpResp.Status)
}
return httpResp.StatusCode, fmt.Errorf("server returned HTTP status %s and body %q (truncated to %d bytes)", httpResp.Status, string(truncatedBody), maxErrMsgLen)
}
return httpResp.StatusCode, nil
}
func compress(input []byte) ([]byte, error) {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
_, err := gz.Write(input)
if err != nil {
return nil, errors.Wrap(err, "unable to compress data")
}
gz.Close()
return b.Bytes(), nil
}