-
Notifications
You must be signed in to change notification settings - Fork 1
/
combiner.go
187 lines (152 loc) · 3.99 KB
/
combiner.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
package zipkin
import (
"fmt"
"time"
zkcore "github.com/mattkanwisher/distributedtrace/gen/zipkincore"
)
// TODO: Turn this into an interface.
type Combiner struct {
config *Config
inputs map[int64]chan *zkcore.Span
output chan OutputMap
stopped bool
semaphore chan bool
}
func NewCombiner(config *Config) *Combiner {
combiner := &Combiner{
config: config,
inputs: map[int64]chan *zkcore.Span{},
output: make(chan OutputMap, config.OutputBufferSize),
stopped: false,
semaphore: make(chan bool, config.MaxConcurrentTraces),
}
// fill up the semaphore
for i := 0; i < config.MaxConcurrentTraces; i++ {
combiner.semaphore <- true
}
return combiner
}
// TODO: More graceful/waitable exit.
func (c *Combiner) Stop() {
c.stopped = true
for i := 0; i < c.config.MaxConcurrentTraces; i++ {
<-c.semaphore
}
for traceId, channel := range c.inputs {
close(channel)
delete(c.inputs, traceId)
}
close(c.output)
}
func (c *Combiner) Send(span *zkcore.Span) error {
if c.stopped {
return fmt.Errorf("combiner stopped.")
}
if channel, ok := c.inputs[span.TraceId]; ok {
channel <- span
return nil
}
channel := make(chan *zkcore.Span, c.config.InputBufferSize)
c.inputs[span.TraceId] = channel
go func() {
defer close(channel)
spans := []*zkcore.Span{}
timeout := time.After(c.config.TraceTimeout)
for_loop:
for {
select {
case s := <-channel:
spans = append(spans, s)
case <-timeout:
break for_loop
}
}
if outputs := c.combine(spans); len(outputs) > 0 {
for _, output := range outputs {
c.output <- output
}
}
}()
return c.Send(span)
}
func (c *Combiner) Receive() <-chan OutputMap {
return c.output
}
func (c *Combiner) combine(spans []*zkcore.Span) []OutputMap {
printf := c.config.Logger.Printf
defer func() {
if r := recover(); r != nil {
printf("Combine(): failed to combine %d span(s): %s", len(spans), r)
} else {
printf("Combine(): combined %d span(s) into output.", len(spans))
}
}()
const nameSep = "|"
// since nodes may not come in exact parent-child order, we need a lookup here first so
// we can reconstruct the tree properly.
lookup := map[int64]*tree{}
for _, span := range spans {
outputMap, e := convertSpanToOutputMap(c.config, span)
noError(e)
node := &tree{
id: span.Id,
parent: nil,
name: span.Name,
span: span,
outputMap: outputMap,
}
lookup[node.id] = node
}
// rebuild the tree
// TODO: Tree building should be a separate step. Then Combiner would be something more
// like a Flattener, or this could be decided at the Output level.
// TODO: panic() less.
var root *tree = nil
for _, node := range lookup {
switch {
case node.span.ParentId != nil:
if parentNode, exists := lookup[*node.span.ParentId]; exists {
parentNode.children = append(parentNode.children, node)
node.parent = parentNode
} else { // !exists
panic(fmt.Errorf("trace has orphaned spans (missing parent)"))
}
case root == nil:
root = node
default: // root != nil
panic(fmt.Errorf("trace has multiple root spans (parentId == nil)!"))
}
}
// compute relative names and times for each node.
root.visitByBreadth(func(node *tree) bool {
name := node.span.Name
if node.parent != nil {
name = node.parent.name + nameSep + name
}
node.name = name
if d, ok := node.outputMap.SD(); ok {
node.absTime = d
} else if d, ok = node.outputMap.CD(); ok {
node.absTime = d
} else {
c.config.Logger.Printf("Combine(): no stat to record for step: %s", name)
node.absTime = 0
}
node.relTime = node.absTime
if node.parent != nil {
node.parent.relTime -= node.absTime
}
return true
})
// TODO: adds other non-stats fields (leaves overriding parents, right overrides left.)?
results := []OutputMap{}
root.visitByBreadth(func(node *tree) bool {
result := OutputMap{}
result["name"] = node.name
result["absTime"] = node.absTime
result["relTime"] = node.relTime
results = append(results, result)
return true
})
return results
}