/
w3c.go
168 lines (149 loc) · 3.97 KB
/
w3c.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
package stdlib
import (
"crypto/rand"
"encoding/hex"
"fmt"
"strings"
)
const (
INVALID_VALUE_COUNT = "invalid number of values"
VERSION_INVALID_LENGTH = "invalid version length"
TRACEID_INVALID_LENGTH = "invalid traceid length"
PARENTID_INVALID_LENGTH = "invalid parentid length"
FLAG_INVALID_LENGTH = "invalid flag length"
TRACEID_IS_ZERO = "error traceid value is zero"
PARENTID_IS_ZERO = "error parentid value is zero"
)
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
return nil, err
} else {
return b, nil
}
}
func GenerateTraceIdRaw() ([]byte, error) {
if raw, err := GenerateRandomBytes(16); err != nil {
return nil, fmt.Errorf("failed to generate bytes %w", err)
} else {
return raw, nil
}
}
func GenerateTraceId() (string, error) {
if raw, err := GenerateTraceIdRaw(); err != nil {
return "", fmt.Errorf("failed to generate trace Id %w", err)
} else {
return hex.EncodeToString(raw), nil
}
}
func GenerateParentIdRaw() ([]byte, error) {
if raw, err := GenerateRandomBytes(8); err != nil {
return nil, fmt.Errorf("failed to generate butes %w", err)
} else {
return raw, nil
}
}
// Counting on go compiler to inline these plsplspls :)
func GenerateParentId() (string, error) {
if raw, err := GenerateParentIdRaw(); err != nil {
return "", fmt.Errorf("failed to generate parent Id %w", err)
} else {
return hex.EncodeToString(raw), nil
}
}
func GenerateNewTraceparent(sampled bool) (string, error) {
var flag string
if sampled {
flag = "01"
} else {
flag = "00"
}
tid, err := GenerateTraceId()
if err != nil {
return "", fmt.Errorf("failed to generate traceId %w", err)
}
pid, err := GenerateParentId()
if err != nil {
return "", fmt.Errorf("failed to generate parentId %w", err)
}
return fmt.Sprintf(
"00-%s-%s-%s",
tid,
pid,
flag,
), nil
}
func ParseTraceparentRaw(
traceparent string,
) ([]byte, []byte, []byte, []byte, error) {
values := strings.Split(traceparent, "-")
if len(values) != 4 {
return nil, nil, nil, nil, fmt.Errorf(INVALID_VALUE_COUNT)
}
vers, err := hex.DecodeString(values[0])
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to decode version %w", err)
}
if len(vers) != 1 {
return nil, nil, nil, nil, fmt.Errorf(VERSION_INVALID_LENGTH)
}
tid, err := hex.DecodeString(values[1])
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to decode traceId %w", err)
}
if len(tid) != 16 {
return nil, nil, nil, nil, fmt.Errorf(TRACEID_INVALID_LENGTH)
}
pid, err := hex.DecodeString(values[2])
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to decode parentId %w", err)
}
if len(pid) != 8 {
return nil, nil, nil, nil, fmt.Errorf(PARENTID_INVALID_LENGTH)
}
flg, err := hex.DecodeString(values[3])
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to decode flag %w", err)
}
if len(flg) != 1 {
return nil, nil, nil, nil, fmt.Errorf(FLAG_INVALID_LENGTH)
}
return vers, tid, pid, flg, nil
}
func ParseTraceparent(
traceparent string,
) (string, string, string, string, error) {
values := strings.Split(traceparent, "-")
if len(values) != 4 {
return "", "", "", "", fmt.Errorf(INVALID_VALUE_COUNT)
}
if len(values[0]) != 2 {
return "", "", "", "", fmt.Errorf(VERSION_INVALID_LENGTH)
}
if len(values[1]) != 32 {
return "", "", "", "", fmt.Errorf(TRACEID_INVALID_LENGTH)
}
if len(values[2]) != 16 {
return "", "", "", "", fmt.Errorf(PARENTID_INVALID_LENGTH)
}
if len(values[3]) != 2 {
return "", "", "", "", fmt.Errorf(FLAG_INVALID_LENGTH)
}
return values[0], values[1], values[2], values[3], nil
}
func ValidateTraceIdValue(traceId []byte) error {
for idx := 0; idx < len(traceId); idx++ {
if traceId[idx] != 0 {
return nil
}
}
return fmt.Errorf(TRACEID_IS_ZERO)
}
func ValidateParentIdValue(parentId []byte) error {
for idx := 0; idx < len(parentId); idx++ {
if parentId[idx] != 0 {
return nil
}
}
return fmt.Errorf(TRACEID_IS_ZERO)
}