@@ -5,11 +5,8 @@ import (
55 "fmt"
66 "github.com/goto/raccoon/serialization"
77 "google.golang.org/protobuf/types/known/timestamppb"
8- "strings"
9- "sync/atomic"
10- "time"
11-
128 "gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
9+ "strings"
1310 // Importing librd to make it work on vendor mode
1411 _ "gopkg.in/confluentinc/confluent-kafka-go.v1/kafka/librdkafka"
1512
@@ -24,8 +21,6 @@ const (
2421 errLargeMessageSize = "Broker: Message size too large" //error msg while producing a message which is larger than message.max.bytes config
2522)
2623
27- var DeliveryEventCount int64
28-
2924// KafkaProducer Produce data to kafka synchronously
3025type KafkaProducer interface {
3126 // ProduceBulk message to kafka. Block until all messages are sent. Return array of error. Order is not guaranteed.
@@ -38,31 +33,34 @@ func NewKafka() (*Kafka, error) {
3833 return & Kafka {}, err
3934 }
4035 return & Kafka {
41- kp : kp ,
42- flushInterval : config .PublisherKafka .FlushInterval ,
43- topicFormat : config .EventDistribution .PublisherPattern ,
44- deliveryReportInterval : config .PublisherKafka .DeliveryReportInterval ,
45- deliveryReportTopic : config .PublisherKafka .DeliveryReportTopic ,
36+ kp : kp ,
37+ flushInterval : config .PublisherKafka .FlushInterval ,
38+ topicFormat : config .EventDistribution .PublisherPattern ,
39+ clickstreamStats : ClickstreamStats {
40+ config .PublisherKafka .ClickstreamStatsTopicName ,
41+ },
4642 }, nil
4743}
4844
49- func NewKafkaFromClient (client Client , flushInterval int , topicFormat string , deliveryReportInterval time. Duration ,
50- topicName string ) * Kafka {
45+ func NewKafkaFromClient (client Client , flushInterval int , topicFormat string ,
46+ clickstreamStats ClickstreamStats ) * Kafka {
5147 return & Kafka {
52- kp : client ,
53- flushInterval : flushInterval ,
54- topicFormat : topicFormat ,
55- deliveryReportInterval : deliveryReportInterval ,
56- deliveryReportTopic : topicName ,
48+ kp : client ,
49+ flushInterval : flushInterval ,
50+ topicFormat : topicFormat ,
51+ clickstreamStats : clickstreamStats ,
5752 }
5853}
5954
6055type Kafka struct {
61- kp Client
62- flushInterval int
63- topicFormat string
64- deliveryReportInterval time.Duration
65- deliveryReportTopic string
56+ kp Client
57+ flushInterval int
58+ topicFormat string
59+ clickstreamStats ClickstreamStats
60+ }
61+
62+ type ClickstreamStats struct {
63+ topicName string
6664}
6765
6866// ProduceBulk messages to kafka. Block until all messages are sent. Return array of error. Order of Errors is guaranteed.
@@ -102,7 +100,7 @@ func (pr *Kafka) ProduceBulk(events []*pb.Event, connGroup string, deliveryChann
102100 totalProcessed ++
103101 }
104102 // Wait for deliveryChannel as many as processed
105- localSuccesses := int64 (0 )
103+ totalEventCount := int64 (0 )
106104 for i := 0 ; i < totalProcessed ; i ++ {
107105 d := <- deliveryChannel
108106 m := d .(* kafka.Message )
@@ -114,13 +112,17 @@ func (pr *Kafka) ProduceBulk(events []*pb.Event, connGroup string, deliveryChann
114112 order := m .Opaque .(int )
115113 errors [order ] = m .TopicPartition .Error
116114 } else {
117- localSuccesses ++
115+ totalEventCount ++
118116 }
119117 }
120-
121- // Single atomic update per batch
122- if localSuccesses > 0 {
123- atomic .AddInt64 (& DeliveryEventCount , localSuccesses )
118+ // send the total successful delivered event count to kafka topic
119+ if totalEventCount > 0 {
120+ //build kafka message for total message count
121+ msg := & pb.TotalEventCountMessage {
122+ EventTimestamp : timestamppb .Now (),
123+ EventCount : int32 (totalEventCount ),
124+ }
125+ pr .produceClickstreamStats (pr .clickstreamStats .topicName , msg )
124126 }
125127
126128 if allNil (errors ) {
@@ -129,7 +131,8 @@ func (pr *Kafka) ProduceBulk(events []*pb.Event, connGroup string, deliveryChann
129131 return BulkError {Errors : errors }
130132}
131133
132- func (pr * Kafka ) produceTotalEventMessage (topicName string , event * pb.TotalEventCountMessage ) error {
134+ // produceClickstreamStats : method to produce stats to a kafka topic
135+ func (pr * Kafka ) produceClickstreamStats (topicName string , event * pb.TotalEventCountMessage ) error {
133136 value , err := serialization .SerializeProto (event )
134137 if err != nil {
135138 return fmt .Errorf ("failed to serialize proto: %w" , err )
@@ -173,25 +176,6 @@ func (pr *Kafka) ReportStats() {
173176 }
174177}
175178
176- func (pr * Kafka ) ReportDeliveryEventCount () {
177- ticker := time .NewTicker (pr .deliveryReportInterval )
178- defer ticker .Stop ()
179-
180- for range ticker .C {
181- // read the value
182- eventCount := atomic .LoadInt64 (& DeliveryEventCount )
183- //build kafka message
184- msg := & pb.TotalEventCountMessage {
185- EventTimestamp : timestamppb .Now (),
186- EventCount : int32 (eventCount ),
187- }
188- //produce to kafka
189- pr .produceTotalEventMessage (pr .deliveryReportTopic , msg )
190- //reset the counter
191- atomic .StoreInt64 (& DeliveryEventCount , 0 )
192- }
193- }
194-
195179// Close wait for outstanding messages to be delivered within given flush interval timeout.
196180func (pr * Kafka ) Close () int {
197181 remaining := pr .kp .Flush (pr .flushInterval )
0 commit comments