-
Notifications
You must be signed in to change notification settings - Fork 4
/
span.go
70 lines (57 loc) · 1.39 KB
/
span.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package tracer
import (
"context"
"sync"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
type Span struct {
span opentracing.Span
once sync.Once
TraceID string
SpanID string
ParentSpanID string
}
func Start(ctx context.Context, operation string) (context.Context, *Span) {
return new(Span).Start(ctx, operation)
}
func (sp *Span) Start(ctx context.Context, operation string) (context.Context, *Span) {
span, cctx := opentracing.StartSpanFromContext(ctx, operation)
entry := GetSpanEntryFromCtx(cctx)
spp := &Span{
span: span,
TraceID: entry.TraceID,
SpanID: entry.SpanID,
ParentSpanID: entry.ParentSpanID,
}
return cctx, spp
}
func (sp *Span) End() {
sp.once.Do(func() {
sp.span.Finish()
})
}
func (sp *Span) SetTag(key string, val interface{}) *Span {
sp.span.SetTag(key, val)
return sp
}
func (sp *Span) LogFields(fields ...log.Field) *Span {
sp.span.LogFields(fields...)
return sp
}
func (sp *Span) LogKV(key string, val interface{}) *Span {
sp.span.LogKV(key, val)
return sp
}
func (sp *Span) LogString(key, val string) *Span {
sp.span.LogFields(log.String(key, val))
return sp
}
func (sp *Span) LogObject(key string, obj interface{}) *Span {
sp.span.LogFields(log.Object(key, obj))
return sp
}
func (sp *Span) LogError(err error) *Span {
sp.span.LogFields(log.Error(err))
return sp
}