-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
59 lines (47 loc) · 1.06 KB
/
client.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
package omloader
import (
"bytes"
"fmt"
"net/http"
"github.com/golang/snappy"
"github.com/prometheus/prometheus/prompb"
)
type Sender interface {
Send(timeSeriesSlice []prompb.TimeSeries) error
}
type HttpSenderConfig struct {
URL string
}
type httpSender struct {
HttpSenderConfig
}
func NewHttpSender(config HttpSenderConfig) Sender {
return &httpSender{
HttpSenderConfig: config,
}
}
func (c *httpSender) Send(timeSeriesSlice []prompb.TimeSeries) error {
writeRequest := &prompb.WriteRequest{
Timeseries: timeSeriesSlice,
}
wrBuf, err := writeRequest.Marshal()
if err != nil {
return err
}
buf := snappy.Encode(nil, wrBuf)
req, err := http.NewRequest("POST", c.URL, bytes.NewBuffer(buf))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set("Content-Encoding", "snappy")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
return nil
}