This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
/
tracer.go
106 lines (90 loc) · 2.8 KB
/
tracer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package tracer
import (
ot "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/thundra-io/thundra-lambda-agent-go/v2/constants"
"github.com/thundra-io/thundra-lambda-agent-go/v2/ext"
"github.com/thundra-io/thundra-lambda-agent-go/v2/plugin"
"github.com/thundra-io/thundra-lambda-agent-go/v2/utils"
)
// New creates and returns a standard Tracer which defers completed Spans to
// `recorder`.
func New(recorder SpanRecorder) ot.Tracer {
return &tracerImpl{
Recorder: recorder,
}
}
type tracerImpl struct {
Recorder SpanRecorder
}
// StartSpan starts a new span with options and returns it.
func (t *tracerImpl) StartSpan(operationName string, opts ...ot.StartSpanOption) ot.Span {
sso := ot.StartSpanOptions{}
for _, o := range opts {
o.Apply(&sso)
}
return t.StartSpanWithOptions(operationName, sso)
}
func (t *tracerImpl) StartSpanWithOptions(operationName string, opts ot.StartSpanOptions) ot.Span {
newSpan := t.getSpan()
newSpan.tracer = t
newSpan.raw.Tags = opts.Tags
newSpan.raw.Logs = []ot.LogRecord{}
newSpan.raw.OperationName = operationName
newSpan.raw.Context.TransactionID = plugin.TransactionID
newSpan.raw.Context.TraceID = plugin.TraceID
newSpan.raw.Context.SpanID = utils.GenerateNewID()
for _, ref := range opts.References {
if ref.Type == ot.ChildOfRef {
parentCtx := ref.ReferencedContext.(SpanContext)
newSpan.setParent(parentCtx)
}
}
if opts.StartTime.IsZero() {
newSpan.raw.StartTimestamp = utils.GetTimestamp()
} else {
newSpan.raw.StartTimestamp = utils.TimeToMs(opts.StartTime)
}
className, ok := opts.Tags[ext.ClassNameKey]
if ok {
classNameStr, ok := className.(string)
if ok {
newSpan.raw.ClassName = classNameStr
} else {
newSpan.raw.ClassName = constants.DefaultClassName
}
} else {
newSpan.raw.ClassName = constants.DefaultClassName
}
domainName, ok := opts.Tags[ext.DomainNameKey]
if ok {
domainNameStr, ok := domainName.(string)
if ok {
newSpan.raw.DomainName = domainNameStr
} else {
newSpan.raw.DomainName = constants.DefaultDomainName
}
} else {
newSpan.raw.DomainName = constants.DefaultDomainName
}
// Add to recorder
t.Recorder.RecordSpan(&newSpan.raw)
return newSpan
}
func (t *tracerImpl) getSpan() *spanImpl {
return &spanImpl{}
}
// TODO Will be implemented
func (t *tracerImpl) Inject(sc ot.SpanContext, format interface{}, carrier interface{}) error {
return errors.New("Inject has not been supported yet")
}
// TODO Will be implemented
func (t *tracerImpl) Extract(format interface{}, carrier interface{}) (ot.SpanContext, error) {
return nil, errors.New("Extract has not been supported yet")
}
func (t *tracerImpl) AddSpanListener(listener ThundraSpanListener) {
RegisterSpanListener(listener)
}
func (t *tracerImpl) GetSpanListeners() []ThundraSpanListener {
return GetSpanListeners()
}