-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_group.go
47 lines (38 loc) · 1.29 KB
/
consumer_group.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
package otelsaramax
import (
"github.com/Shopify/sarama"
)
type consumerGroupHandler struct {
sarama.ConsumerGroupHandler
consumerInfo ConsumerInfo
cfg config
}
// ConsumeClaim wraps the session and claim to add instruments for messages.
// It implements parts of `ConsumerGroupHandler`.
func (h *consumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
// Wrap claim
dispatcher := newConsumerMessagesDispatcherWrapper(claim, h.consumerInfo, h.cfg)
go dispatcher.Run()
claim = &consumerGroupClaim{
ConsumerGroupClaim: claim,
dispatcher: dispatcher,
}
return h.ConsumerGroupHandler.ConsumeClaim(session, claim)
}
// WrapConsumerGroupHandler wraps a sarama.ConsumerGroupHandler causing each received
// message to be traced.
func WrapConsumerGroupHandler(handler sarama.ConsumerGroupHandler, consumerInfo ConsumerInfo, opts ...Option) sarama.ConsumerGroupHandler {
cfg := newConfig(opts...)
return &consumerGroupHandler{
ConsumerGroupHandler: handler,
consumerInfo: consumerInfo,
cfg: cfg,
}
}
type consumerGroupClaim struct {
sarama.ConsumerGroupClaim
dispatcher consumerMessagesDispatcher
}
func (c *consumerGroupClaim) Messages() <-chan *sarama.ConsumerMessage {
return c.dispatcher.Messages()
}