This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
signal.go
84 lines (72 loc) · 2.24 KB
/
signal.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
package blackfire
import (
"os"
"os/signal"
"time"
)
// EnableOnSignal sets up a trigger to enable profiling when the specified signal is received.
// The profiler will profile for the specified duration.
func EnableOnSignal(sig os.Signal, duration time.Duration) (err error) {
if err = globalProbe.configuration.load(); err != nil {
return
}
if !globalProbe.configuration.canProfile() {
return
}
logger := globalProbe.configuration.Logger
logger.Info().Msgf("Blackfire (signal): Signal [%s] triggers profiling for %.0f seconds", sig, float64(duration)/1000000000)
callFuncOnSignal(sig, func() {
logger.Info().Msgf("Blackfire (%s): Profiling for %.0f seconds", sig, float64(duration)/1000000000)
if err := globalProbe.EnableNowFor(duration); err != nil {
logger.Error().Msgf("Blackfire (EnableOnSignal): %v", err)
}
})
return
}
// DisableOnSignal sets up a trigger to disable profiling when the specified signal is received.
func DisableOnSignal(sig os.Signal) (err error) {
if err = globalProbe.configuration.load(); err != nil {
return
}
if !globalProbe.configuration.canProfile() {
return
}
logger := globalProbe.configuration.Logger
logger.Info().Msgf("Blackfire (signal): Signal [%s] stops profiling", sig)
callFuncOnSignal(sig, func() {
logger.Info().Msgf("Blackfire (%s): Disable profiling", sig)
if err := globalProbe.Disable(); err != nil {
logger.Error().Msgf("Blackfire (DisableOnSignal): %v", err)
}
})
return
}
// EndOnSignal sets up a trigger to end the current profile and upload to Blackfire when the
// specified signal is received.
func EndOnSignal(sig os.Signal) (err error) {
if err = globalProbe.configuration.load(); err != nil {
return
}
if !globalProbe.configuration.canProfile() {
return
}
logger := globalProbe.configuration.Logger
logger.Info().Msgf("Blackfire (signal): Signal [%s] ends the current profile", sig)
callFuncOnSignal(sig, func() {
logger.Info().Msgf("Blackfire (%s): End profile", sig)
if err := globalProbe.EndNoWait(); err != nil {
logger.Error().Msgf("Blackfire (EndOnSignal): %v", err)
}
})
return
}
func callFuncOnSignal(sig os.Signal, function func()) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, sig)
go func() {
for {
<-sigs
function()
}
}()
}