-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
91 lines (77 loc) · 2.13 KB
/
convert.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
package zipkin
import (
"bytes"
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
zkcore "github.com/mattkanwisher/distributedtrace/gen/zipkincore"
)
func convertBinaryAnnotationFromThrift(ann *zkcore.BinaryAnnotation) (string, interface{}, error) {
buffer := &thrift.TMemoryBuffer{}
buffer.Buffer = bytes.NewBuffer(ann.Value)
decoder := thrift.NewTBinaryProtocolTransport(buffer)
var val interface{}
var e error
switch ann.AnnotationType {
case zkcore.AnnotationType_BOOL:
val, e = decoder.ReadBool()
case zkcore.AnnotationType_BYTES:
val, e = ann.Value, nil
case zkcore.AnnotationType_I16:
val, e = decoder.ReadI16()
case zkcore.AnnotationType_I32:
val, e = decoder.ReadI32()
case zkcore.AnnotationType_I64:
val, e = decoder.ReadI64()
case zkcore.AnnotationType_DOUBLE:
val, e = decoder.ReadDouble()
case zkcore.AnnotationType_STRING:
// val, e = decoder.ReadString()
val, e = string(ann.Value), nil
default:
e = fmt.Errorf("unrecognized AnnotationType: %#v", ann.AnnotationType)
}
if e != nil {
return "", nil, e
}
return ann.Key, val, nil
}
func convertSpanToOutputMap(config *Config, span *zkcore.Span) (OutputMap, error) {
fields := OutputMap{
"id": span.Id,
"traceId": span.TraceId,
"name": span.Name,
}
if span.ParentId != nil {
fields["parentId"] = *span.ParentId
} else {
fields["parentId"] = nil
}
// flatten annotations into k/v map for the series
for _, ann := range span.Annotations {
fields[ann.Value] = ann.Timestamp
if ann.Duration != nil {
fields[ann.Value+"-duration"] = *ann.Duration
}
}
for _, ann := range span.BinaryAnnotations {
// TODO: TMemoryBuffer embeds *bytes.Buffer so we may not even need this Copy.
key, value, e := convertBinaryAnnotationFromThrift(ann)
if e != nil {
return nil, e
}
fields[key] = value
}
// computes client duration if cs and cr values present.
if cs, ok := fields.CS(); ok {
if cr, ok := fields.CR(); ok {
fields["cd"] = cr - cs
}
}
// computes server duration if ss and sr values present.
if ss, ok := fields.SS(); ok {
if sr, ok := fields.SR(); ok {
fields["sd"] = ss - sr
}
}
return fields, nil
}