new-api/common/pyro.go

51 lines
1.1 KiB
Go
Raw Normal View History

2025-12-13 13:49:38 +08:00
package common
import (
"runtime"
"github.com/grafana/pyroscope-go"
)
func StartPyroScope() error {
2025-12-19 22:27:35 +08:00
pyroscopeUrl := GetEnvOrDefaultString("PYROSCOPE_URL", "")
2025-12-13 13:49:38 +08:00
if pyroscopeUrl == "" {
return nil
}
2025-12-19 22:27:35 +08:00
pyroscopeAppName := GetEnvOrDefaultString("PYROSCOPE_APP_NAME", "new-api")
2025-12-13 13:49:38 +08:00
// These 2 lines are only required if you're using mutex or block profiling
// Read the explanation below for how to set these rates:
runtime.SetMutexProfileFraction(5)
runtime.SetBlockProfileRate(5)
_, err := pyroscope.Start(pyroscope.Config{
2025-12-19 22:27:35 +08:00
ApplicationName: pyroscopeAppName,
2025-12-13 13:49:38 +08:00
ServerAddress: pyroscopeUrl,
Logger: nil,
Tags: map[string]string{"hostname": GetEnvOrDefaultString("HOSTNAME", "new-api")},
ProfileTypes: []pyroscope.ProfileType{
pyroscope.ProfileCPU,
pyroscope.ProfileAllocObjects,
pyroscope.ProfileAllocSpace,
pyroscope.ProfileInuseObjects,
pyroscope.ProfileInuseSpace,
pyroscope.ProfileGoroutines,
pyroscope.ProfileMutexCount,
pyroscope.ProfileMutexDuration,
pyroscope.ProfileBlockCount,
pyroscope.ProfileBlockDuration,
},
})
if err != nil {
return err
}
return nil
}