-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
trace_encoding.go
136 lines (112 loc) · 3.12 KB
/
trace_encoding.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
package tracing
import (
"math"
"time"
"github.com/gogo/protobuf/proto"
"github.com/influxdata/influxdb/pkg/tracing/fields"
"github.com/influxdata/influxdb/pkg/tracing/labels"
"github.com/influxdata/influxdb/pkg/tracing/wire"
)
func fieldsToWire(set fields.Fields) []wire.Field {
var r []wire.Field
for _, f := range set {
wf := wire.Field{Key: f.Key()}
switch val := f.Value().(type) {
case string:
wf.FieldType = wire.FieldTypeString
wf.Value = &wire.Field_StringVal{StringVal: val}
case bool:
var numericVal int64
if val {
numericVal = 1
}
wf.FieldType = wire.FieldTypeBool
wf.Value = &wire.Field_NumericVal{NumericVal: numericVal}
case int64:
wf.FieldType = wire.FieldTypeInt64
wf.Value = &wire.Field_NumericVal{NumericVal: val}
case uint64:
wf.FieldType = wire.FieldTypeUint64
wf.Value = &wire.Field_NumericVal{NumericVal: int64(val)}
case time.Duration:
wf.FieldType = wire.FieldTypeDuration
wf.Value = &wire.Field_NumericVal{NumericVal: int64(val)}
case float64:
wf.FieldType = wire.FieldTypeFloat64
wf.Value = &wire.Field_NumericVal{NumericVal: int64(math.Float64bits(val))}
default:
continue
}
r = append(r, wf)
}
return r
}
func labelsToWire(set labels.Labels) []string {
var r []string
for i := range set {
r = append(r, set[i].Key, set[i].Value)
}
return r
}
func (t *Trace) MarshalBinary() ([]byte, error) {
wt := wire.Trace{}
for _, sp := range t.spans {
wt.Spans = append(wt.Spans, &wire.Span{
Context: wire.SpanContext{
TraceID: sp.Context.TraceID,
SpanID: sp.Context.SpanID,
},
ParentSpanID: sp.ParentSpanID,
Name: sp.Name,
Start: sp.Start,
Labels: labelsToWire(sp.Labels),
Fields: fieldsToWire(sp.Fields),
})
}
return proto.Marshal(&wt)
}
func wireToFields(wfs []wire.Field) fields.Fields {
var fs []fields.Field
for _, wf := range wfs {
switch wf.FieldType {
case wire.FieldTypeString:
fs = append(fs, fields.String(wf.Key, wf.GetStringVal()))
case wire.FieldTypeBool:
var boolVal bool
if wf.GetNumericVal() != 0 {
boolVal = true
}
fs = append(fs, fields.Bool(wf.Key, boolVal))
case wire.FieldTypeInt64:
fs = append(fs, fields.Int64(wf.Key, wf.GetNumericVal()))
case wire.FieldTypeUint64:
fs = append(fs, fields.Uint64(wf.Key, uint64(wf.GetNumericVal())))
case wire.FieldTypeDuration:
fs = append(fs, fields.Duration(wf.Key, time.Duration(wf.GetNumericVal())))
case wire.FieldTypeFloat64:
fs = append(fs, fields.Float64(wf.Key, math.Float64frombits(uint64(wf.GetNumericVal()))))
}
}
return fields.New(fs...)
}
func (t *Trace) UnmarshalBinary(data []byte) error {
var wt wire.Trace
if err := proto.Unmarshal(data, &wt); err != nil {
return err
}
t.spans = make(map[uint64]RawSpan)
for _, sp := range wt.Spans {
t.spans[sp.Context.SpanID] = RawSpan{
Context: SpanContext{
TraceID: sp.Context.TraceID,
SpanID: sp.Context.SpanID,
},
ParentSpanID: sp.ParentSpanID,
Name: sp.Name,
Start: sp.Start,
Labels: labels.New(sp.Labels...),
Fields: wireToFields(sp.Fields),
}
}
return nil
}