fixup tracer span labels
Some checks failed
build / test (push) Failing after 1m29s
codeql / analyze (go) (push) Failing after 1m53s
build / lint (push) Successful in 9m15s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-03-14 16:47:44 +03:00
parent ec7a22b2dc
commit 81dcef8b28

View File

@ -7,9 +7,9 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/twmb/franz-go/pkg/kgo" "github.com/twmb/franz-go/pkg/kgo"
"go.unistack.org/micro/v3/tracer"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0" semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
"go.unistack.org/micro/v3/tracer"
) )
type hookTracer struct { type hookTracer struct {
@ -71,12 +71,8 @@ func (m *hookTracer) OnProduceRecordBuffered(r *kgo.Record) {
if m.clientID != "" { if m.clientID != "" {
attrs = append(attrs, semconv.MessagingKafkaClientIDKey.String(m.clientID)) attrs = append(attrs, semconv.MessagingKafkaClientIDKey.String(m.clientID))
} }
ifattrs := make([]interface{}, 0, len(attrs))
for _, attr := range attrs {
ifattrs = append(ifattrs, attr)
}
opts := []tracer.SpanOption{ opts := []tracer.SpanOption{
tracer.WithSpanLabels(ifattrs...), tracer.WithSpanLabels(otel2Micro(attrs...)),
tracer.WithSpanKind(tracer.SpanKindProducer), tracer.WithSpanKind(tracer.SpanKindProducer),
} }
// Start the "publish" span. // Start the "publish" span.
@ -129,12 +125,8 @@ func (m *hookTracer) OnFetchRecordBuffered(r *kgo.Record) {
if m.group != "" { if m.group != "" {
attrs = append(attrs, semconv.MessagingKafkaConsumerGroupKey.String(m.group)) attrs = append(attrs, semconv.MessagingKafkaConsumerGroupKey.String(m.group))
} }
ifattrs := make([]interface{}, 0, len(attrs))
for _, attr := range attrs {
ifattrs = append(ifattrs, attr)
}
opts := []tracer.SpanOption{ opts := []tracer.SpanOption{
tracer.WithSpanLabels(ifattrs...), tracer.WithSpanLabels(otel2Micro(attrs...)),
tracer.WithSpanKind(tracer.SpanKindConsumer), tracer.WithSpanKind(tracer.SpanKindConsumer),
} }
@ -177,10 +169,6 @@ func (m *hookTracer) WithProcessSpan(r *kgo.Record) (context.Context, tracer.Spa
semconv.MessagingKafkaMessageOffset(int(r.Offset)), semconv.MessagingKafkaMessageOffset(int(r.Offset)),
} }
attrs = maybeKeyAttr(attrs, r) attrs = maybeKeyAttr(attrs, r)
ifattrs := make([]interface{}, 0, len(attrs))
for _, attr := range attrs {
ifattrs = append(ifattrs, attr)
}
if m.clientID != "" { if m.clientID != "" {
attrs = append(attrs, semconv.MessagingKafkaClientIDKey.String(m.clientID)) attrs = append(attrs, semconv.MessagingKafkaClientIDKey.String(m.clientID))
} }
@ -188,7 +176,7 @@ func (m *hookTracer) WithProcessSpan(r *kgo.Record) (context.Context, tracer.Spa
attrs = append(attrs, semconv.MessagingKafkaConsumerGroupKey.String(m.group)) attrs = append(attrs, semconv.MessagingKafkaConsumerGroupKey.String(m.group))
} }
opts := []tracer.SpanOption{ opts := []tracer.SpanOption{
tracer.WithSpanLabels(ifattrs...), tracer.WithSpanLabels(otel2Micro(attrs...)),
tracer.WithSpanKind(tracer.SpanKindConsumer), tracer.WithSpanKind(tracer.SpanKindConsumer),
} }
@ -210,3 +198,11 @@ func maybeKeyAttr(attrs []attribute.KeyValue, r *kgo.Record) []attribute.KeyValu
keykey = string(r.Key) keykey = string(r.Key)
return append(attrs, semconv.MessagingKafkaMessageKeyKey.String(keykey)) return append(attrs, semconv.MessagingKafkaMessageKeyKey.String(keykey))
} }
func otel2Micro(attrs ...attribute.KeyValue) []interface{} {
ret := make([]interface{}, 0, len(attrs))
for _, a := range attrs {
ret = append(ret, a.Key, a.Value)
}
return ret
}