forked from flachnetz/dd-zipkin-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-v1.go
246 lines (206 loc) · 6.1 KB
/
json-v1.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
package codec
import (
"github.com/flachnetz/dd-zipkin-proxy/codec/hyperjson"
"github.com/flachnetz/dd-zipkin-proxy/proxy"
"github.com/modern-go/reflect2"
"github.com/pkg/errors"
"io"
"sync"
"time"
"unsafe"
)
type spanV1 struct {
TraceID Id `json:"traceId"`
ID Id `json:"id"`
ParentID Id `json:"parentId"`
Annotations [4]annotationV1 `json:"annotations"`
BinaryAnnotations []binaryAnnotationV1 `json:"binaryAnnotations"`
Name string `json:"name"`
Timestamp uint64 `json:"timestamp"`
Duration uint64 `json:"duration"`
}
type annotationV1 struct {
Timestamp uint64 `json:"timestamp"`
Value string `json:"value"`
Endpoint endpoint `json:"endpoint"`
}
type binaryAnnotationV1 struct {
Key string `json:"key"`
Value string `json:"value"`
Endpoint endpoint `json:"endpoint"`
}
type endpoint struct {
ServiceName string `json:"serviceName"`
}
var poolSpansV1 = sync.Pool{
New: func() interface{} {
return make([]spanV1, 64)
},
}
func ParseJsonV1(input io.Reader) ([]proxy.Span, error) {
// get a buffer to re-use
buf := bufferPool.Get()
defer bufferPool.Put(buf)
// get a new reader and initialize it with the pooled buffer.
r := hyperjson.NewWithReader(input, buf.([]byte))
// we know, that the reader wont escape, sadly, go doesnt know that,
// so we give it a little hint
p := (*hyperjson.Parser)(reflect2.NoEscape(unsafe.Pointer(r)))
// we can also re-use the slice we use for decoding
ifSlice := poolSpansV1.Get()
defer poolSpansV1.Put(ifSlice)
// decode spans into the span slice.
decoded := ifSlice.([]spanV1)[:0]
if err := decoderSliceSpanV1(reflect2.NoEscape(unsafe.Pointer(&decoded)), p); err != nil {
return nil, errors.WithMessage(err, "parse spans for json v1")
}
parsedSpans := make([]proxy.Span, len(decoded))
for idx, span := range decoded {
parsedSpans[idx] = span.ToSpan()
}
return parsedSpans, nil
}
func (span *spanV1) ToSpan() proxy.Span {
proxySpan := proxy.NewSpan(span.Name, span.TraceID, span.ID, span.ParentID)
for _, annotation := range span.Annotations {
if annotation.Timestamp == 0 {
continue
}
proxySpan.AddTiming(annotation.Value,
proxy.Microseconds(int64(annotation.Timestamp)))
if proxySpan.Service == "" && annotation.Endpoint.ServiceName != "" {
proxySpan.Service = annotation.Endpoint.ServiceName
}
}
proxySpan.Tags = make(map[string]string, 1+len(span.BinaryAnnotations))
for _, annotation := range span.BinaryAnnotations {
proxySpan.AddTag(annotation.Key, annotation.Value)
if proxySpan.Service == "" && annotation.Endpoint.ServiceName != "" {
proxySpan.Service = annotation.Endpoint.ServiceName
}
}
proxySpan.AddTag(tagProtocolVersion, tagJsonV1)
if span.Timestamp != 0 {
proxySpan.Timestamp = proxy.Microseconds(int64(span.Timestamp))
}
if span.Duration != 0 {
proxySpan.Duration = time.Duration(span.Duration) * time.Microsecond
}
fillInTimestamp(&proxySpan)
return proxySpan
}
func fillInTimestamp(proxySpan *proxy.Span) {
sr := proxySpan.Timings.SR
ss := proxySpan.Timings.SS
if sr > 0 && ss > 0 {
proxySpan.Timestamp = sr
proxySpan.Duration = time.Duration(ss - sr)
}
cs := proxySpan.Timings.CS
cr := proxySpan.Timings.CR
if cs > 0 && cr > 0 {
proxySpan.Timestamp = cs
proxySpan.Duration = time.Duration(cr - cs)
}
if proxySpan.Duration == 0 {
proxySpan.Duration = 1 * time.Millisecond
}
}
func spanV1ValueDecoder() hyperjson.ValueDecoder {
decoder := hyperjson.MakeStructDecoder([]hyperjson.Field{
{
JsonName: "id",
Offset: hyperjson.OffsetOf(spanV1{}, "ID"),
Decoder: idValueDecoder,
},
{
JsonName: "traceId",
Offset: hyperjson.OffsetOf(spanV1{}, "TraceID"),
Decoder: idValueDecoder,
},
{
JsonName: "parentId",
Offset: hyperjson.OffsetOf(spanV1{}, "ParentID"),
Decoder: idValueDecoder,
},
{
JsonName: "timestamp",
Offset: hyperjson.OffsetOf(spanV1{}, "Timestamp"),
Decoder: hyperjson.Uint64ValueDecoder,
},
{
JsonName: "duration",
Offset: hyperjson.OffsetOf(spanV1{}, "Duration"),
Decoder: hyperjson.Uint64ValueDecoder,
},
{
JsonName: "name",
Offset: hyperjson.OffsetOf(spanV1{}, "Name"),
Decoder: hyperjson.StringValueDecoder,
},
{
JsonName: "binaryAnnotations",
Offset: hyperjson.OffsetOf(spanV1{}, "BinaryAnnotations"),
Decoder: hyperjson.MakeSliceDecoder(
reflect2.TypeOf([]binaryAnnotationV1{}).(reflect2.SliceType),
hyperjson.MakeStructDecoder([]hyperjson.Field{
{
JsonName: "key",
Offset: hyperjson.OffsetOf(binaryAnnotationV1{}, "Key"),
Decoder: hyperjson.StringValueDecoder,
},
{
JsonName: "value",
Offset: hyperjson.OffsetOf(binaryAnnotationV1{}, "Value"),
Decoder: anyValueDecoder,
},
{
JsonName: "endpoint",
Offset: hyperjson.OffsetOf(binaryAnnotationV1{}, "Endpoint"),
Decoder: endpointValueDecoder,
},
})),
},
{
JsonName: "annotations",
Offset: hyperjson.OffsetOf(spanV1{}, "Annotations"),
Decoder: hyperjson.MakeArrayDecoder(
reflect2.TypeOf([4]annotationV1{}).(reflect2.ArrayType),
hyperjson.MakeStructDecoder([]hyperjson.Field{
{
JsonName: "timestamp",
Offset: hyperjson.OffsetOf(annotationV1{}, "Timestamp"),
Decoder: hyperjson.Uint64ValueDecoder,
},
{
JsonName: "value",
Offset: hyperjson.OffsetOf(annotationV1{}, "Value"),
Decoder: hyperjson.StringValueDecoder,
},
{
JsonName: "endpoint",
Offset: hyperjson.OffsetOf(annotationV1{}, "Endpoint"),
Decoder: endpointValueDecoder,
},
})),
},
})
return func(target unsafe.Pointer, p *hyperjson.Parser) error {
span := (*spanV1)(target)
span.TraceID = 0
span.ID = 0
span.ParentID = 0
span.Duration = 0
span.Timestamp = 0
span.Name = ""
span.Annotations = [4]annotationV1{}
for idx := range span.BinaryAnnotations {
span.BinaryAnnotations[idx] = binaryAnnotationV1{}
}
span.BinaryAnnotations = span.BinaryAnnotations[:0]
return decoder(target, p)
}
}
var decoderSliceSpanV1 = hyperjson.MakeSliceDecoder(
reflect2.TypeOf([]spanV1{}).(reflect2.SliceType),
spanV1ValueDecoder())