-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (143 loc) · 4.06 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
package main
import (
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/getsentry/sentry-go"
"github.com/goccy/go-json"
"os"
"os/signal"
"speedy/pkg"
"sync"
)
func main() {
configurator, err := pkg.NewViperConfigurator()
if err != nil {
panic(err)
}
// Grab hostName
hostName, err := os.Hostname()
if err != nil {
pkg.SugaredLogger.Warn("Failed to get hostname! Using generic name.")
hostName = "Speedy"
}
pkg.SugaredLogger.Infof("Using hostname %s", hostName)
// Init logging
config := configurator.GetConfig()
pkg.InitLoggingWithParams(config.LoggingLevel, "console", "")
// Init sentry
err = sentry.Init(sentry.ClientOptions{
// Either set your DSN here or set the SENTRY_DSN environment variable.
Dsn: config.SentryDSN,
// Either set environment and release here or set the SENTRY_ENVIRONMENT
// and SENTRY_RELEASE environment variables.
Environment: hostName,
Release: "speedy-gonzales",
})
if err != nil {
pkg.SugaredLogger.Error("failed to init Sentry.")
}
// Init kafka
pkg.SugaredLogger.Infof("Using config:\n %s", config.ToPrettyJson())
kafkaConsumer, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": config.KafkaBoostrapServers,
"group.id": config.KafkaGroupId,
"auto.offset.reset": config.KafkaOffsetReset,
"socket.timeout.ms": "300000",
})
if err != nil {
panic(err)
}
err = kafkaConsumer.SubscribeTopics(config.SubscribeTopics, nil)
if err != nil {
pkg.SugaredLogger.Errorf("failed to subscribe: %s", err)
sentry.CaptureException(err)
panic(err)
}
defer func(c *kafka.Consumer) {
err := c.Close()
if err != nil {
panic(err)
}
}(kafkaConsumer)
pkg.SugaredLogger.Info("Initializing")
// Init Sink & Pusher
var lokiClient = pkg.LokiClientFactoryCreate(config.LokiPushMode, config.LokiPushUrl)
var speedyPusher = pkg.NewPusher(lokiClient, config.BufferMaxBatchSize, config.BufferMaxBytesSize)
go speedyPusher.RunForever()
isRunning := true
var waitGroup sync.WaitGroup
for i := 0; i < 1; i++ {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
for isRunning {
ev := kafkaConsumer.Poll(config.KafkaPollingTimeoutMs)
switch event := ev.(type) {
case kafka.AssignedPartitions:
err := kafkaConsumer.Assign(event.Partitions)
if err != nil {
pkg.SugaredLogger.Error(err)
sentry.CaptureException(err)
return
}
case kafka.RevokedPartitions:
err := kafkaConsumer.Unassign()
if err != nil {
pkg.SugaredLogger.Error(err)
sentry.CaptureException(err)
return
}
case *kafka.Message:
var messageMap = make(map[string]interface{})
err := json.Unmarshal(event.Value, &messageMap)
if err != nil {
pkg.SugaredLogger.Error(err)
continue
}
flattenMap := pkg.FlattenMap(messageMap)
flattenMapString, err := json.Marshal(flattenMap)
if err != nil {
pkg.SugaredLogger.Error(err)
continue
}
labelsMap := map[string]string{
"key": *event.TopicPartition.Topic,
}
clientId := (*flattenMap)["clientID"]
clientIdStr, ok := clientId.(string)
if ok {
labelsMap["clientId"] = clientIdStr
}
// Size of clientId key and val
labelsSize := 8 + len(clientIdStr)
speedyPusher.DataChannel <- pkg.LokiStream{
Labels: labelsMap,
Values: [][]string{{"", string(flattenMapString)}},
Size: len(event.Value) + labelsSize,
}
case kafka.PartitionEOF:
pkg.SugaredLogger.Info()
case kafka.Error:
if event.Code() == kafka.ErrTimedOut {
pkg.SugaredLogger.Debugf("Consumer error: %v\n", err)
} else {
// The client will automatically try to recover from all errors.
pkg.SugaredLogger.Warnf("Consumer error: %v\n", err)
sentry.CaptureException(err)
}
default:
}
}
}()
}
go func() {
// Handle SIGINT
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Block until a signal is received.
<-c
pkg.SugaredLogger.Info("Received SIGINT, shutting down.")
isRunning = false
}()
waitGroup.Wait()
pkg.SugaredLogger.Info("Exiting.")
}