-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstepcounttracer.go
110 lines (91 loc) · 2.34 KB
/
stepcounttracer.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
package tracing
import (
"sync"
)
// StepCountTracer can collect the total time of a certain step is triggerred.
type StepCountTracer struct {
filter TaskFilter
lock sync.Mutex
inflightTasks map[string]Task
stepNames []string
stepCount map[string]uint64
taskWithStepCount map[string]uint64
}
// NewStepCountTracer creates a new StepCountTracer
func NewStepCountTracer(filter TaskFilter) *StepCountTracer {
t := &StepCountTracer{
filter: filter,
inflightTasks: make(map[string]Task),
stepCount: make(map[string]uint64),
taskWithStepCount: make(map[string]uint64),
}
return t
}
// GetStepNames returns all the step names collected.
func (t *StepCountTracer) GetStepNames() []string {
return t.stepNames
}
// GetStepCount returns the number of steps that is recorded with a certain step
// name.
func (t *StepCountTracer) GetStepCount(stepName string) uint64 {
return t.stepCount[stepName]
}
// GetTaskCount returns the number of tasks that is recorded to have a certain
// step with a given name.
func (t *StepCountTracer) GetTaskCount(stepName string) uint64 {
return t.taskWithStepCount[stepName]
}
// StartTask records the task start time
func (t *StepCountTracer) StartTask(task Task) {
if !t.filter(task) {
return
}
t.lock.Lock()
t.inflightTasks[task.ID] = task
t.lock.Unlock()
}
// StepTask does nothing
func (t *StepCountTracer) StepTask(task Task) {
t.lock.Lock()
defer t.lock.Unlock()
t.countStep(task)
t.countTask(task)
}
func (t *StepCountTracer) countStep(task Task) {
step := task.Steps[0]
_, ok := t.stepCount[step.What]
if !ok {
t.stepNames = append(t.stepNames, step.What)
}
t.stepCount[step.What]++
}
func (t *StepCountTracer) countTask(task Task) {
step := task.Steps[0]
originalTask, ok := t.inflightTasks[task.ID]
if !ok {
return
}
if !taskContainsStep(originalTask, step) {
t.taskWithStepCount[step.What]++
}
originalTask.Steps = append(originalTask.Steps, step)
}
func taskContainsStep(task Task, step TaskStep) bool {
for _, s := range task.Steps {
if s.What == step.What {
return true
}
}
return false
}
// EndTask records the end of the task
func (t *StepCountTracer) EndTask(task Task) {
t.lock.Lock()
_, ok := t.inflightTasks[task.ID]
if !ok {
t.lock.Unlock()
return
}
delete(t.inflightTasks, task.ID)
t.lock.Unlock()
}