This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
/
span.go
219 lines (186 loc) · 4.57 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package tracer
import (
logger "log"
"sync"
"time"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/thundra-io/thundra-lambda-agent-go/utils"
)
type spanImpl struct {
tracer *tracerImpl
sync.Mutex // protects the fields below
raw RawSpan
// The number of logs dropped because of MaxLogsPerSpan.
numDroppedLogs int
}
// Finish is the last call that is made
func (s *spanImpl) Finish() {
s.FinishWithOptions(ot.FinishOptions{})
}
// FinishWithOptions finishes span and adds the given options to it
func (s *spanImpl) FinishWithOptions(opts ot.FinishOptions) {
if opts.FinishTime.IsZero() {
s.raw.EndTimestamp = utils.GetTimestamp()
} else {
s.raw.EndTimestamp = utils.TimeToMs(opts.FinishTime)
}
s.Lock()
defer func() {
s.Unlock()
s.onFinished()
}()
for _, lr := range opts.LogRecords {
s.appendLog(lr)
}
for _, ld := range opts.BulkLogData {
s.appendLog(ld.ToLogRecord())
}
}
// Deprecated: use LogFields or LogKV.
func (s *spanImpl) LogEvent(event string) {
s.Log(ot.LogData{
Event: event,
})
}
// Deprecated: use LogFields or LogKV.
func (s *spanImpl) LogEventWithPayload(event string, payload interface{}) {
s.Log(ot.LogData{
Event: event,
Payload: payload,
})
}
// Log .
func (s *spanImpl) Log(ld ot.LogData) {
s.Lock()
defer s.Unlock()
if ld.Timestamp.IsZero() {
ld.Timestamp = time.Now()
}
s.appendLog(ld.ToLogRecord())
}
// SetOperationName sets operation name.
func (s *spanImpl) SetOperationName(operationName string) ot.Span {
s.Lock()
defer s.Unlock()
s.raw.OperationName = operationName
return s
}
// SetTag sets a tag with given key and value.
func (s *spanImpl) SetTag(key string, value interface{}) ot.Span {
s.Lock()
defer s.Unlock()
if s.raw.Tags == nil {
s.raw.Tags = ot.Tags{}
}
s.raw.Tags[key] = value
return s
}
// LogKV logs logFields
func (s *spanImpl) LogKV(keyValues ...interface{}) {
fields, err := log.InterleavedKVToFields(keyValues...)
if err != nil {
s.LogFields(log.Error(err), log.String("function", "LogKV"))
return
}
s.LogFields(fields...)
}
func (s *spanImpl) appendLog(lr ot.LogRecord) {
s.raw.Logs = append(s.raw.Logs, lr)
return
}
// LogFields parses parameter fields sequentially, as first one is the key and the second is it's value.
func (s *spanImpl) LogFields(fields ...log.Field) {
lr := ot.LogRecord{
Fields: fields,
}
s.Lock()
defer s.Unlock()
if lr.Timestamp.IsZero() {
lr.Timestamp = time.Now()
}
s.appendLog(lr)
}
// Tracer return span's Tracer
func (s *spanImpl) Tracer() ot.Tracer {
return s.tracer
}
// Context returns SpanContext
func (s *spanImpl) Context() ot.SpanContext {
return s.raw.Context
}
// SetBaggageItem sets BaggageItem
func (s *spanImpl) SetBaggageItem(key, val string) ot.Span {
s.Lock()
defer s.Unlock()
s.raw.Context = s.raw.Context.WithBaggageItem(key, val)
return s
}
// BaggageItem returns BaggageItem
func (s *spanImpl) BaggageItem(key string) string {
s.Lock()
defer s.Unlock()
return s.raw.Context.Baggage[key]
}
// Operation returns the name of the "operation" this span is an instance of
func (s *spanImpl) OperationName() string {
return s.raw.OperationName
}
// StartTimestamp returns StartTimestamp
func (s *spanImpl) StartTimestamp() int64 {
return s.raw.StartTimestamp
}
// GetRaw casts opentracing span interface to spanImpl struct
func GetRaw(ots ot.Span) (*RawSpan, bool) {
s, ok := ots.(*spanImpl)
if !ok {
return nil, ok
}
return &s.raw, ok
}
func (s *spanImpl) setParent(parentCtx SpanContext) {
s.raw.ParentSpanID = parentCtx.SpanID
if l := len(parentCtx.Baggage); l > 0 {
s.raw.Context.Baggage = make(map[string]string, l)
for k, v := range parentCtx.Baggage {
s.raw.Context.Baggage[k] = v
}
}
}
func OnSpanStarted(ots ot.Span) {
if span, ok := ots.(*spanImpl); ok {
span.onStarted()
}
}
func (s *spanImpl) onStarted() {
spanListeners := s.tracer.GetSpanListeners()
for _, sl := range spanListeners {
s.handleOnSpanStarted(sl)
}
}
func (s *spanImpl) onFinished() {
spanListeners := s.tracer.GetSpanListeners()
for _, sl := range spanListeners {
s.handleOnSpanFinished(sl)
}
}
func (s *spanImpl) handleOnSpanStarted(listener ThundraSpanListener) {
defer func() {
if !listener.PanicOnError() {
if r := recover(); r != nil {
logger.Println("Error on span started:", r)
}
}
}()
listener.OnSpanStarted(s)
}
func (s *spanImpl) handleOnSpanFinished(listener ThundraSpanListener) {
defer func() {
if !listener.PanicOnError() {
if r := recover(); r != nil {
logger.Println("Error on span finished:", r)
}
}
}()
listener.OnSpanFinished(s)
}