/
tx-tracing.go
80 lines (61 loc) · 1.19 KB
/
tx-tracing.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
package tracing
import (
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/opentracing/opentracing-go"
)
var (
enabled bool
txSpans = make(map[common.Hash]opentracing.Span)
txSpansMu sync.RWMutex
noopSpan = opentracing.NoopTracer{}.StartSpan("")
)
func SetEnabled(val bool) {
enabled = val
}
func Enabled() bool {
return enabled
}
func StartTx(tx common.Hash, operation string) {
if !enabled {
return
}
txSpansMu.Lock()
defer txSpansMu.Unlock()
if _, ok := txSpans[tx]; ok {
return
}
span := opentracing.StartSpan("lifecycle")
span.SetTag("txhash", tx.String())
span.SetTag("enter", operation)
txSpans[tx] = span
}
func FinishTx(tx common.Hash, operation string) {
if !enabled {
return
}
txSpansMu.Lock()
defer txSpansMu.Unlock()
span, ok := txSpans[tx]
if !ok {
return
}
span.SetTag("exit", operation)
span.Finish()
delete(txSpans, tx)
}
func CheckTx(tx common.Hash, operation string) opentracing.Span {
if !enabled {
return noopSpan
}
txSpansMu.RLock()
defer txSpansMu.RUnlock()
span, ok := txSpans[tx]
if !ok {
return noopSpan
}
return opentracing.GlobalTracer().StartSpan(
operation,
opentracing.ChildOf(span.Context()),
)
}