/
main.go
executable file
·163 lines (144 loc) · 4.86 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"github.com/Shopify/sarama"
"github.com/hashicorp/go-uuid"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
t "github.com/nosinovacao/floki/types"
)
var (
logger = log.With(log.NewJSONLogger(os.Stdout), "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
debug = level.Debug(logger)
info = level.Info(logger)
errorl = level.Error(logger)
lokiURL string
internalBuffer *time.Duration
maxMessages int
)
func sortAndUpload() {
collectionLength := len(collection)
if collectionLength == 0 {
return
}
debug.Log("msg", "sorting and processing", "collectionLength", collectionLength)
copyOfCollection := make(logCollection, collectionLength)
copy(copyOfCollection, collection)
go sendToLoki(copyOfCollection)
collection = make(logCollection, 0, 1000)
}
func handleLogMessage(ch chan []byte) {
info.Log("msg", "starting handleLogMessage")
ticker := time.NewTicker(*internalBuffer)
defer ticker.Stop()
for {
select {
case data := <-ch:
if len(data) < 2 {
// a valid json needs at least two bytes: { and }
continue
}
obj := &t.FilebeatLog{}
err := json.Unmarshal(data, obj)
if err != nil {
errorl.Log("msg", "unable to unmarshall json data", "err", err, "raw_data", string(data))
} else {
//debug.Log("ts", obj.Timestamp, "instance", obj.Host.Name, "ns", obj.Kubernetes.Namespace, "pod", obj.Kubernetes.Pod.Name, "msg", obj.JSON.Log)
collection = append(collection, obj)
}
if len(collection) > maxMessages {
sortAndUpload()
}
case <-ticker.C:
sortAndUpload()
}
}
}
func main() {
var (
lokiAddr = flag.String("lokiurl", "http://loki:3100", "the loki url, default is http://loki:3100")
buffer = flag.Duration("buffer", 5*time.Second, "how much time to buffer before sending to loki")
brokerList = flag.String("brokerList", "awesome.kafka.broker:32400", "the kafka broker list")
clientID = flag.String("clientid", "", "the kafka client id")
groupID = flag.String("groupid", "floki", "the kafka group id")
topicPatterns = flag.String("topicPatterns", "^logging-*", "Regex pattern for kafka topic subscription")
kafkaVersion = flag.String("kafkaVersion", "2.1.1", "kafka cluster's version")
maxBuffer = flag.Uint("maxBuffer", 10000, "max messages before uploading")
)
flag.Parse()
lokiURL = *lokiAddr
internalBuffer = buffer
maxMessages = int(*maxBuffer)
sarama.Logger = saramaLogger{}
config := sarama.NewConfig()
config.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin
if *clientID != "" {
config.ClientID = *clientID
} else {
if hostname, err := os.Hostname(); err == nil {
config.ClientID = hostname
} else if id, err := uuid.GenerateUUID(); err == nil {
config.ClientID = id
} else {
config.ClientID = "floki"
}
}
if version, err := sarama.ParseKafkaVersion(*kafkaVersion); err == nil {
config.Version = version
} else {
config.Version = sarama.V2_1_0_0
}
consumer := Consumer{
ready: make(chan bool),
messageChan: make(chan []byte, 1000),
}
ctx, cancel := context.WithCancel(context.Background())
client, err := sarama.NewConsumerGroup(strings.Split(*brokerList, ","), *groupID, config)
if err != nil {
errorl.Log("msg", "Error creating consumer group client", "err", err)
os.Exit(9)
}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for {
// `Consume` should be called inside an infinite loop, when a
// server-side rebalance happens, the consumer session will need to be
// recreated to get the new claims
if err := client.Consume(ctx, strings.Split(*topicPatterns, ","), &consumer); err != nil {
errorl.Log("msg", "Error from consumer", "err", err)
os.Exit(8)
}
// check if context was cancelled, signaling that the consumer should stop
if ctx.Err() != nil {
return
}
consumer.ready = make(chan bool)
}
}()
<-consumer.ready // Await till the consumer has been set up
info.Log("msg", "Sarama consumer up and running!...")
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ctx.Done():
info.Log("msg", "terminating: context cancelled")
case <-sigterm:
info.Log("msg", "terminating: via signal")
}
cancel()
wg.Wait()
if err = client.Close(); err != nil {
errorl.Log("msg", "Error closing client", "err", err)
os.Exit(6)
}
}