/
protoclient.go
154 lines (132 loc) · 2.98 KB
/
protoclient.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
147
148
149
150
151
152
153
154
package promtail
import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/golang/snappy"
"github.com/habakke/promtail-client/logproto"
"golang.org/x/oauth2"
"log"
"net/http"
"sync"
"time"
)
type protoLogEntry struct {
entry *logproto.Entry
level LogLevel
extraLabels LabelSet
}
type clientProto struct {
config *ClientConfig
quit chan struct{}
entries chan protoLogEntry
waitGroup sync.WaitGroup
client httpClient
token *oauth2.Token
sent int
buffered int
}
func NewClientProto(conf ClientConfig, parent *http.Client) (Client, error) {
client := clientProto{
config: &conf,
quit: make(chan struct{}),
entries: make(chan protoLogEntry, LOG_ENTRIES_CHAN_SIZE),
client: httpClient{
parent: *parent,
},
}
client.waitGroup.Add(1)
go client.run()
return &client, nil
}
func (c *clientProto) Log(line string, level LogLevel, extraLabels LabelSet) {
now := time.Now().UnixNano()
c.entries <- protoLogEntry{
entry: &logproto.Entry{
Timestamp: ×tamp.Timestamp{
Seconds: now / int64(time.Second),
Nanos: int32(now % int64(time.Second)),
},
Line: line,
},
level: level,
extraLabels: extraLabels.Copy(),
}
}
func (c *clientProto) Shutdown() {
close(c.quit)
c.waitGroup.Wait()
}
func (c *clientProto) Sent() int {
return c.sent
}
func (c *clientProto) Buffered() int {
return c.buffered
}
func (c *clientProto) run() {
batch := NewBatchMap()
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:
c.buffered++
batch.Append(entry.extraLabels, entry.entry)
batchSize++
if batchSize >= c.config.BatchEntriesNumber {
c.send(batch)
batch = NewBatchMap()
batchSize = 0
maxWait.Reset(c.config.BatchWait)
}
case <-maxWait.C:
if batchSize > 0 {
c.send(batch)
batch = NewBatchMap()
batchSize = 0
}
maxWait.Reset(c.config.BatchWait)
}
}
}
func (c *clientProto) send(batchMap BatchMap) {
var streams []*logproto.Stream
// Generate some statistics :)
batchSize := 0
defer func() {
c.buffered = c.buffered - batchSize
c.sent = c.sent + batchSize
}()
for _, batch := range batchMap {
batchSize += len(batch.entries)
streams = append(streams, &logproto.Stream{
Labels: c.config.Labels.WithExtras(batch.labels).String(),
Entries: batch.entries,
})
}
req := logproto.PushRequest{
Streams: streams,
}
buf, err := proto.Marshal(&req)
if err != nil {
log.Printf("unable to marshal")
return
}
buf = snappy.Encode(nil, buf)
resp, body, err := c.client.sendJsonReq("POST", c.config.PushURL, "application/x-protobuf", buf)
if err != nil {
log.Printf("unable to send an http request: %v", err)
return
}
if resp.StatusCode != 204 {
log.Printf("unexpected http status code: %d, message: %s\n", resp.StatusCode, body)
return
}
}