This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
kafka_tracker.go
102 lines (87 loc) · 2.54 KB
/
kafka_tracker.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
package rex
import (
"time"
"github.com/Shopify/sarama"
"github.com/juju/loggo"
"github.com/remerge/rex/kafka"
"github.com/remerge/rex/log"
"github.com/remerge/rex/rollbar"
)
type KafkaTracker struct {
*BaseTracker
Client *kafka.Client
Fast *kafka.Producer
FastTimeout time.Duration
Safe *kafka.Producer
SafeTimeout time.Duration
log loggo.Logger
}
func NewKafkaTracker(broker string, metadata *EventMetadata) (_ Tracker, err error) {
self := &KafkaTracker{
BaseTracker: NewBaseTracker(metadata),
FastTimeout: 10 * time.Millisecond,
SafeTimeout: 100 * time.Millisecond,
log: log.GetLogger("rex.tracker"),
}
self.Client, err = kafka.NewClient("tracker", broker)
if err != nil {
return nil, err
}
self.Fast, err = self.Client.NewFastProducer(nil)
if err != nil {
self.Close()
return nil, err
}
self.Safe, err = self.Client.NewSafeProducer()
if err != nil {
self.Close()
return nil, err
}
return self, nil
}
func (self *KafkaTracker) Close() {
if self.Safe != nil {
self.log.Infof("shutting down safe producer")
self.Safe.Shutdown()
}
if self.Fast != nil {
self.log.Infof("shutting down fast producer")
self.Fast.Shutdown()
}
rollbar.Error(rollbar.WARN, self.Client.Close())
self.log.Infof("tracker is stopped")
}
func (self *KafkaTracker) FastMessage(topic string, value []byte) error {
if self.log.IsTraceEnabled() {
self.log.Tracef("topic=%s value=%s", topic, string(value))
}
return self.Fast.SendMessage(&sarama.ProducerMessage{
Topic: topic,
Value: sarama.ByteEncoder(value),
})
}
func (self *KafkaTracker) FastEvent(topic string, e EventBase, full bool) error {
self.AddMetadata(e, full)
return self.FastMessage(topic, self.Encode(e))
}
func (self *KafkaTracker) FastEventMap(topic string, event map[string]interface{}, full bool) error {
self.AddMetadataMap(event, full)
return self.FastMessage(topic, self.EncodeMap(event))
}
func (self *KafkaTracker) SafeMessage(topic string, value []byte) error {
if self.log.IsTraceEnabled() {
self.log.Tracef("topic=%s value=%s", topic, string(value))
}
return self.Safe.SendMessage(&sarama.ProducerMessage{
Topic: topic,
Value: sarama.ByteEncoder(value),
})
}
func (self *KafkaTracker) SafeEvent(topic string, event EventBase, full bool) error {
self.AddMetadata(event, full)
return self.SafeMessage(topic, self.Encode(event))
}
func (self *KafkaTracker) SafeEventMap(topic string, event map[string]interface{}, full bool) error {
self.AddMetadataMap(event, full)
return self.SafeMessage(topic, self.EncodeMap(event))
}