forked from SpirentOrion/trace
/
trace.go
321 lines (276 loc) · 7.59 KB
/
trace.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package trace
import (
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"log"
"math"
"net/http"
"os"
"time"
"github.com/jtolds/gls"
)
const (
// Trace kinds
KindRequest = "request"
// gls.ContextManager keys
spanIdKey = "trace:spanid"
traceIdKey = "trace:traceid"
)
var (
// Process is process name used when New or Continue creates new Spans.
Process string
// HeaderKey is the key used when ServeHTTP inserts an id header in requests or responses.
HeaderKey = "X-Request-Id"
// HonorReqHeader determines whether or not ServeHTTP honors id headers in requests.
HonorReqHeader bool = false
// AddRespHeader determines whether or not ServeHTTP adds id headers to responses.
AddRespHeader bool = true
// Internal state
cm *gls.ContextManager
spans chan *Span
// Errors
errBufferRequired = errors.New("buffer must be greater than zero")
errRecNotActive = errors.New("trace recording isn't active")
errNoContextInStack = errors.New("no trace context in this call stack")
)
func init() {
// "argv[0]:pid@hostname"
host, _ := os.Hostname()
Process = fmt.Sprintf("%s:%d@%s", os.Args[0], os.Getpid(), host)
}
// Span tracks a processing activity within a trace.
type Span struct {
SpanId int64 `yaml:"span_id"`
TraceId int64 `yaml:"trace_id"`
ParentId int64 `yaml:"parent_id"`
Process string `yaml:",omitempty"`
Kind string `yaml:",omitempty"`
Name string `yaml:",omitempty"`
Data []byte `yaml:",omitempty"`
Start time.Time `yaml:"-"`
StartTimestamp string `yaml:"start,omitempty"`
Finish time.Time `yaml:"-"`
FinishTimestamp string `yaml:"finish,omitempty"`
recStart bool
recError bool
}
// Recorder instances persist TraceSpans to an external datastore.
type Recorder interface {
Start(s *Span) error
Finish(s *Span) error
}
// CurrentSpanId returns the caller's current span id.
func CurrentSpanId() int64 {
if cm == nil {
return 0
}
v, ok := cm.GetValue(spanIdKey)
if !ok {
return 0
}
spanId, ok := v.(int64)
if !ok {
return 0
}
return spanId
}
// CurrentTraceId returns the caller's current trace id.
func CurrentTraceId() int64 {
if cm == nil {
return 0
}
v, ok := cm.GetValue(traceIdKey)
if !ok {
return 0
}
traceId, ok := v.(int64)
if !ok {
return 0
}
return traceId
}
// Record starts recording in a goroutine. Because Run must not be
// allowed to block, buffer must be greater than zero. If a Logger is
// provided, then errors that occur during recording will be logged.
func Record(rec Recorder, buffer int, logger *log.Logger) error {
if buffer < 1 {
return errBufferRequired
}
cm = gls.NewContextManager()
spans = make(chan *Span, buffer)
go record(rec, logger)
return nil
}
func record(rec Recorder, logger *log.Logger) {
for ts := range spans {
if !ts.recStart {
ts.recStart = true
if err := rec.Start(ts); err != nil {
ts.recError = true
if logger != nil {
log.Printf("failed to record trace %x span %x start: %s", ts.TraceId, ts.SpanId, err)
}
}
} else if !ts.recError {
if err := rec.Finish(ts); err != nil {
if logger != nil {
log.Printf("failed to record trace %x span %x finish: %s", ts.TraceId, ts.SpanId, err)
}
}
}
}
}
// New starts a new trace. If recording is active, a new Span is
// allocated and returned, otherwise no allocation occurs and nil is
// returned (along with an error).
//
// As a caller convenience, if traceId is non-zero, then that value is
// used instead of generating a probablistically unique id. This may
// be useful for callers that want to generate their own id values.
func New(traceId int64, kind string, name string) (*Span, error) {
if spans == nil {
return nil, errRecNotActive
}
spanId, err := GenerateId()
if err != nil {
return nil, err
}
if traceId == 0 {
traceId, err = GenerateId()
if err != nil {
return nil, err
}
}
return &Span{
SpanId: spanId,
TraceId: traceId,
Process: Process,
Kind: kind,
Name: name,
}, nil
}
// Continue continues an existing trace. If recording is active, a
// new Span instance is allocated and returned, otherwise no
// allocation occurs and nil is returned (along with an error).
func Continue(kind string, name string) (*Span, error) {
if spans == nil {
return nil, errRecNotActive
}
parentId := CurrentSpanId()
traceId := CurrentTraceId()
if parentId == 0 || traceId == 0 {
return nil, errNoContextInStack
}
spanId, err := GenerateId()
if err != nil {
return nil, err
}
return &Span{
SpanId: spanId,
TraceId: traceId,
ParentId: parentId,
Process: Process,
Kind: kind,
Name: name,
}, nil
}
// Run records a Span (to provide visibility that the span has
// started), invokes the function f, and then records the Span a
// second time (to update the finish time).
func Run(s *Span, f func()) {
// If New or Continue returned nil, then ts is probably also
// nil. We quietly tolerate so that callers don't need to know
// or care whether recording is active.
if s != nil && spans != nil {
// Record the span start
s.Start = time.Now()
s.StartTimestamp = s.Start.Format(time.RFC3339Nano)
s.Finish = time.Time{}
spans <- s
// Setup to record the span finish
defer func() {
s.Finish = time.Now()
s.FinishTimestamp = s.Finish.Format(time.RFC3339Nano)
spans <- s
}()
// Stash the span id and trace id on the stack and invoke f
values := gls.Values{
spanIdKey: s.SpanId,
traceIdKey: s.TraceId,
}
cm.SetValues(values, f)
} else {
f()
}
}
// Go functions similarly to Run, except that f is run in a new goroutine.
func Go(s *Span, f func()) {
if s != nil && spans != nil {
gls.Go(func() {
Run(s, f)
})
} else {
go f()
}
}
// GenerateId returns a probablistically unique 64-bit id. All id
// values returned by this function will be positive integers. This
// may be useful for callers that want to generate their own id
// values.
func GenerateId() (int64, error) {
// Return a random int64, constrained to positive values
for retry := 0; retry < 3; retry++ {
var x uint64
if err := binary.Read(rand.Reader, binary.LittleEndian, &x); err != nil {
return 0, err
}
id := int64(x & math.MaxInt64)
if id > 0 {
return id, nil
}
}
// Failsafe
return 0, errors.New("rand.Reader failed to produce a useable value")
}
// ServeHTTP is a middleware function that facilitates HTTP request tracing.
//
// If the request contains an id header and HonorReqHeader is true,
// then the id values are used (allowing trace contexts to span
// services). Otherwise a new trace id is generated. An id header is
// optionally added to the response.
func ServeHTTP(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
var (
traceId, parentId int64
s *Span
)
// Optionally honor incoming id headers. If present, header must be in the form "traceId:parentId".
if HonorReqHeader {
if _, exists := req.Header[HeaderKey]; exists {
var traceId, parentId int64
n, _ := fmt.Scanf("%d:%d", &traceId, &parentId)
if n < 2 {
traceId = 0
parentId = 0
}
}
}
// Start a new trace, either using an existing id (from the request header) or a new one
s, err := New(traceId, KindRequest, fmt.Sprintf("%s %s", req.Method, req.URL.Path))
if err == nil {
s.ParentId = parentId
// Add headers
req.Header.Set(HeaderKey, fmt.Sprintf("%d:%d", s.TraceId, s.SpanId))
if AddRespHeader {
rw.Header().Set(HeaderKey, fmt.Sprintf("%d", s.TraceId))
}
// Invoke the next handler
Run(s, func() {
next(rw, req)
})
} else {
// Invoke the next handler
next(rw, req)
}
}