Skip to content

Commit f186f47

Browse files
feat: add kafka processing lateny
1 parent 32a0859 commit f186f47

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

publisher/kafka.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"strings"
7+
"time"
78

89
"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
910
// Importing librd to make it work on vendor mode
@@ -57,7 +58,10 @@ func (pr *Kafka) ProduceBulk(events []*pb.Event, connGroup string, deliveryChann
5758
message := &kafka.Message{
5859
Value: event.EventBytes,
5960
TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
60-
Opaque: order,
61+
Opaque: struct {
62+
Order int
63+
Sent time.Time
64+
}{order, time.Now()},
6165
}
6266

6367
err := pr.kp.Produce(message, deliveryChannel)
@@ -78,12 +82,22 @@ func (pr *Kafka) ProduceBulk(events []*pb.Event, connGroup string, deliveryChann
7882
for i := 0; i < totalProcessed; i++ {
7983
d := <-deliveryChannel
8084
m := d.(*kafka.Message)
85+
// Extract timestamp from Opaque
86+
opaque := m.Opaque.(struct {
87+
Order int
88+
Sent time.Time
89+
})
90+
deliveryLatency := time.Since(opaque.Sent)
91+
order := opaque.Order
8192
if m.TopicPartition.Error != nil {
8293
eventType := events[i].Type
8394
metrics.Decrement("kafka_messages_delivered_total", fmt.Sprintf("success=true,conn_group=%s,event_type=%s", connGroup, eventType))
8495
metrics.Increment("kafka_messages_delivered_total", fmt.Sprintf("success=false,conn_group=%s,event_type=%s", connGroup, eventType))
85-
order := m.Opaque.(int)
8696
errors[order] = m.TopicPartition.Error
97+
} else {
98+
// Record metric
99+
metrics.Timing("kafka_delivery_latency_ms", deliveryLatency.Milliseconds(),
100+
fmt.Sprintf("conn_group=%s,event_type=%s", connGroup, events[order].Type))
87101
}
88102
}
89103

publisher/kafka_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"testing"
7+
"time"
78

89
pb "buf.build/gen/go/gotocompany/proton/protocolbuffers/go/gotocompany/raccoon/v1beta1"
910
"github.com/goto/raccoon/logger"
@@ -52,6 +53,13 @@ func TestKafka_ProduceBulk(suite *testing.T) {
5253
Offset: 0,
5354
Error: nil,
5455
},
56+
Opaque: struct {
57+
Order int
58+
Sent time.Time
59+
}{
60+
Order: 0, // or proper index
61+
Sent: time.Now(),
62+
},
5563
}
5664
}()
5765
})
@@ -75,6 +83,13 @@ func TestKafka_ProduceBulk(suite *testing.T) {
7583
Offset: 0,
7684
Error: nil,
7785
},
86+
Opaque: struct {
87+
Order int
88+
Sent time.Time
89+
}{
90+
Order: 0, // or proper index
91+
Sent: time.Now(),
92+
},
7893
}
7994
}()
8095
}).Once()
@@ -111,7 +126,13 @@ func TestKafka_ProduceBulk(suite *testing.T) {
111126
Offset: 0,
112127
Error: fmt.Errorf("timeout"),
113128
},
114-
Opaque: 1,
129+
Opaque: struct {
130+
Order int
131+
Sent time.Time
132+
}{
133+
Order: 1, // or proper index
134+
Sent: time.Now(),
135+
},
115136
}
116137
}()
117138
}).Once()

0 commit comments

Comments
 (0)