Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
7948652293 | |||
54d9857bb9 | |||
1e9abb9905 | |||
6b9edf3593 |
@@ -76,6 +76,8 @@ func (t *otTracer) Start(ctx context.Context, name string, opts ...tracer.SpanOp
|
||||
}
|
||||
}
|
||||
|
||||
sp.AddLabels(options.Labels...)
|
||||
|
||||
return tracer.NewSpanContext(ctx, sp), sp
|
||||
}
|
||||
|
||||
@@ -118,23 +120,29 @@ func (os *otSpan) Tracer() tracer.Tracer {
|
||||
}
|
||||
|
||||
func (os *otSpan) Finish(opts ...tracer.SpanOption) {
|
||||
if len(os.opts.Labels)%2 != 0 {
|
||||
os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1]
|
||||
options := os.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
os.opts.Labels = tracer.UniqLabels(os.opts.Labels)
|
||||
for idx := 0; idx < len(os.opts.Labels); idx += 2 {
|
||||
k, ok := os.opts.Labels[idx].(string)
|
||||
if !ok {
|
||||
continue
|
||||
|
||||
l := len(options.Labels)
|
||||
for idx := 0; idx < l; idx++ {
|
||||
switch lt := options.Labels[idx].(type) {
|
||||
case attribute.KeyValue:
|
||||
os.span.SetTag(string(lt.Key), lt.Value.AsInterface())
|
||||
case string:
|
||||
if l > idx+1 {
|
||||
os.span.SetTag(lt, options.Labels[idx+1])
|
||||
idx++
|
||||
}
|
||||
}
|
||||
v := os.opts.Labels[idx+1]
|
||||
os.span.SetTag(k, v)
|
||||
}
|
||||
|
||||
if os.status == tracer.SpanStatusError {
|
||||
os.span.SetTag("error", true)
|
||||
os.span.LogKV("error", os.statusMsg)
|
||||
}
|
||||
os.span.SetTag("span.kind", os.opts.Kind)
|
||||
os.span.SetTag("span.kind", options.Kind)
|
||||
os.span.Finish()
|
||||
}
|
||||
|
||||
@@ -164,12 +172,13 @@ func (os *otSpan) Kind() tracer.SpanKind {
|
||||
|
||||
func (os *otSpan) AddLabels(labels ...interface{}) {
|
||||
l := len(labels)
|
||||
for idx := 0; idx < len(labels); idx++ {
|
||||
|
||||
for idx := 0; idx < l; idx++ {
|
||||
switch lt := labels[idx].(type) {
|
||||
case attribute.KeyValue:
|
||||
os.span.SetTag(string(lt.Key), lt.Value.AsInterface())
|
||||
case string:
|
||||
if l < idx+1 {
|
||||
if l > idx+1 {
|
||||
os.span.SetTag(lt, labels[idx+1])
|
||||
idx++
|
||||
}
|
||||
|
@@ -30,3 +30,24 @@ func TestTraceID(t *testing.T) {
|
||||
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())
|
||||
}
|
||||
|
Reference in New Issue
Block a user