Skip to content

Commit 3147932

Browse files
feat : emit total delivery message count
1 parent b697a95 commit 3147932

4 files changed

Lines changed: 212 additions & 0 deletions

File tree

app/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func StartServer(ctx context.Context, cancel context.CancelFunc, shutdown chan b
3737
workerPool.StartWorkers()
3838
go kPublisher.ReportStats()
3939
go reportProcMetrics()
40+
go kPublisher.ReportDeliveryEventCount()
4041
// create signal channel at startup
4142
signalChan := make(chan os.Signal, 1)
4243
signal.Notify(signalChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

proto/TotalEventCount.pb.go

Lines changed: 163 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/TotalEventCount.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
import "google/protobuf/timestamp.proto";
3+
4+
package github.com.goto.raccoon;
5+
6+
option go_package = "github.com/goto/raccoon/proto";
7+
8+
message TotalEventCountMessage {
9+
google.protobuf.Timestamp event_timestamp = 1; //partition column
10+
int32 event_count = 2;
11+
}

publisher/kafka.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ package publisher
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/goto/raccoon/proto"
7+
"github.com/goto/raccoon/serialization"
8+
"google.golang.org/protobuf/types/known/timestamppb"
69
"strings"
10+
"sync/atomic"
11+
"time"
712

813
"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
914
// Importing librd to make it work on vendor mode
@@ -20,6 +25,8 @@ const (
2025
errLargeMessageSize = "Broker: Message size too large" //error msg while producing a message which is larger than message.max.bytes config
2126
)
2227

28+
var DeliveryEventCount atomic.Int64
29+
2330
// KafkaProducer Produce data to kafka synchronously
2431
type KafkaProducer interface {
2532
// ProduceBulk message to kafka. Block until all messages are sent. Return array of error. Order is not guaranteed.
@@ -100,6 +107,7 @@ func (pr *Kafka) ProduceBulk(events []*pb.Event, connGroup string, deliveryChann
100107
order := m.Opaque.(int)
101108
errors[order] = m.TopicPartition.Error
102109
}
110+
DeliveryEventCount.Add(1)
103111
}
104112

105113
if allNil(errors) {
@@ -108,6 +116,17 @@ func (pr *Kafka) ProduceBulk(events []*pb.Event, connGroup string, deliveryChann
108116
return BulkError{Errors: errors}
109117
}
110118

119+
func (pr *Kafka) ProduceTotalEventMessage(topicName string, event *proto.TotalEventCountMessage) error {
120+
value, err := serialization.SerializeProto(event)
121+
if err != nil {
122+
return fmt.Errorf("failed to serialize proto: %w", err)
123+
}
124+
return pr.kp.Produce(&kafka.Message{
125+
TopicPartition: kafka.TopicPartition{Topic: &topicName, Partition: kafka.PartitionAny},
126+
Value: value,
127+
}, nil)
128+
}
129+
111130
func (pr *Kafka) ReportStats() {
112131
for v := range pr.kp.Events() {
113132
switch e := v.(type) {
@@ -141,6 +160,24 @@ func (pr *Kafka) ReportStats() {
141160
}
142161
}
143162

163+
func (pr *Kafka) ReportDeliveryEventCount() {
164+
ticker := time.NewTicker(1 * time.Minute)
165+
defer ticker.Stop()
166+
167+
for range ticker.C {
168+
fmt.Println("get the current value:", DeliveryEventCount.Load())
169+
//build kafka message
170+
msg := &proto.TotalEventCountMessage{
171+
EventTimestamp: timestamppb.Now(),
172+
EventCount: int32(DeliveryEventCount.Load()),
173+
}
174+
//produce to kafka
175+
pr.ProduceTotalEventMessage("clickstream-total-event", msg)
176+
//reset the counter
177+
DeliveryEventCount.Store(0)
178+
}
179+
}
180+
144181
// Close wait for outstanding messages to be delivered within given flush interval timeout.
145182
func (pr *Kafka) Close() int {
146183
remaining := pr.kp.Flush(pr.flushInterval)

0 commit comments

Comments
 (0)