forked from afiskon/promtail-client
/
jsonclient.go
146 lines (123 loc) · 3.05 KB
/
jsonclient.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package promtail
import (
"encoding/json"
"fmt"
"log"
"sync"
"time"
)
type jsonLogEntry struct {
Ts time.Time `json:"ts"`
Line string `json:"line"`
level LogLevel // not used in JSON
}
type promtailStream struct {
Labels string `json:"labels"`
Entries []*jsonLogEntry `json:"entries"`
}
type promtailMsg struct {
Streams []promtailStream `json:"streams"`
}
type clientJson struct {
config *ClientConfig
quit chan struct{}
entries chan *jsonLogEntry
waitGroup sync.WaitGroup
client httpClient
}
func NewClientJson(conf ClientConfig) (Client, error) {
client := clientJson{
config: &conf,
quit: make(chan struct{}),
entries: make(chan *jsonLogEntry, LOG_ENTRIES_CHAN_SIZE),
client: httpClient{},
}
client.waitGroup.Add(1)
go client.run()
return &client, nil
}
func (c *clientJson) Debugf(format string, args ...interface{}) {
c.log(format, DEBUG, "Debug: ", args...)
}
func (c *clientJson) Infof(format string, args ...interface{}) {
c.log(format, INFO, "Info: ", args...)
}
func (c *clientJson) Warnf(format string, args ...interface{}) {
c.log(format, WARN, "Warn: ", args...)
}
func (c *clientJson) Errorf(format string, args ...interface{}) {
c.log(format, ERROR, "Error: ", args...)
}
func (c *clientJson) log(format string, level LogLevel, prefix string, args ...interface{}) {
if (level >= c.config.SendLevel) || (level >= c.config.PrintLevel) {
c.entries <- &jsonLogEntry{
Ts: time.Now(),
Line: fmt.Sprintf(prefix+format, args...),
level: level,
}
}
}
func (c *clientJson) Shutdown() {
close(c.quit)
c.waitGroup.Wait()
}
func (c *clientJson) run() {
var batch []*jsonLogEntry
batchSize := 0
maxWait := time.NewTimer(c.config.BatchWait)
defer func() {
if batchSize > 0 {
c.send(batch)
}
c.waitGroup.Done()
}()
for {
select {
case <-c.quit:
return
case entry := <-c.entries:
if entry.level >= c.config.PrintLevel {
log.Print(entry.Line)
}
if entry.level >= c.config.SendLevel {
batch = append(batch, entry)
batchSize++
if batchSize >= c.config.BatchEntriesNumber {
c.send(batch)
batch = []*jsonLogEntry{}
batchSize = 0
maxWait.Reset(c.config.BatchWait)
}
}
case <-maxWait.C:
if batchSize > 0 {
c.send(batch)
batch = []*jsonLogEntry{}
batchSize = 0
}
maxWait.Reset(c.config.BatchWait)
}
}
}
func (c *clientJson) send(entries []*jsonLogEntry) {
var streams []promtailStream
streams = append(streams, promtailStream{
Labels: c.config.Labels,
Entries: entries,
})
msg := promtailMsg{Streams: streams}
jsonMsg, err := json.Marshal(msg)
if err != nil {
log.Printf("promtail.ClientJson: unable to marshal a JSON document: %s\n", err)
return
}
resp, body, err := c.client.sendJsonReq("POST", c.config.PushURL, "application/json", jsonMsg)
if err != nil {
log.Printf("promtail.ClientJson: unable to send an HTTP request: %s\n", err)
return
}
if resp.StatusCode != 204 {
log.Printf("promtail.ClientJson: Unexpected HTTP status code: %d, message: %s\n", resp.StatusCode, body)
return
}
}