forked from afiskon/promtail-client
/
common.go
52 lines (42 loc) · 1.05 KB
/
common.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
package promtail
import (
"bytes"
"io/ioutil"
"net/http"
"time"
)
const LOG_ENTRIES_CHAN_SIZE = 5000
type ClientConfig struct {
// E.g. http://localhost:3100/api/prom/push
PushURL string
// E.g. "{job=\"somejob\"}"
Labels string
BatchWait time.Duration
BatchEntriesNumber int
}
type Client interface {
Log(message string)
Shutdown()
}
// http.Client wrapper for adding new methods, particularly sendJsonReq
type httpClient struct {
parent http.Client
}
// A bit more convenient method for sending requests to the HTTP server
func (client *httpClient) sendJsonReq(method, url string, ctype string, reqBody []byte) (resp *http.Response, resBody []byte, err error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(reqBody))
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", ctype)
resp, err = client.parent.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
resBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
return resp, resBody, nil
}