/
trace.go
491 lines (409 loc) · 11.5 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// This package is a reduced version of the go trace implementations
package tracer
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"os"
"regexp"
"strings"
"time"
util "github.com/jimxshaw/tracerlogger"
)
const (
traceCtxKey string = "trace"
traceVersion string = "00"
traceparentHeader string = "traceparent"
tracestateHeader string = "tracestate"
traceStateFormat string = "node=%02x"
sampleFlag = traceFlag(0x01)
traceIDHexLength = 32
spanIDHexLength = 16
flagLength = 2
maxVersion = 0
versionLength = 2
traceElements = 4
)
var (
errInvalidHex = errors.New("invalid hex value")
errInvalidTraceLength = errors.New("length of traceid must be 32")
errInvalidTraceID = errors.New("invalid trace ID")
errInvalidSpanID = errors.New("invalid span ID")
errInvalidSpanLength = errors.New("length of traceid must be 16")
traceRegex = regexp.MustCompile(`^(?P<version>[0-9a-f]{2})-(?P<traceID>[a-f0-9]{32})-(?P<spanID>[a-f0-9]{16})-(?P<traceFlags>[a-f0-9]{2})(?:-.*)?$`)
stateRegex = regexp.MustCompile(`^node=(?P<node>[0-9a-f]{2})(?:-.*)?$`)
)
type traceID [16]byte
var nilTraceID traceID
func (ti traceID) IsValid() bool {
return !bytes.Equal(ti[:], nilTraceID[:])
}
func (ti traceID) MarshalJSON() ([]byte, error) {
return json.Marshal(ti.String())
}
func (ti traceID) String() string {
return hex.EncodeToString(ti[:])
}
type spanID [8]byte
var nilSpanID spanID
func (si spanID) IsValid() bool {
return !bytes.Equal(si[:], nilSpanID[:])
}
func (si spanID) MarshalJSON() ([]byte, error) {
return json.Marshal(si.String())
}
func (si spanID) String() string {
return hex.EncodeToString(si[:])
}
type traceFlag byte
// HexToTraceID convert string to traceID.
func HexToTraceID(hexString string) (traceID, error) {
trace := traceID{}
if len(hexString) != traceIDHexLength {
return trace, errInvalidTraceLength
}
err := decodeHex(hexString, trace[:])
if err != nil {
return trace, err
}
if !trace.IsValid() {
return trace, errInvalidTraceID
}
return trace, nil
}
// HexToSpanID convert string to spanID.
func HexToSpanID(hexString string) (spanID, error) {
span := spanID{}
if len(hexString) != spanIDHexLength {
return span, errInvalidSpanLength
}
err := decodeHex(hexString, span[:])
if err != nil {
return span, err
}
if !span.IsValid() {
return span, errInvalidSpanID
}
return span, nil
}
// decodeHex decode string to bytes.
func decodeHex(hexString string, b []byte) error {
decoded, err := hex.DecodeString(hexString)
if err != nil {
return errInvalidHex
}
copy(b, decoded)
return nil
}
// ipToHex convert the ip to hex value.
func ipToHex(ip net.IP) string {
ip = ip.To16()
i := int(ip[12]) * 16777216
i += int(ip[13]) * 65536
i += int(ip[14]) * 256
i += int(ip[15])
return fmt.Sprintf("%08x", i)
}
// makeTimestamp get the current time in miliseconds.
func makeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
type traceState struct {
Node string
}
// TracerContext store the trace context to be propagated.
// This wraps the http.ResponseWriter, injecting the trace headers in
// the various end point responses.
type TracerContext struct {
http.ResponseWriter
Span spanID
Trace traceID
Flag traceFlag
childs int
traceState traceState
nodeID string
internal bool
}
// TraceField is the sanitized version of the TracerContext in case that
// the TracerContext will be returned as data structure.
type TraceField struct {
TraceID string `json:"trace_id"`
SpandID string `json:"span_id"`
}
func (tc *TracerContext) String() string {
return fmt.Sprintf(
"%02x-%s-%s-%s",
maxVersion,
tc.Trace.String(),
tc.nodeID,
"00",
)
}
// mapSpan maps the request position as node in the request tree.
func (tc *TracerContext) mapSpan() {
if !tc.Span.IsValid() {
tc.nodeID = "0100000000000000"
return
}
var nodeID spanID
var key [1]byte
decodeHex("00", key[:])
var newKey [1]byte
decodeHex(tc.traceState.Node, newKey[:])
index := bytes.Index(tc.Span[:], key[:])
if index < 0 {
tc.nodeID = "0100000000000000"
return
}
copy(nodeID[:], tc.Span[:])
copy(nodeID[index:], newKey[:])
tc.nodeID = hex.EncodeToString(nodeID[:])
}
// next generates or gets the traceID and generates the state for the next node.
func (tc *TracerContext) next() (trace string, state string) {
tc.childs++
newNodeID := fmt.Sprintf(traceStateFormat, tc.childs)
currentTrace := tc.String()
return currentTrace, newNodeID
}
// IsValid returns true if the Trace and Span are valid.
func (tc *TracerContext) IsValid() bool {
isValidTrace := tc.Trace.IsValid()
isValidSpan := tc.Span.IsValid()
return isValidTrace && isValidSpan
}
// propagateHeaders add the trace headers in the responses.
func (tc *TracerContext) propagateHeaders() {
tc.ResponseWriter.Header().Add(traceparentHeader, tc.String())
state := fmt.Sprintf("node=%s", tc.traceState.Node)
if tc.internal {
tc.ResponseWriter.Header().Add(tracestateHeader, state)
}
}
func (tc *TracerContext) WriteHeader(statusCode int) {
tc.propagateHeaders()
tc.ResponseWriter.WriteHeader(statusCode)
}
// Propagate injects the TracerContext into http.Request and http.ResponseWriter.
func (tc *TracerContext) Propagate(r *http.Request, w http.ResponseWriter) (*http.Request, http.ResponseWriter) {
tc.ResponseWriter = w
newTrace, internal := generateTrace(r)
tc.internal = internal
if !internal || !tc.IsValid() {
tc.Trace = newTrace
tc.traceState.Node = "01"
}
tc.mapSpan()
return InjectInRequest(r, tc), tc
}
func (tc *TracerContext) Sanitize() TraceField {
return TraceField{
SpandID: tc.nodeID,
TraceID: tc.Trace.String(),
}
}
// Propagator implements the logic to be propagated in the request.
type Propagator interface {
// Inject the trace context in the http.Request context and the http.ResponseWriter
Propagate(r *http.Request, w http.ResponseWriter) (*http.Request, http.ResponseWriter)
// Return a sanitized version of the TracerContext
Sanitize() TraceField
// Get the next node values
next() (trace string, state string)
}
// ExtractFromRequest extracts the trace information from the request headers.
func ExtractFromRequest(r *http.Request) Propagator {
tracer := &TracerContext{}
rawTraceParent := r.Header.Get(traceparentHeader)
rawTraceState := r.Header.Get(tracestateHeader)
state := extractState(rawTraceState)
elements := traceRegex.FindStringSubmatch(rawTraceParent)
if len(elements) == 0 {
return tracer
}
// the FindStringSubmatch return string match and the sub matches
// thats why traceElements + 1
if len(elements) < traceElements+1 {
return tracer
}
if len(elements[1]) != versionLength {
return tracer
}
verByte, err := hex.DecodeString(elements[1])
if err != nil {
return tracer
}
version := int(verByte[0])
if version > maxVersion {
return tracer
}
if len(elements[2]) != traceIDHexLength {
return tracer
}
if len(elements[3]) != spanIDHexLength {
return tracer
}
if len(elements[4]) != flagLength {
return tracer
}
flag, err := hex.DecodeString(elements[4])
if err != nil {
return tracer
}
traceID, err := HexToTraceID(elements[2])
if err != nil {
return tracer
}
spanID, err := HexToSpanID(elements[3])
if err != nil {
return tracer
}
tracer.Flag = traceFlag(flag[0])
tracer.Trace = traceID
tracer.Span = spanID
tracer.traceState = state
return tracer
}
// ExtractFromCtx extracts the trace information from the context.
// In case the middleware is not loaded the the function returns an empty propagator.
func ExtractFromCtx(ctx context.Context) Propagator {
ctxValue, ok := ctx.Value(traceCtxKey).(*TracerContext)
if ok {
return ctxValue
}
return NewTracerContext()
}
// InjectInRequest the trace information in the request context.
func InjectInRequest(r *http.Request, trace *TracerContext) *http.Request {
ctx := InjectInCtx(r.Context(), trace)
return r.WithContext(ctx)
}
// InjectHeaders injects the trace information in the request headers.
// The intention is that it will be used in API clients for internal calls.
func InjectHeaders(ctx context.Context, req *http.Request) *http.Request {
propagator := ExtractFromCtx(ctx)
trace, state := propagator.next()
req.Header.Set(traceparentHeader, trace)
req.Header.Set(tracestateHeader, state)
return req
}
// InjectInCtx injects the trace information in the context.
func InjectInCtx(ctx context.Context, trace *TracerContext) context.Context {
ctxNew := context.WithValue(ctx, traceCtxKey, trace)
return ctxNew
}
// getServerIp gets the server ip.
func getServerIp() (net.IP, *net.IPNet, error) {
containerHostname, err := os.Hostname()
if err != nil {
return net.IP{}, &net.IPNet{}, err
}
ipAddr, err := net.ResolveIPAddr("ip", containerHostname)
if err != nil {
return net.IP{}, &net.IPNet{}, err
}
containerIp := net.ParseIP(ipAddr.String())
ipMask := containerIp.DefaultMask()
network := containerIp.Mask(ipMask)
net := &net.IPNet{
IP: network,
Mask: ipMask,
}
return containerIp, net, nil
}
// generateTrace gets the trace or generates a new one for externals request.
func generateTrace(r *http.Request) (traceID, bool) {
var ipHex string
var isInternalRequest bool
var requestIp net.IP
ip, network, err := getServerIp()
remoteAddr := strings.Split(r.RemoteAddr, ":")[0]
if err == nil {
requestIp = net.ParseIP(remoteAddr)
isInternalRequest = network.Contains(requestIp) ||
isIpPrivateOrLocal(requestIp)
ipHex = ipToHex(ip)
} else {
isInternalRequest = false
ipHex, _ = util.RandomHex(4)
}
currentTime := makeTimestamp()
uniqueID, _ := util.RandomHex(5)
traceIDHex := fmt.Sprintf("%s%d0%s", ipHex, currentTime, uniqueID)
trace, _ := HexToTraceID(traceIDHex)
return trace, isInternalRequest
}
// NewTracerContext creates a new TracerContext.
func NewTracerContext() *TracerContext {
ipHex, _ := util.RandomHex(4)
currentTime := makeTimestamp()
uniqueID, _ := util.RandomHex(5)
traceIDHex := fmt.Sprintf("%s%d0%s", ipHex, currentTime, uniqueID)
trace, _ := HexToTraceID(traceIDHex)
tc := &TracerContext{
Trace: trace,
traceState: traceState{
Node: "01",
},
}
tc.mapSpan()
return tc
}
// extractState extracts the tracestate in a string value.
func extractState(value string) traceState {
defaultState := traceState{
Node: "01",
}
matches := stateRegex.FindStringSubmatch(value)
if len(matches) != 2 {
return defaultState
}
if !isHexString(matches[1]) {
return defaultState
}
return traceState{
Node: matches[1],
}
}
// isHexString validate if string is a hex value.
func isHexString(s string) bool {
_, err := hex.DecodeString(s)
return err == nil
}
// privateIPBlocks is a map of private and loopbacks ips.
var privateIPBlocks []*net.IPNet
// isIpPrivateOrLocal returns true if the ip is local or private.
func isIpPrivateOrLocal(ip net.IP) bool {
if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
return true
}
for _, block := range privateIPBlocks {
if block.Contains(ip) {
return true
}
}
return false
}
func init() {
for _, cidr := range []string{
"127.0.0.0/8", // IPv4 loopback
"10.0.0.0/8", // RFC1918
"172.16.0.0/12", // RFC1918
"192.168.0.0/16", // RFC1918
"169.254.0.0/16", // RFC3927 link-local
"::1/128", // IPv6 loopback
"fe80::/10", // IPv6 link-local
"fc00::/7", // IPv6 unique local addr
} {
_, block, err := net.ParseCIDR(cidr)
if err != nil {
panic(fmt.Errorf("parse error on %q: %v", cidr, err))
}
privateIPBlocks = append(privateIPBlocks, block)
}
}