67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package loki_client
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const LOG_ENTRIES_CHAN_SIZE = 5000
|
|
|
|
type LogLevel int
|
|
|
|
const (
|
|
DEBUG LogLevel = iota
|
|
INFO LogLevel = iota
|
|
WARN LogLevel = iota
|
|
ERROR LogLevel = iota
|
|
DISABLE LogLevel = iota
|
|
)
|
|
|
|
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 {
|
|
Debugf(format string, args ...interface{})
|
|
Infof(format string, args ...interface{})
|
|
Warnf(format string, args ...interface{})
|
|
Errorf(format string, args ...interface{})
|
|
Panicf(format string, args ...interface{})
|
|
Shutdown()
|
|
}
|
|
|
|
// http.Client wrapper for adding new methods, particularly sendJsonReq
|
|
type httpClient struct {
|
|
parent http.Client
|
|
}
|
|
|
|
// 推送日志到服务器
|
|
func (client *httpClient) sendJsonReq(url string, reqBody []byte) (resp *http.Response, resBody []byte, err error) {
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
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
|
|
}
|