add set ct for records && update setHeaders
All checks were successful
lint / lint (pull_request) Successful in 1m29s
test / test (pull_request) Successful in 3m27s

This commit is contained in:
Денис Евстигнеев 2025-01-20 13:46:11 +03:00
parent 2ca4e5c74e
commit c71478ee9a
2 changed files with 18 additions and 8 deletions

View File

@ -55,22 +55,27 @@ func (c RecordCarrier) Keys() []string {
func setHeaders(r *kgo.Record, md metadata.Metadata) { func setHeaders(r *kgo.Record, md metadata.Metadata) {
seen := make(map[string]struct{}) seen := make(map[string]struct{})
loop: loop:
for k, v := range md { for k, v := range md {
if _, ok := seen[k]; ok {
continue loop
}
for i := 0; i < len(r.Headers); i++ { for i := 0; i < len(r.Headers); i++ {
if r.Headers[i].Key == k { if r.Headers[i].Key == k {
// Key exist, update the value. // Key exist, update the value.
r.Headers[i].Value = []byte(v) r.Headers[i].Value = []byte(v)
continue loop continue loop
} else if _, ok := seen[k]; ok {
continue loop
} }
}
// Key does not exist, append new header. // Key does not exist, append new header.
r.Headers = append(r.Headers, kgo.RecordHeader{ r.Headers = append(r.Headers, kgo.RecordHeader{
Key: k, Key: k,
Value: []byte(v), Value: []byte(v),
}) })
seen[k] = struct{}{} seen[k] = struct{}{}
} }
}
} }

5
kgo.go
View File

@ -279,8 +279,13 @@ func (k *Broker) publish(ctx context.Context, msgs []*broker.Message, opts ...br
for _, msg := range msgs { for _, msg := range msgs {
rec := &kgo.Record{Context: ctx, Key: key} rec := &kgo.Record{Context: ctx, Key: key}
rec.Topic, _ = msg.Header.Get(metadata.HeaderTopic) rec.Topic, _ = msg.Header.Get(metadata.HeaderTopic)
msg.Header.Del(metadata.HeaderTopic) msg.Header.Del(metadata.HeaderTopic)
ct, _ := msg.Header.Get(metadata.HeaderContentType)
rec.Headers = append(rec.Headers, kgo.RecordHeader{Key: metadata.HeaderContentType, Value: []byte(ct)})
k.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Inc() k.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Inc()
if options.BodyOnly || k.opts.Codec.String() == "noop" { if options.BodyOnly || k.opts.Codec.String() == "noop" {
rec.Value = msg.Body rec.Value = msg.Body