forked from jaegertracing/jaeger
/
json.go
232 lines (204 loc) · 5.35 KB
/
json.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zipkin
import (
"encoding/json"
"errors"
"strconv"
"strings"
"github.com/uber/jaeger/thrift-gen/zipkincore"
)
type endpoint struct {
ServiceName string `json:"serviceName"`
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
Port int16 `json:"port"`
}
type annotation struct {
Endpoint endpoint `json:"endpoint"`
Value string `json:"value"`
Timestamp int64 `json:"timestamp"`
}
type binaryAnnotation struct {
Endpoint endpoint `json:"endpoint"`
Key string `json:"key"`
Value string `json:"value"`
}
type zipkinSpan struct {
ID string `json:"id"`
ParentID string `json:"parentId,omitempty"`
TraceID string `json:"traceId"`
Name string `json:"name"`
Timestamp *int64 `json:"timestamp"`
Duration *int64 `json:"duration"`
Debug bool `json:"debug"`
Annotations []annotation `json:"annotations"`
BinaryAnnotations []binaryAnnotation `json:"binaryAnnotations"`
}
var (
errIsNotUnsignedLog = errors.New("id is not an unsigned long")
errWrongIpv4 = errors.New("wrong ipv4")
)
func deserializeJSON(body []byte) ([]*zipkincore.Span, error) {
spans, err := decode(body)
if err != nil {
return nil, err
}
return spansToThrift(spans)
}
func decode(body []byte) ([]zipkinSpan, error) {
var spans []zipkinSpan
if err := json.Unmarshal(body, &spans); err != nil {
return nil, err
}
return spans, nil
}
func spansToThrift(spans []zipkinSpan) ([]*zipkincore.Span, error) {
var tSpans []*zipkincore.Span
for _, span := range spans {
tSpan, err := spanToThrift(span)
if err != nil {
return nil, err
}
tSpans = append(tSpans, tSpan)
}
return tSpans, nil
}
func spanToThrift(s zipkinSpan) (*zipkincore.Span, error) {
// TODO use model.SpanIDFromString and model.TraceIDFromString
id, err := hexToUnsignedLong(s.ID)
if err != nil {
return nil, err
}
traceID, err := hexToUnsignedLong(s.TraceID)
if err != nil {
return nil, err
}
tSpan := &zipkincore.Span{
ID: int64(id),
TraceID: int64(traceID),
Name: s.Name,
Debug: s.Debug,
Timestamp: s.Timestamp,
Duration: s.Duration,
}
if len(s.TraceID) == 32 {
// take 0-16
traceIDHigh, err := hexToUnsignedLong(s.TraceID[:16])
if err != nil {
return nil, err
}
help := int64(traceIDHigh)
tSpan.TraceIDHigh = &help
}
if len(s.ParentID) > 0 {
parentID, err := hexToUnsignedLong(s.ParentID)
if err != nil {
return nil, err
}
signed := int64(parentID)
tSpan.ParentID = &signed
}
for _, a := range s.Annotations {
tA, err := annoToThrift(a)
if err != nil {
return nil, err
}
tSpan.Annotations = append(tSpan.Annotations, tA)
}
for _, ba := range s.BinaryAnnotations {
tBa, err := binAnnoToThrift(ba)
if err != nil {
return nil, err
}
tSpan.BinaryAnnotations = append(tSpan.BinaryAnnotations, tBa)
}
return tSpan, nil
}
func endpointToThrift(e endpoint) (*zipkincore.Endpoint, error) {
ipv4, err := parseIpv4(e.IPv4)
if err != nil {
return nil, err
}
return &zipkincore.Endpoint{
ServiceName: e.ServiceName,
Port: e.Port,
Ipv4: ipv4,
Ipv6: []byte(e.IPv6),
}, nil
}
func annoToThrift(a annotation) (*zipkincore.Annotation, error) {
endpoint, err := endpointToThrift(a.Endpoint)
if err != nil {
return nil, err
}
return &zipkincore.Annotation{
Timestamp: a.Timestamp,
Value: a.Value,
Host: endpoint,
}, nil
}
func binAnnoToThrift(ba binaryAnnotation) (*zipkincore.BinaryAnnotation, error) {
endpoint, err := endpointToThrift(ba.Endpoint)
if err != nil {
return nil, err
}
return &zipkincore.BinaryAnnotation{
Key: ba.Key,
Value: []byte(ba.Value),
Host: endpoint,
AnnotationType: zipkincore.AnnotationType_STRING,
}, nil
}
func parseIpv4(str string) (int32, error) {
// TODO use net.ParseIP
segments := strings.Split(str, ".")
if len(segments) == 1 {
return 0, nil
}
var ipv4 int32
for _, segment := range segments {
parsed, err := strconv.ParseInt(segment, 10, 32)
if err != nil {
return 0, errWrongIpv4
}
parsed32 := int32(parsed)
ipv4 = ipv4<<8 | (parsed32 & 0xff)
}
return ipv4, nil
}
func hexToUnsignedLong(hex string) (uint64, error) {
// TODO remove this func in favor of model.XxxFromString methods
len := len(hex)
if len < 1 || len > 32 {
return 0, errIsNotUnsignedLog
}
start := 0
if len > 16 {
start = len - 16
}
var result uint64
for i := start; i < len; i++ {
c := hex[i]
result <<= 4
if c >= '0' && c <= '9' {
result = result | uint64(c-'0')
} else if c >= 'a' && c <= 'f' {
result = result | uint64(c-'a'+10)
} else {
return 0, errIsNotUnsignedLog
}
}
return result, nil
}