/
performance.go
104 lines (90 loc) · 3.46 KB
/
performance.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
// Package performance provides helpers to get sentry performance monitoring working fast.
package performance
import (
"context"
"github.com/getsentry/sentry-go"
"sync"
)
type transactionContext struct {
transactions map[string]*sentry.Span
spans map[string]map[string]*sentry.Span
transactionsMutex sync.RWMutex
spansMutex sync.RWMutex
}
var currentTransactionContext *transactionContext = nil
func (tc *transactionContext) createTransaction(name string, operation string, ctx context.Context) *sentry.Span {
var transaction = sentry.StartSpan(ctx, operation, sentry.WithTransactionName(name))
tc.transactionsMutex.Lock()
tc.transactions[name] = transaction
tc.transactionsMutex.Unlock()
tc.spansMutex.Lock()
tc.spans[name] = make(map[string]*sentry.Span)
tc.spansMutex.Unlock()
return transaction
}
func (tc *transactionContext) getTransaction(name string) *sentry.Span {
tc.transactionsMutex.Lock()
if transaction, ok := tc.transactions[name]; ok {
tc.transactionsMutex.Unlock()
return transaction
}
tc.transactionsMutex.Unlock()
return nil
}
func (tc *transactionContext) createSpan(transactionName string, operation string) *sentry.Span {
var transaction = tc.getTransaction(transactionName)
if nil == transaction {
transaction = tc.createTransaction(transactionName, operation, context.Background())
}
var span = sentry.StartSpan(transaction.Context(), operation)
tc.spansMutex.Lock()
tc.spans[transactionName][operation] = span
tc.spansMutex.Unlock()
return span
}
func (tc *transactionContext) getSpan(transactionName string, operation string) *sentry.Span {
tc.spansMutex.Lock()
if transaction, ok := tc.spans[transactionName]; ok {
if span, ok := transaction[operation]; ok {
tc.spansMutex.Unlock()
return span
}
}
tc.spansMutex.Unlock()
return nil
}
func getCurrentTransactionContext() *transactionContext {
if nil == currentTransactionContext {
Refresh()
}
return currentTransactionContext
}
// Refresh clears current transaction context.
func Refresh() {
currentTransactionContext = &transactionContext{
transactions: make(map[string]*sentry.Span),
spans: make(map[string]map[string]*sentry.Span),
transactionsMutex: sync.RWMutex{},
spansMutex: sync.RWMutex{},
}
}
// CreateTransaction creates a transaction that can be attached additional spans.
func CreateTransaction(name string, operation string) *sentry.Span {
return getCurrentTransactionContext().createTransaction(name, operation, context.Background())
}
// CreateTransactionWithContext creates a transaction that can be attached additional spans and sets an existing context.
func CreateTransactionWithContext(name string, operation string, ctx context.Context) *sentry.Span {
return getCurrentTransactionContext().createTransaction(name, operation, ctx)
}
// GetTransaction returns a transaction by its name (or nil if none exists).
func GetTransaction(name string) *sentry.Span {
return getCurrentTransactionContext().getTransaction(name)
}
// CreateSpan creates a span and attaches it to a transaction specified by its name (returns nil if transaction doesn't exist).
func CreateSpan(transactionName string, operation string) *sentry.Span {
return getCurrentTransactionContext().createSpan(transactionName, operation)
}
// GetSpan returns a transaction by its name (or nil if none exists).
func GetSpan(transactionName string, operation string) *sentry.Span {
return getCurrentTransactionContext().getSpan(transactionName, operation)
}