This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 288
/
zipkin_thrift_span.go
329 lines (301 loc) Β· 9.42 KB
/
zipkin_thrift_span.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// 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 jaeger
import (
"encoding/binary"
"fmt"
"time"
"github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger-client-go/internal/spanlog"
z "github.com/uber/jaeger-client-go/thrift-gen/zipkincore"
"github.com/uber/jaeger-client-go/utils"
)
const (
// Zipkin UI does not work well with non-string tag values
allowPackedNumbers = false
)
var specialTagHandlers = map[string]func(*zipkinSpan, interface{}){
string(ext.SpanKind): setSpanKind,
string(ext.PeerHostIPv4): setPeerIPv4,
string(ext.PeerPort): setPeerPort,
string(ext.PeerService): setPeerService,
TracerIPTagKey: removeTag,
}
// BuildZipkinThrift builds thrift span based on internal span.
// TODO: (breaking change) move to transport/zipkin and make private.
func BuildZipkinThrift(s *Span) *z.Span {
span := &zipkinSpan{Span: s}
span.handleSpecialTags()
parentID := int64(span.context.parentID)
var ptrParentID *int64
if parentID != 0 {
ptrParentID = &parentID
}
traceIDHigh := int64(span.context.traceID.High)
var ptrTraceIDHigh *int64
if traceIDHigh != 0 {
ptrTraceIDHigh = &traceIDHigh
}
timestamp := utils.TimeToMicrosecondsSinceEpochInt64(span.startTime)
duration := span.duration.Nanoseconds() / int64(time.Microsecond)
endpoint := &z.Endpoint{
ServiceName: span.tracer.serviceName,
Ipv4: int32(span.tracer.hostIPv4)}
thriftSpan := &z.Span{
TraceID: int64(span.context.traceID.Low),
TraceIDHigh: ptrTraceIDHigh,
ID: int64(span.context.spanID),
ParentID: ptrParentID,
Name: span.operationName,
Timestamp: ×tamp,
Duration: &duration,
Debug: span.context.IsDebug(),
Annotations: buildAnnotations(span, endpoint),
BinaryAnnotations: buildBinaryAnnotations(span, endpoint)}
return thriftSpan
}
func buildAnnotations(span *zipkinSpan, endpoint *z.Endpoint) []*z.Annotation {
// automatically adding 2 Zipkin CoreAnnotations
annotations := make([]*z.Annotation, 0, 2+len(span.logs))
var startLabel, endLabel string
if span.spanKind == string(ext.SpanKindRPCClientEnum) {
startLabel, endLabel = z.CLIENT_SEND, z.CLIENT_RECV
} else if span.spanKind == string(ext.SpanKindRPCServerEnum) {
startLabel, endLabel = z.SERVER_RECV, z.SERVER_SEND
}
if !span.startTime.IsZero() && startLabel != "" {
start := &z.Annotation{
Timestamp: utils.TimeToMicrosecondsSinceEpochInt64(span.startTime),
Value: startLabel,
Host: endpoint}
annotations = append(annotations, start)
if span.duration != 0 {
endTs := span.startTime.Add(span.duration)
end := &z.Annotation{
Timestamp: utils.TimeToMicrosecondsSinceEpochInt64(endTs),
Value: endLabel,
Host: endpoint}
annotations = append(annotations, end)
}
}
for _, log := range span.logs {
anno := &z.Annotation{
Timestamp: utils.TimeToMicrosecondsSinceEpochInt64(log.Timestamp),
Host: endpoint}
if content, err := spanlog.MaterializeWithJSON(log.Fields); err == nil {
anno.Value = truncateString(string(content), span.tracer.options.maxTagValueLength)
} else {
anno.Value = err.Error()
}
annotations = append(annotations, anno)
}
return annotations
}
func buildBinaryAnnotations(span *zipkinSpan, endpoint *z.Endpoint) []*z.BinaryAnnotation {
// automatically adding local component or server/client address tag, and client version
annotations := make([]*z.BinaryAnnotation, 0, 2+len(span.tags))
if span.peerDefined() && span.isRPC() {
peer := z.Endpoint{
Ipv4: span.peer.Ipv4,
Port: span.peer.Port,
ServiceName: span.peer.ServiceName}
label := z.CLIENT_ADDR
if span.isRPCClient() {
label = z.SERVER_ADDR
}
anno := &z.BinaryAnnotation{
Key: label,
Value: []byte{1},
AnnotationType: z.AnnotationType_BOOL,
Host: &peer}
annotations = append(annotations, anno)
}
if !span.isRPC() {
componentName := endpoint.ServiceName
for _, tag := range span.tags {
if tag.key == string(ext.Component) {
componentName = stringify(tag.value)
break
}
}
local := &z.BinaryAnnotation{
Key: z.LOCAL_COMPONENT,
Value: []byte(componentName),
AnnotationType: z.AnnotationType_STRING,
Host: endpoint}
annotations = append(annotations, local)
}
for _, tag := range span.tags {
// "Special tags" are already handled by this point, we'd be double reporting the
// tags if we don't skip here
if _, ok := specialTagHandlers[tag.key]; ok {
continue
}
if anno := buildBinaryAnnotation(tag.key, tag.value, span.tracer.options.maxTagValueLength, nil); anno != nil {
annotations = append(annotations, anno)
}
}
return annotations
}
func buildBinaryAnnotation(key string, val interface{}, maxTagValueLength int, endpoint *z.Endpoint) *z.BinaryAnnotation {
bann := &z.BinaryAnnotation{Key: key, Host: endpoint}
if value, ok := val.(string); ok {
bann.Value = []byte(truncateString(value, maxTagValueLength))
bann.AnnotationType = z.AnnotationType_STRING
} else if value, ok := val.([]byte); ok {
if len(value) > maxTagValueLength {
value = value[:maxTagValueLength]
}
bann.Value = value
bann.AnnotationType = z.AnnotationType_BYTES
} else if value, ok := val.(int32); ok && allowPackedNumbers {
bann.Value = int32ToBytes(value)
bann.AnnotationType = z.AnnotationType_I32
} else if value, ok := val.(int64); ok && allowPackedNumbers {
bann.Value = int64ToBytes(value)
bann.AnnotationType = z.AnnotationType_I64
} else if value, ok := val.(int); ok && allowPackedNumbers {
bann.Value = int64ToBytes(int64(value))
bann.AnnotationType = z.AnnotationType_I64
} else if value, ok := val.(bool); ok {
bann.Value = []byte{boolToByte(value)}
bann.AnnotationType = z.AnnotationType_BOOL
} else {
value := stringify(val)
bann.Value = []byte(truncateString(value, maxTagValueLength))
bann.AnnotationType = z.AnnotationType_STRING
}
return bann
}
func stringify(value interface{}) string {
if s, ok := value.(string); ok {
return s
}
return fmt.Sprintf("%+v", value)
}
func truncateString(value string, maxLength int) string {
// we ignore the problem of utf8 runes possibly being sliced in the middle,
// as it is rather expensive to iterate through each tag just to find rune
// boundaries.
if len(value) > maxLength {
return value[:maxLength]
}
return value
}
func boolToByte(b bool) byte {
if b {
return 1
}
return 0
}
// int32ToBytes converts int32 to bytes.
func int32ToBytes(i int32) []byte {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(i))
return buf
}
// int64ToBytes converts int64 to bytes.
func int64ToBytes(i int64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}
type zipkinSpan struct {
*Span
// peer points to the peer service participating in this span,
// e.g. the Client if this span is a server span,
// or Server if this span is a client span
peer struct {
Ipv4 int32
Port int16
ServiceName string
}
// used to distinguish local vs. RPC Server vs. RPC Client spans
spanKind string
}
func (s *zipkinSpan) handleSpecialTags() {
s.Lock()
defer s.Unlock()
if s.firstInProcess {
// append the process tags
s.tags = append(s.tags, s.tracer.tags...)
}
filteredTags := make([]Tag, 0, len(s.tags))
for _, tag := range s.tags {
if handler, ok := specialTagHandlers[tag.key]; ok {
handler(s, tag.value)
} else {
filteredTags = append(filteredTags, tag)
}
}
s.tags = filteredTags
}
func setSpanKind(s *zipkinSpan, value interface{}) {
if val, ok := value.(string); ok {
s.spanKind = val
return
}
if val, ok := value.(ext.SpanKindEnum); ok {
s.spanKind = string(val)
}
}
func setPeerIPv4(s *zipkinSpan, value interface{}) {
if val, ok := value.(string); ok {
if ip, err := utils.ParseIPToUint32(val); err == nil {
s.peer.Ipv4 = int32(ip)
return
}
}
if val, ok := value.(uint32); ok {
s.peer.Ipv4 = int32(val)
return
}
if val, ok := value.(int32); ok {
s.peer.Ipv4 = val
}
}
func setPeerPort(s *zipkinSpan, value interface{}) {
if val, ok := value.(string); ok {
if port, err := utils.ParsePort(val); err == nil {
s.peer.Port = int16(port)
return
}
}
if val, ok := value.(uint16); ok {
s.peer.Port = int16(val)
return
}
if val, ok := value.(int); ok {
s.peer.Port = int16(val)
}
}
func setPeerService(s *zipkinSpan, value interface{}) {
if val, ok := value.(string); ok {
s.peer.ServiceName = val
}
}
func removeTag(s *zipkinSpan, value interface{}) {}
func (s *zipkinSpan) peerDefined() bool {
return s.peer.ServiceName != "" || s.peer.Ipv4 != 0 || s.peer.Port != 0
}
func (s *zipkinSpan) isRPC() bool {
s.RLock()
defer s.RUnlock()
return s.spanKind == string(ext.SpanKindRPCClientEnum) || s.spanKind == string(ext.SpanKindRPCServerEnum)
}
func (s *zipkinSpan) isRPCClient() bool {
s.RLock()
defer s.RUnlock()
return s.spanKind == string(ext.SpanKindRPCClientEnum)
}