-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds grafana pyroscope to worker and backend (#3144)
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pyroscope_logger | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/grafana/pyroscope-go" | ||
) | ||
|
||
type PyroscopeLogger struct { | ||
logger *slog.Logger | ||
} | ||
|
||
var _ pyroscope.Logger = (*PyroscopeLogger)(nil) | ||
|
||
func (p *PyroscopeLogger) Debugf(format string, args ...any) { | ||
p.logger.Debug(fmt.Sprintf(format, args...)) | ||
} | ||
|
||
func (p *PyroscopeLogger) Infof(format string, args ...any) { | ||
p.logger.Info(fmt.Sprintf(format, args...)) | ||
} | ||
|
||
func (p *PyroscopeLogger) Errorf(format string, args ...any) { | ||
p.logger.Error(fmt.Sprintf(format, args...)) | ||
} | ||
|
||
func New(logger *slog.Logger) *PyroscopeLogger { | ||
return &PyroscopeLogger{logger: logger} | ||
} | ||
|
||
type noopLogger struct{} | ||
|
||
var _ pyroscope.Logger = (*noopLogger)(nil) | ||
|
||
func (n *noopLogger) Debugf(_ string, _ ...any) {} | ||
func (n *noopLogger) Infof(_ string, _ ...any) {} | ||
func (n *noopLogger) Errorf(_ string, _ ...any) {} | ||
|
||
func NewNoop() *noopLogger { | ||
return &noopLogger{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pyroscope_env | ||
|
||
import ( | ||
"errors" | ||
"log/slog" | ||
"runtime" | ||
|
||
"github.com/grafana/pyroscope-go" | ||
pyroscope_logger "github.com/nucleuscloud/neosync/internal/pyroscope/logger" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func NewFromEnv(applicationName string, logger *slog.Logger) (*pyroscope.Config, bool, error) { | ||
isPyroscopeEnabled := viper.GetBool("PYROSCOPE_ENABLED") | ||
if !isPyroscopeEnabled { | ||
return nil, false, nil | ||
} | ||
|
||
logger.Debug("pyroscope is enabled") | ||
serverAddress := viper.GetString("PYROSCOPE_SERVER_ADDRESS") | ||
if serverAddress == "" { | ||
return nil, false, errors.New("PYROSCOPE_SERVER_ADDRESS is required") | ||
} | ||
basicAuthUser := viper.GetString("PYROSCOPE_BASIC_AUTH_USER") | ||
basicAuthPassword := viper.GetString("PYROSCOPE_BASIC_AUTH_PASSWORD") | ||
|
||
pyroscopeTags := map[string]string{} | ||
|
||
isLoggerEnabled := viper.GetBool("PYROSCOPE_LOGGER_ENABLED") | ||
var pyroscopeLogger pyroscope.Logger | ||
if isLoggerEnabled { | ||
pyroscopeLogger = pyroscope_logger.New(logger) | ||
} else { | ||
pyroscopeLogger = pyroscope_logger.NewNoop() | ||
} | ||
|
||
runtime.SetMutexProfileFraction(5) | ||
runtime.SetBlockProfileRate(5) | ||
|
||
pyroscopeConfig := &pyroscope.Config{ | ||
ApplicationName: applicationName, | ||
ServerAddress: serverAddress, | ||
Logger: pyroscopeLogger, | ||
BasicAuthUser: basicAuthUser, | ||
BasicAuthPassword: basicAuthPassword, | ||
Tags: pyroscopeTags, | ||
ProfileTypes: []pyroscope.ProfileType{ | ||
pyroscope.ProfileCPU, | ||
pyroscope.ProfileAllocObjects, | ||
pyroscope.ProfileAllocSpace, | ||
pyroscope.ProfileInuseObjects, | ||
pyroscope.ProfileInuseSpace, | ||
|
||
pyroscope.ProfileGoroutines, | ||
pyroscope.ProfileMutexCount, | ||
pyroscope.ProfileMutexDuration, | ||
pyroscope.ProfileBlockCount, | ||
pyroscope.ProfileBlockDuration, | ||
}, | ||
DisableGCRuns: true, | ||
} | ||
|
||
return pyroscopeConfig, true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters