-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathprofile.go
58 lines (46 loc) · 1.19 KB
/
profile.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
package internal
import (
"math/rand"
"time"
conf "github.com/wal-g/wal-g/internal/config"
"github.com/pkg/profile"
"github.com/spf13/viper"
)
type ProfileStopper interface {
Stop()
}
func Profile() (ProfileStopper, error) {
if !viper.IsSet(conf.ProfileSamplingRatio) {
return nil, nil
}
samplingRatio := viper.GetFloat64(conf.ProfileSamplingRatio)
// sample profiling invoked commands
rand.Seed(time.Now().UnixNano())
if rand.Float64() >= samplingRatio {
return nil, nil
}
var opts []func(*profile.Profile)
profileMode := viper.GetString(conf.ProfileMode)
switch profileMode {
case "cpu":
opts = append(opts, profile.CPUProfile)
case "mem":
opts = append(opts, profile.MemProfile)
case "mutex":
opts = append(opts, profile.MutexProfile)
case "block":
opts = append(opts, profile.BlockProfile)
case "threadcreation":
opts = append(opts, profile.ThreadcreationProfile)
case "trace":
opts = append(opts, profile.TraceProfile)
case "goroutine":
opts = append(opts, profile.GoroutineProfile)
}
profilePath := viper.GetString(conf.ProfilePath)
if profilePath != "" {
opts = append(opts, profile.ProfilePath(profilePath))
}
p := profile.Start(opts...)
return p, nil
}