pyro.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package common
  2. import (
  3. "runtime"
  4. "github.com/grafana/pyroscope-go"
  5. )
  6. func StartPyroScope() error {
  7. pyroscopeUrl := GetEnvOrDefaultString("PYROSCOPE_URL", "")
  8. if pyroscopeUrl == "" {
  9. return nil
  10. }
  11. pyroscopeAppName := GetEnvOrDefaultString("PYROSCOPE_APP_NAME", "new-api")
  12. // These 2 lines are only required if you're using mutex or block profiling
  13. // Read the explanation below for how to set these rates:
  14. runtime.SetMutexProfileFraction(5)
  15. runtime.SetBlockProfileRate(5)
  16. _, err := pyroscope.Start(pyroscope.Config{
  17. ApplicationName: pyroscopeAppName,
  18. ServerAddress: pyroscopeUrl,
  19. Logger: nil,
  20. Tags: map[string]string{"hostname": GetEnvOrDefaultString("HOSTNAME", "new-api")},
  21. ProfileTypes: []pyroscope.ProfileType{
  22. pyroscope.ProfileCPU,
  23. pyroscope.ProfileAllocObjects,
  24. pyroscope.ProfileAllocSpace,
  25. pyroscope.ProfileInuseObjects,
  26. pyroscope.ProfileInuseSpace,
  27. pyroscope.ProfileGoroutines,
  28. pyroscope.ProfileMutexCount,
  29. pyroscope.ProfileMutexDuration,
  30. pyroscope.ProfileBlockCount,
  31. pyroscope.ProfileBlockDuration,
  32. },
  33. })
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }