forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
290 lines (261 loc) · 6.84 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
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
// 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 (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"math"
"net"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
)
type endpoint struct {
ServiceName string `json:"serviceName"`
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
Port int32 `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 interface{} `json:"value"`
Type string `json:"type"`
}
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 (
errWrongIpv4 = errors.New("wrong ipv4")
errWrongIpv6 = errors.New("wrong ipv6")
)
// DeserializeJSON deserialize zipkin v1 json spans into zipkin thrift
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) {
id, err := model.SpanIDFromString(cutLongID(s.ID))
if err != nil {
return nil, err
}
traceID, err := model.TraceIDFromString(s.TraceID)
if err != nil {
return nil, err
}
tSpan := &zipkincore.Span{
ID: int64(id),
TraceID: int64(traceID.Low),
Name: s.Name,
Debug: s.Debug,
Timestamp: s.Timestamp,
Duration: s.Duration,
}
if traceID.High != 0 {
help := int64(traceID.High)
tSpan.TraceIDHigh = &help
}
if len(s.ParentID) > 0 {
parentID, err := model.SpanIDFromString(cutLongID(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
}
// id can be padded with zeros. We let it fail later in case it's longer than 32
func cutLongID(id string) string {
l := len(id)
if l > 16 && l <= 32 {
start := l - 16
return id[start:]
}
return id
}
func endpointToThrift(e endpoint) (*zipkincore.Endpoint, error) {
return eToThrift(e.IPv4, e.IPv6, e.Port, e.ServiceName)
}
func eToThrift(ip4 string, ip6 string, p int32, service string) (*zipkincore.Endpoint, error) {
ipv4, err := parseIpv4(ip4)
if err != nil {
return nil, err
}
port := port(p)
ipv6, err := parseIpv6(string(ip6))
if err != nil {
return nil, err
}
return &zipkincore.Endpoint{
ServiceName: service,
Port: int16(port),
Ipv4: ipv4,
Ipv6: ipv6,
}, nil
}
func port(p int32) int32 {
if p >= (1 << 15) {
// Zipkin.thrift defines port as i16, so values between (2^15 and 2^16-1) must be encoded as negative
p = p - (1 << 16)
}
return p
}
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
}
var val []byte
var valType zipkincore.AnnotationType
switch ba.Type {
case "BOOL":
if ba.Value.(bool) {
val = []byte{1}
} else {
val = []byte{0}
}
valType = zipkincore.AnnotationType_BOOL
case "I16":
buff := new(bytes.Buffer)
binary.Write(buff, binary.LittleEndian, int16(ba.Value.(float64)))
val = buff.Bytes()
valType = zipkincore.AnnotationType_I16
case "I32":
buff := new(bytes.Buffer)
binary.Write(buff, binary.LittleEndian, int32(ba.Value.(float64)))
val = buff.Bytes()
valType = zipkincore.AnnotationType_I32
case "I64":
buff := new(bytes.Buffer)
binary.Write(buff, binary.LittleEndian, int64(ba.Value.(float64)))
val = buff.Bytes()
valType = zipkincore.AnnotationType_I64
case "DOUBLE":
val = float64bytes(ba.Value.(float64))
valType = zipkincore.AnnotationType_DOUBLE
case "BYTES":
val, err = base64.StdEncoding.DecodeString(ba.Value.(string))
if err != nil {
return nil, err
}
valType = zipkincore.AnnotationType_BYTES
case "STRING":
fallthrough
default:
str := fmt.Sprintf("%s", ba.Value)
val = []byte(str)
valType = zipkincore.AnnotationType_STRING
}
return &zipkincore.BinaryAnnotation{
Key: ba.Key,
Value: val,
Host: endpoint,
AnnotationType: valType,
}, nil
}
// taken from https://stackoverflow.com/a/22492518/4158442
func float64bytes(float float64) []byte {
bits := math.Float64bits(float)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, bits)
return bytes
}
func parseIpv6(str string) (net.IP, error) {
if str == "" {
return nil, nil
}
ip := net.ParseIP(str).To16()
if ip == nil {
return nil, errWrongIpv6
}
return ip, nil
}
func parseIpv4(str string) (int32, error) {
if str == "" {
return 0, nil
}
ip := net.ParseIP(str).To4()
if ip == nil {
return 0, errWrongIpv4
}
var ipv4 int32
for _, segment := range ip {
parsed32 := int32(segment)
ipv4 = ipv4<<8 | (parsed32 & 0xff)
}
return ipv4, nil
}