add set ct for records && update setHeaders
This commit is contained in:
parent
2ca4e5c74e
commit
c71478ee9a
11
carrier.go
11
carrier.go
@ -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
5
kgo.go
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user