/
span.go
258 lines (235 loc) · 5.62 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package apmot
import (
"fmt"
"net/http"
"net/url"
"sync"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/elastic/apm-agent-go"
"github.com/elastic/apm-agent-go/module/apmhttp"
)
// otSpan wraps elasticapm objects to implement the opentracing.Span interface.
type otSpan struct {
tracer *otTracer
mu sync.Mutex
tx *elasticapm.Transaction
span *elasticapm.Span
tags opentracing.Tags
ctx spanContext
}
// SetOperationName sets or changes the operation name.
func (s *otSpan) SetOperationName(operationName string) opentracing.Span {
if s.tx != nil {
s.tx.Name = operationName
} else {
s.span.Name = operationName
}
return s
}
// SetTag adds or changes a tag.
func (s *otSpan) SetTag(key string, value interface{}) opentracing.Span {
s.mu.Lock()
defer s.mu.Unlock()
if s.tags == nil {
s.tags = make(opentracing.Tags, 1)
}
s.tags[key] = value
return s
}
// Finish ends the span; this (or FinishWithOptions) must be the last method
// call on the span, except for calls to Context which may be called at any
// time.
func (s *otSpan) Finish() {
s.FinishWithOptions(opentracing.FinishOptions{})
}
// FinishWithOptions is like Finish, but provides explicit control over the
// end timestamp and log data.
func (s *otSpan) FinishWithOptions(opts opentracing.FinishOptions) {
s.mu.Lock()
defer s.mu.Unlock()
if !opts.FinishTime.IsZero() {
if s.span != nil {
s.span.Duration = opts.FinishTime.Sub(s.span.Timestamp)
} else {
s.tx.Duration = opts.FinishTime.Sub(s.tx.Timestamp)
}
}
if s.span != nil {
s.setSpanContext()
s.span.End()
} else {
s.setTransactionContext()
s.tx.End()
}
}
// Tracer returns the Tracer that created this span.
func (s *otSpan) Tracer() opentracing.Tracer {
return s.tracer
}
// Context returns the span's current context.
//
// It is valid to call Context after calling Finish or FinishWithOptions.
// The resulting context is also valid after the span is finished.
func (s *otSpan) Context() opentracing.SpanContext {
s.mu.Lock()
defer s.mu.Unlock()
return s.ctx
}
// SetBaggageItem is a no-op; we do not support baggage.
func (s *otSpan) SetBaggageItem(key, val string) opentracing.Span {
// We do not support baggage.
return s
}
// BaggageItem returns the empty string; we do not support baggage.
func (s *otSpan) BaggageItem(key string) string {
return ""
}
func (*otSpan) LogKV(keyValues ...interface{}) {
// No-op.
}
func (*otSpan) LogFields(fields ...log.Field) {
// No-op.
}
func (*otSpan) LogEvent(event string) {
// No-op.
}
func (*otSpan) LogEventWithPayload(event string, payload interface{}) {
// No-op.
}
func (*otSpan) Log(ld opentracing.LogData) {
// No-op.
}
func (s *otSpan) setSpanContext() {
var (
dbContext elasticapm.DatabaseSpanContext
component string
httpURL string
httpMethod string
haveDBContext bool
haveHTTPContext bool
)
for k, v := range s.tags {
switch k {
case "component":
component = fmt.Sprint(v)
case "db.instance":
dbContext.Instance = fmt.Sprint(v)
haveDBContext = true
case "db.statement":
dbContext.Statement = fmt.Sprint(v)
haveDBContext = true
case "db.type":
dbContext.Type = fmt.Sprint(v)
haveDBContext = true
case "db.user":
dbContext.User = fmt.Sprint(v)
haveDBContext = true
case "http.url":
haveHTTPContext = true
httpURL = fmt.Sprint(v)
case "http.method":
haveHTTPContext = true
httpMethod = fmt.Sprint(v)
// Elastic APM-specific tags:
case "type":
s.span.Type = fmt.Sprint(v)
}
}
switch {
case haveHTTPContext:
if s.span.Type == "" {
s.span.Type = "ext.http"
}
url, err := url.Parse(httpURL)
if err == nil {
var req http.Request
req.ProtoMajor = 1 // Assume HTTP/1.1
req.ProtoMinor = 1
req.Method = httpMethod
req.URL = url
s.span.Context.SetHTTPRequest(&req)
}
case haveDBContext:
if s.span.Type == "" {
dbType := "sql"
if dbContext.Type != "" {
dbType = dbContext.Type
}
s.span.Type = fmt.Sprintf("db.%s.query", dbType)
}
s.span.Context.SetDatabase(dbContext)
}
if s.span.Type == "" {
s.span.Type = component
if s.span.Type == "" {
s.span.Type = "unknown"
}
}
}
func (s *otSpan) setTransactionContext() {
var (
component string
httpMethod string
httpStatusCode = -1
httpURL string
isError bool
)
for k, v := range s.tags {
switch k {
case "component":
component = fmt.Sprint(v)
case "http.method":
httpMethod = fmt.Sprint(v)
case "http.status_code":
if code, ok := v.(uint16); ok {
httpStatusCode = int(code)
}
case "http.url":
httpURL = fmt.Sprint(v)
case "error":
isError, _ = v.(bool)
// Elastic APM-specific tags:
case "type":
s.tx.Type = fmt.Sprint(v)
case "result":
s.tx.Result = fmt.Sprint(v)
case "user.id":
s.tx.Context.SetUserID(fmt.Sprint(v))
case "user.email":
s.tx.Context.SetUserEmail(fmt.Sprint(v))
case "user.username":
s.tx.Context.SetUsername(fmt.Sprint(v))
default:
s.tx.Context.SetTag(k, fmt.Sprint(v))
}
}
if s.tx.Type == "" {
if httpURL != "" {
s.tx.Type = "request"
} else if component != "" {
s.tx.Type = component
} else {
s.tx.Type = "unknown"
}
}
if s.tx.Result == "" {
if httpStatusCode != -1 {
s.tx.Result = apmhttp.StatusCodeResult(httpStatusCode)
s.tx.Context.SetHTTPStatusCode(httpStatusCode)
} else if isError {
s.tx.Result = "error"
}
}
if httpURL != "" {
uri, err := url.ParseRequestURI(httpURL)
if err == nil {
var req http.Request
req.ProtoMajor = 1 // Assume HTTP/1.1
req.ProtoMinor = 1
req.Method = httpMethod
req.URL = uri
s.tx.Context.SetHTTPRequest(&req)
}
}
}