Compare commits

...

5 Commits

Author SHA1 Message Date
905910e8ce avoid duplicate tags
Some checks failed
build / test (push) Failing after 1m17s
codeql / analyze (go) (push) Failing after 2m45s
build / lint (push) Successful in 9m30s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-17 23:07:06 +03:00
7948652293 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>
2024-03-17 00:51:29 +03:00
54d9857bb9 fixup labels
Some checks failed
build / test (push) Failing after 1m11s
codeql / analyze (go) (push) Failing after 1m48s
build / lint (push) Successful in 9m20s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-17 00:35:37 +03:00
1e9abb9905 fixup tracing
Some checks failed
build / test (push) Failing after 1m11s
codeql / analyze (go) (push) Failing after 1m47s
build / lint (push) Successful in 9m19s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-16 23:59:36 +03:00
6b9edf3593 fixup finish
Some checks failed
build / test (push) Failing after 1m21s
codeql / analyze (go) (push) Failing after 1m51s
build / lint (push) Successful in 9m15s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-15 11:34:31 +03:00
2 changed files with 39 additions and 22 deletions

View File

@@ -94,6 +94,7 @@ type otSpan struct {
opts tracer.SpanOptions opts tracer.SpanOptions
status tracer.SpanStatus status tracer.SpanStatus
statusMsg string statusMsg string
labels []interface{}
} }
func (os *otSpan) TraceID() string { func (os *otSpan) TraceID() string {
@@ -118,23 +119,29 @@ func (os *otSpan) Tracer() tracer.Tracer {
} }
func (os *otSpan) Finish(opts ...tracer.SpanOption) { func (os *otSpan) Finish(opts ...tracer.SpanOption) {
if len(os.opts.Labels)%2 != 0 { options := os.opts
os.opts.Labels = os.opts.Labels[:len(os.opts.Labels)-1] for _, o := range opts {
o(&options)
} }
os.opts.Labels = tracer.UniqLabels(os.opts.Labels)
for idx := 0; idx < len(os.opts.Labels); idx += 2 { l := len(options.Labels)
k, ok := os.opts.Labels[idx].(string) for idx := 0; idx < l; idx++ {
if !ok { switch lt := options.Labels[idx].(type) {
continue 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 { if os.status == tracer.SpanStatusError {
os.span.SetTag("error", true) os.span.SetTag("error", true)
os.span.LogKV("error", os.statusMsg) os.span.LogKV("error", os.statusMsg)
} }
os.span.SetTag("span.kind", os.opts.Kind) os.span.SetTag("span.kind", options.Kind)
os.span.Finish() os.span.Finish()
} }
@@ -163,18 +170,7 @@ func (os *otSpan) Kind() tracer.SpanKind {
} }
func (os *otSpan) AddLabels(labels ...interface{}) { func (os *otSpan) AddLabels(labels ...interface{}) {
l := len(labels) os.labels = append(os.labels, labels...)
for idx := 0; idx < len(labels); idx++ {
switch lt := 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, labels[idx+1])
idx++
}
}
}
} }
func NewTracer(opts ...tracer.Option) *otTracer { func NewTracer(opts ...tracer.Option) *otTracer {

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())
}