-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
295 lines (255 loc) · 8.54 KB
/
writer.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package zkafka
//go:generate mockgen -package mock_confluent -destination=./mocks/confluent/kafka_producer.go . KafkaProducer
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
"go.opentelemetry.io/otel/trace"
)
// Writer is the convenient interface for kafka KWriter
type Writer interface {
// Write sends messages to kafka with message key set as nil.
// The value arg passed to this method is marshalled by
// the configured formatter and used as the kafka message's value
Write(ctx context.Context, value any, opts ...WriteOption) (Response, error)
// WriteKey send message to kafka with a defined keys.
// The value arg passed to this method is marshalled by
// the configured formatter and used as the kafka message's value
WriteKey(ctx context.Context, key string, value any, opts ...WriteOption) (Response, error)
// WriteRaw sends messages to kafka. The caller is responsible for marshalling the data themselves.
WriteRaw(ctx context.Context, key *string, value []byte, opts ...WriteOption) (Response, error)
Close()
}
// static type checking for the convenient Writer interface
var _ Writer = (*KWriter)(nil)
type KafkaProducer interface {
Produce(msg *kafka.Message, deliveryChan chan kafka.Event) error
Close()
}
var _ KafkaProducer = (*kafka.Producer)(nil)
// KWriter is a kafka producer. KWriter should be initialized from the Client to be usable
type KWriter struct {
mu sync.Mutex
producer KafkaProducer
topicConfig ProducerTopicConfig
formatter kFormatter
logger Logger
tracer trace.Tracer
p propagation.TextMapPropagator
lifecycle LifecycleHooks
isClosed bool
}
type keyValuePair struct {
key *string
value any
}
type writerArgs struct {
cfg Config
pCfg ProducerTopicConfig
producerProvider confluentProducerProvider
f kFormatter
l Logger
t trace.Tracer
p propagation.TextMapPropagator
hooks LifecycleHooks
opts []WriterOption
}
func newWriter(args writerArgs) (*KWriter, error) {
conf := args.cfg
topicConfig := args.pCfg
producer := args.producerProvider
formatter := args.f
confluentConfig, err := makeProducerConfig(conf, topicConfig)
if err != nil {
return nil, err
}
p, err := producer(confluentConfig)
if err != nil {
return nil, err
}
w := &KWriter{
producer: p,
topicConfig: topicConfig,
formatter: formatter,
logger: args.l,
tracer: args.t,
p: args.p,
lifecycle: args.hooks,
}
s := WriterSettings{}
for _, opt := range args.opts {
opt(&s)
}
if s.f != nil {
w.formatter = s.f
}
return w, nil
}
// Write sends messages to kafka with message key set as nil.
// The value arg passed to this method is marshalled by
// the configured formatter and used as the kafka message's value
func (w *KWriter) Write(ctx context.Context, value any, opts ...WriteOption) (Response, error) {
return w.write(ctx, keyValuePair{value: value}, opts...)
}
// WriteKey send message to kafka with a defined keys.
// The value arg passed to this method is marshalled by
// the configured formatter and used as the kafka message's value
func (w *KWriter) WriteKey(ctx context.Context, key string, value any, opts ...WriteOption) (Response, error) {
return w.write(ctx, keyValuePair{
key: &key,
value: value,
}, opts...)
}
// WriteRaw allows you to write messages using a lower level API than Write and WriteKey.
// WriteRaw raw doesn't use a formatter to marshall the value data and instead takes the bytes as is and places them
// as the value for the kafka message
// It's convenient for forwarding message in dead letter operations.
func (w *KWriter) WriteRaw(ctx context.Context, key *string, value []byte, opts ...WriteOption) (Response, error) {
kafkaMessage := makeProducerMessageRaw(ctx, w.topicConfig.Topic, key, value)
for _, opt := range opts {
opt.apply(&kafkaMessage)
}
if w.lifecycle.PreWrite != nil {
resp, err := w.lifecycle.PreWrite(ctx, LifecyclePreWriteMeta{})
if err != nil {
w.logger.Warnw(ctx, "Lifecycle pre-write failed", "error", err)
}
kafkaMessage = addHeaders(kafkaMessage, resp.Headers)
}
w.logger.Debugw(ctx, "write message", "message", kafkaMessage)
span := w.startSpan(ctx, &kafkaMessage)
defer span.End()
deliveryChan := make(chan kafka.Event)
begin := time.Now()
err := w.producer.Produce(&kafkaMessage, deliveryChan)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return Response{}, fmt.Errorf("error writing message: %w", err)
}
// wait on callback channel for kafka broker to ack written message
e := <-deliveryChan
w.lifecyclePostAck(ctx, begin)
m, ok := e.(*kafka.Message)
if !ok {
return Response{}, errors.New("unexpected message delivered on kafka delivery channel")
}
span.SetAttributes(
semconv.MessagingMessageIDKey.Int64(int64(m.TopicPartition.Offset)),
semconv.MessagingKafkaDestinationPartitionKey.Int64(int64(m.TopicPartition.Partition)),
)
if m.TopicPartition.Error != nil {
w.logger.Debugw(ctx, "Delivery failed", "error", m.TopicPartition.Error)
return Response{}, fmt.Errorf("failed to produce kafka message: %w", m.TopicPartition.Error)
}
return Response{Partition: m.TopicPartition.Partition, Offset: int64(m.TopicPartition.Offset)}, nil
}
func (w *KWriter) lifecyclePostAck(ctx context.Context, begin time.Time) {
if w.lifecycle.PostAck != nil {
lcMeta := LifecyclePostAckMeta{
Topic: w.topicConfig.Topic,
ProduceTime: begin,
}
if err := w.lifecycle.PostAck(ctx, lcMeta); err != nil {
w.logger.Warnw(ctx, "Lifecycle post-ack failed", "error", err, "meta", lcMeta)
}
}
}
func (w *KWriter) startSpan(ctx context.Context, msg *kafka.Message) spanWrapper {
if msg == nil || w.tracer == nil {
return spanWrapper{}
}
topic := ""
if msg.TopicPartition.Topic != nil {
topic = *msg.TopicPartition.Topic
}
opts := []trace.SpanStartOption{
trace.WithAttributes(
semconv.MessagingOperationPublish,
semconv.MessagingDestinationName(topic),
semconv.MessagingKafkaMessageKey(string(msg.Key)),
semconv.MessagingKafkaMessageOffset(int(msg.TopicPartition.Offset)),
semconv.MessagingKafkaDestinationPartition(int(msg.TopicPartition.Partition)),
),
trace.WithSpanKind(trace.SpanKindProducer),
}
operationName := "zkafka.write"
ctx, span := w.tracer.Start(ctx, operationName, opts...)
// Inject the current span into the original message, so it can be used to
// propagate the span.
if w.p != nil {
carrier := &kMsgCarrier{msg: msg}
w.p.Inject(ctx, carrier)
}
return spanWrapper{span: span}
}
func (w *KWriter) write(ctx context.Context, msg keyValuePair, opts ...WriteOption) (Response, error) {
value, err := w.marshall(ctx, msg.value, w.topicConfig.SchemaRegistry.Serialization.Schema)
if err != nil {
return Response{}, err
}
return w.WriteRaw(ctx, msg.key, value, opts...)
}
func (w *KWriter) marshall(_ context.Context, value any, schema string) ([]byte, error) {
if w.formatter == nil {
return nil, errors.New("formatter or confluent formatter is not supplied to produce kafka message")
}
return w.formatter.marshall(marshReq{
topic: w.topicConfig.Topic,
v: value,
schema: schema,
})
}
// Close terminates the writer gracefully and mark it as closed
func (w *KWriter) Close() {
w.mu.Lock()
defer w.mu.Unlock()
w.producer.Close()
w.isClosed = true
}
type WriterSettings struct {
f kFormatter
}
// WriterOption is a function that modify the writer configurations
type WriterOption func(*WriterSettings)
// WFormatterOption sets the formatter for this writer
func WFormatterOption(f Formatter) WriterOption {
return func(s *WriterSettings) {
if f != nil {
s.f = zfmtShim{F: f}
}
}
}
// WriteOption is a function that modifies the kafka.Message to be transmitted
type WriteOption interface {
apply(s *kafka.Message)
}
// WithHeaders allows for the specification of headers. Specified headers will override collisions.
func WithHeaders(headers map[string]string) WriteOption { return withHeaderOption{headers: headers} }
type withHeaderOption struct {
headers map[string]string
}
func (o withHeaderOption) apply(s *kafka.Message) {
updateHeaders := func(k, v string) {
header := kafka.Header{
Key: k,
Value: []byte(v),
}
for i, h := range s.Headers {
if h.Key == k {
s.Headers[i] = header
return
}
}
s.Headers = append(s.Headers, header)
}
for k, v := range o.headers {
updateHeaders(k, v)
}
}