-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
65 lines (54 loc) · 1.45 KB
/
stats.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
package readers
import (
"context"
"encoding/json"
"time"
"github.com/georgeyord/go-url-shortener/pkg/kafka"
"github.com/rs/zerolog/log"
kafkalib "github.com/segmentio/kafka-go"
_ "github.com/segmentio/kafka-go/snappy"
)
const retriesThreshold = 10
const sleepOnError = 2
func RunStatsReader(topic, groupId string) {
var reader *kafkalib.Reader
for usedRetries := 0; usedRetries < retriesThreshold; usedRetries++ {
reader = kafka.NewReader(topic, groupId)
for {
msg, err := reader.ReadMessage(context.Background())
if err != nil {
log.Error().Str("topic", topic).Err(err).Msg("Error while receiving message")
break
}
parse(msg, topic)
}
log.Warn().Int("duration", sleepOnError).Int("retry", usedRetries).Msg("Sleeping...")
reader.Close()
time.Sleep(sleepOnError * time.Second)
}
defer reader.Close()
}
func parse(msg kafkalib.Message, topic string) {
jsonValue := msg.Value
var value map[string]string
errJson := json.Unmarshal(jsonValue, &value)
if errJson != nil {
log.Error().
Str("v", string(jsonValue)).
Str("topic", topic).
Err(errJson).
Msg("Error while decoding json value")
return
}
log.Info().
Str("v", string(jsonValue)).
Str("short", value["short"]).
Str("long", value["long"]).
Str("host", value["host"]).
Str("clientIP", value["clientIP"]).
Time("time", msg.Time).
Str("topic", msg.Topic).
Int("partition", msg.Partition).
Int64("offset", msg.Offset).
Msg("Message received")
}