fixup labels
Some checks failed
build / test (push) Failing after 1m13s
codeql / analyze (go) (push) Failing after 1m46s
build / lint (push) Successful in 9m14s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-03-17 00:51:29 +03:00
parent 54d9857bb9
commit 7948652293
2 changed files with 26 additions and 4 deletions

View File

@ -126,12 +126,12 @@ func (os *otSpan) Finish(opts ...tracer.SpanOption) {
} }
l := len(options.Labels) l := len(options.Labels)
for idx := 0; idx < len(options.Labels); idx++ { for idx := 0; idx < l; idx++ {
switch lt := options.Labels[idx].(type) { switch lt := options.Labels[idx].(type) {
case attribute.KeyValue: case attribute.KeyValue:
os.span.SetTag(string(lt.Key), lt.Value.AsInterface()) os.span.SetTag(string(lt.Key), lt.Value.AsInterface())
case string: case string:
if l < idx+1 { if l > idx+1 {
os.span.SetTag(lt, options.Labels[idx+1]) os.span.SetTag(lt, options.Labels[idx+1])
idx++ idx++
} }
@ -172,12 +172,13 @@ func (os *otSpan) Kind() tracer.SpanKind {
func (os *otSpan) AddLabels(labels ...interface{}) { func (os *otSpan) AddLabels(labels ...interface{}) {
l := len(labels) l := len(labels)
for idx := 0; idx < len(labels); idx++ {
for idx := 0; idx < l; idx++ {
switch lt := labels[idx].(type) { switch lt := labels[idx].(type) {
case attribute.KeyValue: case attribute.KeyValue:
os.span.SetTag(string(lt.Key), lt.Value.AsInterface()) os.span.SetTag(string(lt.Key), lt.Value.AsInterface())
case string: case string:
if l < idx+1 { if l > idx+1 {
os.span.SetTag(lt, labels[idx+1]) os.span.SetTag(lt, labels[idx+1])
idx++ idx++
} }

View File

@ -30,3 +30,24 @@ func TestTraceID(t *testing.T) {
t.Fatalf("invalid span span id %#+v", v) t.Fatalf("invalid span span id %#+v", v)
} }
} }
func TestTraceTags(t *testing.T) {
md := metadata.New(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx = metadata.NewIncomingContext(ctx, md)
mtr := mocktracer.New()
tr := NewTracer(Tracer(mtr))
if err := tr.Init(); err != nil {
t.Fatal(err)
}
var sp tracer.Span
ctx, sp = tr.Start(ctx, "test", tracer.WithSpanLabels("key", "val", "odd"))
sp.Finish()
msp := mtr.FinishedSpans()[0]
t.Logf("mock span %#+v", msp.Tags())
}