forked from kubeshop/xk6-tracetest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceId.go
62 lines (50 loc) · 1.48 KB
/
traceId.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
package models
import (
cr "crypto/rand"
"encoding/binary"
"encoding/hex"
"fmt"
)
const (
K6Prefix = 0756 // Being 075 the ASCII code for 'K' :)
K6Code_Cloud = 12 // To ingest and process the related spans in k6 Cloud.
K6Code_Local = 33 // To not ingest and process the related spans, b/c they are part of a non-cloud run.
)
type TraceID struct {
Prefix int16
Code int8
UnixTimestampNano uint64
}
func (t *TraceID) IsValid() bool {
return t.Prefix == K6Prefix && (t.Code == K6Code_Cloud || t.Code == K6Code_Local)
}
func (t *TraceID) IsValidCloud() bool {
return t.Prefix == K6Prefix && t.Code == K6Code_Cloud
}
func (t *TraceID) Encode() (string, []byte, error) {
if !t.IsValid() {
return "", nil, fmt.Errorf("failed to encode traceID: %v", t)
}
buf := make([]byte, 16)
n := binary.PutVarint(buf, int64(t.Prefix))
n += binary.PutVarint(buf[n:], int64(t.Code))
n += binary.PutUvarint(buf[n:], t.UnixTimestampNano)
randomness := make([]byte, 16-len(buf[:n]))
err := binary.Read(cr.Reader, binary.BigEndian, randomness)
if err != nil {
return "", nil, err
}
buf = append(buf[:n], randomness[:]...)
hx := hex.EncodeToString(buf)
return hx, buf, nil
}
func DecodeTraceID(buf []byte) *TraceID {
pre, preLen := binary.Varint(buf)
code, codeLen := binary.Varint(buf[preLen:])
ts, _ := binary.Uvarint(buf[preLen+codeLen:])
return &TraceID{
Prefix: int16(pre),
Code: int8(code),
UnixTimestampNano: uint64(ts),
}
}