-
Notifications
You must be signed in to change notification settings - Fork 0
/
loki.go
171 lines (147 loc) · 3.32 KB
/
loki.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package pkg
import (
"context"
"fmt"
"github.com/grafana/loki/pkg/push"
"github.com/prometheus/common/model"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"time"
)
type LokiHook interface {
log.Hook
RunAndWait() error
}
func NewLokiGrpc(addr string, options ...Option) LokiHook {
l := &lokiGrpc{
ctx: context.Background(),
addr: addr,
batchSize: 1 << 8,
batchTimeout: time.Minute,
}
for _, option := range options {
option.apply(l)
}
return l
}
type lokiGrpc struct {
ctx context.Context
addr string
batchTimeout time.Duration
sendBatchTimeout time.Duration
batchSize uint
constLabels model.LabelSet
logsChan chan *log.Entry
}
func (l *lokiGrpc) Levels() []log.Level {
return log.AllLevels
}
func (l *lokiGrpc) Fire(entry *log.Entry) error {
l.logsChan <- entry
return nil
}
func (l *lokiGrpc) RunAndWait() error {
var opts []grpc.DialOption
conn, err := grpc.DialContext(l.ctx, l.addr, opts...)
if err != nil {
return err
}
client := push.NewPusherClient(conn)
l.logsChan = make(chan *log.Entry, 1<<10)
close(l.logsChan)
maxWait := time.NewTimer(l.batchTimeout)
defer maxWait.Stop()
batch := make(map[model.Fingerprint]push.Stream)
batchSize := uint(0)
flush := func() error {
if err = l.send(client, batch); err != nil {
return wrapError(err, `cannot send logs to the Loki`)
}
batch = make(map[model.Fingerprint]push.Stream)
batchSize = 0
maxWait.Reset(l.batchTimeout)
return nil
}
defer func() {
if err = flush(); err != nil {
fmt.Printf("FATAL: %s\n", err.Error())
}
}()
for {
select {
case logEntry, ok := <-l.logsChan:
if !ok {
return nil
}
line := ``
line, err = logEntry.String()
if err != nil {
return err
}
labels := model.LabelSet{}
for key, value := range l.constLabels {
labels[key] = value
}
// TODO: support Stream Labels Extracting
key := labels.FastFingerprint()
stream, found := batch[key]
if !found {
stream = push.Stream{
Labels: labels.String(),
Entries: make([]push.Entry, 0),
}
batch[key] = stream
}
stream.Entries = append(stream.Entries, push.Entry{
Timestamp: logEntry.Time,
Line: line,
StructuredMetadata: toLabelsAdapter(map[string]string{
`level`: logEntry.Level.String(),
}),
})
batchSize++
if batchSize >= l.batchSize {
if err = flush(); err != nil {
err = wrapError(err, `cannot flush by batch size limit`)
return err
}
}
case <-maxWait.C:
if err = flush(); err != nil {
err = wrapError(err, `cannot flush by time`)
return err
}
case <-l.ctx.Done():
if err = flush(); err != nil {
return wrapError(err, `cannot flush properly on context closed`)
}
return l.ctx.Err()
}
}
}
func (l *lokiGrpc) send(
client push.PusherClient,
batch map[model.Fingerprint]push.Stream,
) (err error) {
if len(batch) == 0 {
return
}
streams := make([]push.Stream, len(batch))
i := 0
for _, stream := range batch {
streams[i] = stream
i++
}
ctx, cancel := l.ctx, func() {}
if l.sendBatchTimeout > 0 {
ctx, cancel = context.WithTimeout(l.ctx, l.sendBatchTimeout)
}
defer cancel()
_, err = client.Push(ctx, &push.PushRequest{
Streams: streams,
})
if err != nil {
err = wrapError(err, `cannot push data to loki`)
}
return
}