-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtraceid.go
200 lines (171 loc) · 3.92 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
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
package klogga
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"github.com/google/uuid"
"github.com/pkg/errors"
)
type TraceID uuid.UUID
const TraceIDSize = 16
func NewTraceID() TraceID {
return TraceID(uuid.New())
}
func (t TraceID) IsZero() bool {
return t == TraceID{}
}
func (t TraceID) AsUUID() uuid.UUID {
return uuid.UUID(t)
}
func (t TraceID) Bytes() []byte {
return t[:]
}
func (t TraceID) AsNullableUUID() *uuid.UUID {
if t.IsZero() {
return nil
}
u := uuid.UUID(t)
return &u
}
func TraceIDFromBytes(bb []byte) (TraceID, error) {
if len(bb) != TraceIDSize {
return TraceID{}, errors.Errorf("TraceIDFromString: wrong TraceID size %v expected %v", len(bb), TraceIDSize)
}
res, err := uuid.ParseBytes(bb)
return TraceID(res), err
}
func TraceIDFromBytesOrZero(bb []byte) TraceID {
res, err := uuid.ParseBytes(bb)
if err != nil {
return TraceID{}
}
return TraceID(res)
}
func TraceIDFromString(traceID string) (res TraceID, err error) {
bytes, err := base64.RawURLEncoding.DecodeString(traceID)
if err != nil {
return TraceID{}, err
}
if len(bytes) == 0 {
return TraceID{}, nil
}
if len(bytes) != len(res) {
return TraceID{}, errors.Errorf("TraceIDFromString: wrong TraceID size %v expected %v", len(bytes), TraceIDSize)
}
copy(res[:], bytes)
return res, err
}
// default storage and string format for TraceID is base64!
func (t TraceID) String() string {
if t.IsZero() {
return ""
}
return base64.RawURLEncoding.EncodeToString(t[:])
}
func (t TraceID) MarshalText() ([]byte, error) {
if t.IsZero() {
return nil, nil
}
return []byte(base64.RawURLEncoding.EncodeToString(t[:])), nil
}
func (t TraceID) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
func (t *TraceID) UnmarshalJSON(bb []byte) error {
var str string
if err := json.Unmarshal(bb, &str); err != nil {
return err
}
tt, err := TraceIDFromString(str)
if err != nil {
return err
}
*t = tt
return err
}
// SpanID like in OpenTelemetry
type SpanID [8]byte
const SpanIDSize = 8
func (s SpanID) IsZero() bool {
return s == SpanID{}
}
func NewSpanID() (res SpanID) {
_, _ = rand.Read(res[:])
return res
}
// default string format for SpanID is base64!
func (s SpanID) String() string {
if s.IsZero() {
return ""
}
return base64.RawURLEncoding.EncodeToString(s[:])
}
func (s SpanID) Bytes() []byte {
return s[:]
}
func (s SpanID) AsNullableBytes() []byte {
if s.IsZero() {
return nil
}
return s[:]
}
func ParseSpanID(spanID string) (res SpanID, err error) {
bytes, err := base64.RawURLEncoding.DecodeString(spanID)
if err != nil {
return SpanID{}, err
}
if len(bytes) != SpanIDSize {
return SpanID{}, errors.Errorf("ParseSpanID: wrong SpanID size %v expected %v", len(bytes), SpanIDSize)
}
copy(res[:], bytes)
return res, err
}
func (s SpanID) MarshalText() ([]byte, error) {
if s.IsZero() {
return nil, nil
}
return []byte(s.String()), nil
}
func (s SpanID) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *SpanID) UnmarshalJSON(bb []byte) error {
var str string
if err := json.Unmarshal(bb, &str); err != nil {
return err
}
tt, err := SpanIDFromString(str)
if err != nil {
return err
}
*s = tt
return err
}
func SpanIDFromBytes(bb []byte) (res SpanID, err error) {
if len(bb) != SpanIDSize {
return SpanID{}, errors.Errorf("SpanIDFromBytes: wrong SpanID size %v expected %v", len(bb), SpanIDSize)
}
copy(res[:], bb)
return res, nil
}
func SpanIDFromBytesOrZero(bb []byte) SpanID {
res, err := SpanIDFromBytes(bb)
if err != nil {
return SpanID{}
}
return res
}
func SpanIDFromString(s string) (res SpanID, err error) {
bytes, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
return SpanID{}, err
}
if len(bytes) == 0 {
return SpanID{}, nil
}
if len(bytes) != SpanIDSize {
return SpanID{}, errors.Errorf("SpanIDFromString: wrong SpanID size %v expected %v", len(bytes), SpanIDSize)
}
copy(res[:], bytes)
return res, err
}