@@ -6,27 +6,32 @@ import (
66 "time"
77
88 "github.com/grafana/pyroscope-go"
9-
109 "github.com/zeromicro/go-zero/core/logx"
1110 "github.com/zeromicro/go-zero/core/proc"
1211 "github.com/zeromicro/go-zero/core/stat"
1312 "github.com/zeromicro/go-zero/core/threading"
1413)
1514
15+ const (
16+ defaultCheckInterval = time .Second * 10
17+ defaultProfilingDuration = time .Minute * 2
18+ defaultUploadRate = time .Second * 15
19+ )
20+
1621type (
1722 Config struct {
1823 // Name is the name of the application.
1924 Name string `json:",optional,inherit"`
20- // ServerAddress is the address of the profiling server.
21- ServerAddress string
25+ // ServerAddr is the address of the profiling server.
26+ ServerAddr string
2227 // AuthUser is the username for basic authentication.
2328 AuthUser string `json:",optional"`
2429 // AuthPassword is the password for basic authentication.
2530 AuthPassword string `json:",optional"`
26- // UploadDuration is the duration for which profiling data is uploaded.
27- UploadDuration time.Duration `json:",default=15s"`
28- // IntervalDuration is the interval for which profiling data is collected .
29- IntervalDuration time.Duration `json:",default=10s"`
31+ // UploadRate is the duration for which profiling data is uploaded.
32+ UploadRate time.Duration `json:",default=15s"`
33+ // CheckInterval is the interval to check if profiling should start .
34+ CheckInterval time.Duration `json:",default=10s"`
3035 // ProfilingDuration is the duration for which profiling data is collected.
3136 ProfilingDuration time.Duration `json:",default=2m"`
3237 // CpuThreshold the collection is allowed only when the current service cpu < CpuThreshold
5661 Stop () error
5762 }
5863
59- pyProfiler struct {
64+ pyroscopeProfiler struct {
6065 c Config
6166 profiler * pyroscope.Profiler
6267 }
@@ -66,63 +71,58 @@ var (
6671 once sync.Once
6772
6873 newProfiler = func (c Config ) profiler {
69- return newPyProfiler (c )
74+ return newPyroscopeProfiler (c )
7075 }
7176)
7277
7378// Start initializes the pyroscope profiler with the given configuration.
7479func Start (c Config ) {
7580 // check if the profiling is enabled
76- if c . ServerAddress == "" {
81+ if len ( c . ServerAddr ) == 0 {
7782 return
7883 }
7984
8085 // set default values for the configuration
8186 if c .ProfilingDuration <= 0 {
82- c .ProfilingDuration = time . Minute * 2
87+ c .ProfilingDuration = defaultProfilingDuration
8388 }
8489
8590 // set default values for the configuration
86- if c .IntervalDuration <= 0 {
87- c .IntervalDuration = time . Second * 10
91+ if c .CheckInterval <= 0 {
92+ c .CheckInterval = defaultCheckInterval
8893 }
8994
90- if c .UploadDuration <= 0 {
91- c .UploadDuration = time . Second * 15
95+ if c .UploadRate <= 0 {
96+ c .UploadRate = defaultUploadRate
9297 }
9398
9499 once .Do (func () {
95100 logx .Info ("continuous profiling started" )
96101
97- var done = make (chan struct {})
98- proc .AddShutdownListener (func () {
99- done <- struct {}{}
100- close (done )
101- })
102-
103102 threading .GoSafe (func () {
104- startPyroScope (c , done )
103+ startPyroscope (c , proc . Done () )
105104 })
106105 })
107106}
108107
109- // startPyroScope starts the pyroscope profiler with the given configuration.
110- func startPyroScope (c Config , done <- chan struct {}) {
108+ // startPyroscope starts the pyroscope profiler with the given configuration.
109+ func startPyroscope (c Config , done <- chan struct {}) {
111110 var (
112- intervalTicker = time .NewTicker (c .IntervalDuration )
113- profilingTicker = time .NewTicker (c .ProfilingDuration )
114-
115- pr profiler
116- err error
117-
111+ pr profiler
112+ err error
118113 latestProfilingTime time.Time
114+ intervalTicker = time .NewTicker (c .CheckInterval )
115+ profilingTicker = time .NewTicker (c .ProfilingDuration )
119116 )
120117
118+ defer profilingTicker .Stop ()
119+ defer intervalTicker .Stop ()
120+
121121 for {
122122 select {
123123 case <- intervalTicker .C :
124124 // Check if the machine is overloaded and if the profiler is not running
125- if pr == nil && checkMachinePerformance (c ) {
125+ if pr == nil && isCpuOverloaded (c ) {
126126 pr = newProfiler (c )
127127 if err := pr .Start (); err != nil {
128128 logx .Errorf ("failed to start profiler: %v" , err )
@@ -131,7 +131,7 @@ func startPyroScope(c Config, done <-chan struct{}) {
131131
132132 // record the latest profiling time
133133 latestProfilingTime = time .Now ()
134- logx .Infof ("pyroScope profiler started." )
134+ logx .Infof ("pyroscope profiler started." )
135135 }
136136 case <- profilingTicker .C :
137137 // check if the profiling duration has passed
@@ -144,30 +144,26 @@ func startPyroScope(c Config, done <-chan struct{}) {
144144 if err = pr .Stop (); err != nil {
145145 logx .Errorf ("failed to stop profiler: %v" , err )
146146 }
147- logx .Infof ("pyroScope profiler stopped." )
147+ logx .Infof ("pyroscope profiler stopped." )
148148 pr = nil
149149 }
150150 case <- done :
151151 logx .Infof ("continuous profiling stopped." )
152- intervalTicker .Stop ()
153- profilingTicker .Stop ()
154152 return
155153 }
156154 }
157155}
158156
159- // genPyroScopeConf generates the pyroscope configuration based on the given config.
160- func genPyroScopeConf (c Config ) pyroscope.Config {
157+ // genPyroscopeConf generates the pyroscope configuration based on the given config.
158+ func genPyroscopeConf (c Config ) pyroscope.Config {
161159 pConf := pyroscope.Config {
162- UploadRate : c .UploadDuration ,
160+ UploadRate : c .UploadRate ,
163161 ApplicationName : c .Name ,
164162 BasicAuthUser : c .AuthUser , // http basic auth user
165163 BasicAuthPassword : c .AuthPassword , // http basic auth password
166- ServerAddress : c .ServerAddress ,
164+ ServerAddress : c .ServerAddr ,
167165 Logger : nil ,
168-
169- HTTPHeaders : map [string ]string {},
170-
166+ HTTPHeaders : map [string ]string {},
171167 // you can provide static tags via a map:
172168 Tags : map [string ]string {
173169 "name" : c .Name ,
@@ -200,46 +196,46 @@ func genPyroScopeConf(c Config) pyroscope.Config {
200196 return pConf
201197}
202198
203- // checkMachinePerformance checks the machine performance based on the given configuration.
204- func checkMachinePerformance (c Config ) bool {
199+ // isCpuOverloaded checks the machine performance based on the given configuration.
200+ func isCpuOverloaded (c Config ) bool {
205201 currentValue := stat .CpuUsage ()
206202 if currentValue >= c .CpuThreshold {
207- logx .Infof ("continuous profiling cpu overload, cpu:%d" , currentValue )
203+ logx .Infof ("continuous profiling cpu overload, cpu: %d" , currentValue )
208204 return true
209205 }
210206
211207 return false
212208}
213209
214- func newPyProfiler (c Config ) profiler {
215- return & pyProfiler {
210+ func newPyroscopeProfiler (c Config ) profiler {
211+ return & pyroscopeProfiler {
216212 c : c ,
217213 }
218214}
219215
220- func (p * pyProfiler ) Start () error {
221- pConf := genPyroScopeConf (p .c )
216+ func (p * pyroscopeProfiler ) Start () error {
217+ pConf := genPyroscopeConf (p .c )
222218 // set mutex and block profile rate
223219 setFraction (p .c )
224- profiler , err := pyroscope .Start (pConf )
220+ prof , err := pyroscope .Start (pConf )
225221 if err != nil {
226222 resetFraction (p .c )
227223 return err
228224 }
229225
230- p .profiler = profiler
226+ p .profiler = prof
231227 return nil
232228}
233229
234- func (p * pyProfiler ) Stop () error {
230+ func (p * pyroscopeProfiler ) Stop () error {
235231 if p .profiler == nil {
236232 return nil
237233 }
238234
239- err := p .profiler .Stop ()
240- if err != nil {
235+ if err := p .profiler .Stop (); err != nil {
241236 return err
242237 }
238+
243239 resetFraction (p .c )
244240 p .profiler = nil
245241
0 commit comments