-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
trace.go
56 lines (49 loc) · 1.1 KB
/
trace.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
package trace
import (
"sync"
"time"
"k8s.io/utils/trace"
)
type Tracer struct {
traceMap map[string]*trace.Trace
mux sync.Mutex
Threshold time.Duration
}
func (t *Tracer) StartTrace(key string, name string, field ...trace.Field) {
t.mux.Lock()
defer t.mux.Unlock()
if t.traceMap == nil {
t.traceMap = make(map[string]*trace.Trace)
}
t.traceMap[key] = trace.New(name, field...)
return
}
func (t *Tracer) StopTrace(key string) {
if key == "" {
return
}
t.mux.Lock()
defer t.mux.Unlock()
if _, ok := t.traceMap[key]; !ok {
return
}
t.traceMap[key].LogIfLong(t.Threshold)
delete(t.traceMap, key)
return
}
// A trace Step adds a new step with a specific message.
// Call StepTrace after an execution step to record how long it took.
func (t *Tracer) StepTrace(key string, name string, field ...trace.Field) {
// Trace shouldn't be making noise unless the Trace is slow.
// Fail silently on errors like empty or incorrect keys.
if key == "" {
return
}
t.mux.Lock()
defer t.mux.Unlock()
if _, ok := t.traceMap[key]; !ok {
return
}
t.traceMap[key].Step(name, field...)
return
}