@@ -27,9 +27,11 @@ type Producer struct {
27
27
28
28
// Metrics stores metrics reported from this package
29
29
type Metrics struct {
30
- produceCount adapter.Counter
31
- byteTotal adapter.Counter
32
- errorCount adapter.Counter
30
+ produceCount adapter.Counter
31
+ bytesTotal adapter.Counter
32
+ produceAckCount adapter.Counter
33
+ bytesAckTotal adapter.Counter
34
+ errorCount adapter.Counter
33
35
}
34
36
35
37
var (
@@ -57,9 +59,7 @@ func NewProducer(config *kafka.ConfigMap, namespace string, reliableAckWorkers i
57
59
reliableAck : reliableAckWorkers > 0 ,
58
60
}
59
61
60
- for i := 0 ; i < reliableAckWorkers ; i ++ {
61
- go producer .handleProducerEvents (ackChan )
62
- }
62
+ go producer .handleProducerEvents (ackChan )
63
63
producer .logger .ActivityLog ("kafka_registered" , logrus.LogInfo {"namespace" : namespace })
64
64
return producer , nil
65
65
}
@@ -74,21 +74,18 @@ func (p *Producer) Produce(entry *telemetry.Record) {
74
74
Key : []byte (entry .Vin ),
75
75
Headers : headersFromRecord (entry ),
76
76
Timestamp : time .Now (),
77
+ Opaque : entry ,
77
78
}
78
79
79
80
// Note: confluent kafka supports the concept of one channel per connection, so we could add those here and get rid of reliableAckWorkers
80
81
// ex.: https://github.com/confluentinc/confluent-kafka-go/blob/master/examples/producer_custom_channel_example/producer_custom_channel_example.go#L79
81
- if p .reliableAck {
82
- msg .Opaque = entry
83
- }
84
82
entry .ProduceTime = time .Now ()
85
83
if err := p .kafkaProducer .Produce (msg , nil ); err != nil {
86
84
p .logError (err )
87
85
return
88
86
}
89
-
90
87
metricsRegistry .produceCount .Inc (map [string ]string {"record_type" : entry .TxType })
91
- metricsRegistry .byteTotal .Add (int64 (entry .Length ()), map [string ]string {"record_type" : entry .TxType })
88
+ metricsRegistry .bytesTotal .Add (int64 (entry .Length ()), map [string ]string {"record_type" : entry .TxType })
92
89
}
93
90
94
91
// ReportError to airbrake and logger
@@ -113,9 +110,14 @@ func (p *Producer) handleProducerEvents(ackChan chan (*telemetry.Record)) {
113
110
case kafka.Error :
114
111
p .logError (fmt .Errorf ("producer_error %v" , ev ))
115
112
case * kafka.Message :
116
- record , ok := ev .Opaque .(* telemetry.Record )
117
- if ok {
118
- ackChan <- record
113
+ entry , ok := ev .Opaque .(* telemetry.Record )
114
+ if ! ok {
115
+ continue
116
+ }
117
+ metricsRegistry .produceAckCount .Inc (map [string ]string {"record_type" : entry .TxType })
118
+ metricsRegistry .bytesAckTotal .Add (int64 (entry .Length ()), map [string ]string {"record_type" : entry .TxType })
119
+ if p .reliableAck {
120
+ ackChan <- entry
119
121
}
120
122
default :
121
123
p .logger .ActivityLog ("kafka_event_ignored" , logrus.LogInfo {"event" : ev .String ()})
@@ -139,12 +141,24 @@ func registerMetrics(metricsCollector metrics.MetricCollector) {
139
141
Labels : []string {"record_type" },
140
142
})
141
143
142
- metricsRegistry .byteTotal = metricsCollector .RegisterCounter (adapter.CollectorOptions {
144
+ metricsRegistry .bytesTotal = metricsCollector .RegisterCounter (adapter.CollectorOptions {
143
145
Name : "kafka_produce_total_bytes" ,
144
146
Help : "The number of bytes produced to Kafka." ,
145
147
Labels : []string {"record_type" },
146
148
})
147
149
150
+ metricsRegistry .produceAckCount = metricsCollector .RegisterCounter (adapter.CollectorOptions {
151
+ Name : "kafka_produce_ack_total" ,
152
+ Help : "The number of records produced to Kafka for which we got an ACK." ,
153
+ Labels : []string {"record_type" },
154
+ })
155
+
156
+ metricsRegistry .bytesAckTotal = metricsCollector .RegisterCounter (adapter.CollectorOptions {
157
+ Name : "kafka_produce_ack_total_bytes" ,
158
+ Help : "The number of bytes produced to Kafka for which we got an ACK." ,
159
+ Labels : []string {"record_type" },
160
+ })
161
+
148
162
metricsRegistry .errorCount = metricsCollector .RegisterCounter (adapter.CollectorOptions {
149
163
Name : "kafka_err" ,
150
164
Help : "The number of errors while producing to Kafka." ,
0 commit comments