mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
profiling: Support binding pprof server to custom network interfaces (#36580)
Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
const (
|
||||
profilingEnabledEnvName = "GF_DIAGNOSTICS_PROFILING_ENABLED"
|
||||
profilingAddrEnvName = "GF_DIAGNOSTICS_PROFILING_ADDR"
|
||||
profilingPortEnvName = "GF_DIAGNOSTICS_PROFILING_PORT"
|
||||
tracingEnabledEnvName = "GF_DIAGNOSTICS_TRACING_ENABLED"
|
||||
tracingFileEnvName = "GF_DIAGNOSTICS_TRACING_FILE"
|
||||
@@ -15,12 +16,14 @@ const (
|
||||
|
||||
type profilingDiagnostics struct {
|
||||
enabled bool
|
||||
addr string
|
||||
port uint64
|
||||
}
|
||||
|
||||
func newProfilingDiagnostics(enabled bool, port uint64) *profilingDiagnostics {
|
||||
func newProfilingDiagnostics(enabled bool, addr string, port uint64) *profilingDiagnostics {
|
||||
return &profilingDiagnostics{
|
||||
enabled: enabled,
|
||||
addr: addr,
|
||||
port: port,
|
||||
}
|
||||
}
|
||||
@@ -35,6 +38,11 @@ func (pd *profilingDiagnostics) overrideWithEnv() error {
|
||||
pd.enabled = enabled
|
||||
}
|
||||
|
||||
addrEnv := os.Getenv(profilingAddrEnvName)
|
||||
if addrEnv != "" {
|
||||
pd.addr = addrEnv
|
||||
}
|
||||
|
||||
portEnv := os.Getenv(profilingPortEnvName)
|
||||
if portEnv != "" {
|
||||
port, parseErr := strconv.ParseUint(portEnv, 0, 64)
|
||||
|
||||
@@ -12,14 +12,15 @@ func TestProfilingDiagnostics(t *testing.T) {
|
||||
tcs := []struct {
|
||||
defaults *profilingDiagnostics
|
||||
enabledEnv string
|
||||
addrEnv string
|
||||
portEnv string
|
||||
expected *profilingDiagnostics
|
||||
}{
|
||||
{defaults: newProfilingDiagnostics(false, 6060), enabledEnv: "", portEnv: "", expected: newProfilingDiagnostics(false, 6060)},
|
||||
{defaults: newProfilingDiagnostics(true, 8080), enabledEnv: "", portEnv: "", expected: newProfilingDiagnostics(true, 8080)},
|
||||
{defaults: newProfilingDiagnostics(false, 6060), enabledEnv: "false", portEnv: "8080", expected: newProfilingDiagnostics(false, 8080)},
|
||||
{defaults: newProfilingDiagnostics(false, 6060), enabledEnv: "true", portEnv: "8080", expected: newProfilingDiagnostics(true, 8080)},
|
||||
{defaults: newProfilingDiagnostics(false, 6060), enabledEnv: "true", portEnv: "", expected: newProfilingDiagnostics(true, 6060)},
|
||||
{defaults: newProfilingDiagnostics(false, "localhost", 6060), enabledEnv: "", addrEnv: "", portEnv: "", expected: newProfilingDiagnostics(false, "localhost", 6060)},
|
||||
{defaults: newProfilingDiagnostics(true, "0.0.0.0", 8080), enabledEnv: "", addrEnv: "", portEnv: "", expected: newProfilingDiagnostics(true, "0.0.0.0", 8080)},
|
||||
{defaults: newProfilingDiagnostics(false, "", 6060), enabledEnv: "false", addrEnv: "", portEnv: "8080", expected: newProfilingDiagnostics(false, "", 8080)},
|
||||
{defaults: newProfilingDiagnostics(false, "localhost", 6060), enabledEnv: "true", addrEnv: "0.0.0.0", portEnv: "8080", expected: newProfilingDiagnostics(true, "0.0.0.0", 8080)},
|
||||
{defaults: newProfilingDiagnostics(false, "127.0.0.1", 6060), enabledEnv: "true", addrEnv: "", portEnv: "", expected: newProfilingDiagnostics(true, "127.0.0.1", 6060)},
|
||||
}
|
||||
|
||||
for i, tc := range tcs {
|
||||
@@ -29,6 +30,10 @@ func TestProfilingDiagnostics(t *testing.T) {
|
||||
err := os.Setenv(profilingEnabledEnvName, tc.enabledEnv)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
if tc.addrEnv != "" {
|
||||
err := os.Setenv(profilingAddrEnvName, tc.addrEnv)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
if tc.portEnv != "" {
|
||||
err := os.Setenv(profilingPortEnvName, tc.portEnv)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -63,6 +63,7 @@ func main() {
|
||||
v = flag.Bool("v", false, "prints current version and exits")
|
||||
vv = flag.Bool("vv", false, "prints current version, all dependencies and exits")
|
||||
profile = flag.Bool("profile", false, "Turn on pprof profiling")
|
||||
profileAddr = flag.String("profile-addr", "localhost", "Define custom address for profiling")
|
||||
profilePort = flag.Uint64("profile-port", 6060, "Define custom port for profiling")
|
||||
tracing = flag.Bool("tracing", false, "Turn on tracing")
|
||||
tracingFile = flag.String("tracing-file", "trace.out", "Define tracing output file")
|
||||
@@ -83,7 +84,7 @@ func main() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
profileDiagnostics := newProfilingDiagnostics(*profile, *profilePort)
|
||||
profileDiagnostics := newProfilingDiagnostics(*profile, *profileAddr, *profilePort)
|
||||
if err := profileDiagnostics.overrideWithEnv(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
@@ -96,10 +97,10 @@ func main() {
|
||||
}
|
||||
|
||||
if profileDiagnostics.enabled {
|
||||
fmt.Println("diagnostics: pprof profiling enabled", "port", profileDiagnostics.port)
|
||||
fmt.Println("diagnostics: pprof profiling enabled", "addr", profileDiagnostics.addr, "port", profileDiagnostics.port)
|
||||
runtime.SetBlockProfileRate(1)
|
||||
go func() {
|
||||
err := http.ListenAndServe(fmt.Sprintf("localhost:%d", profileDiagnostics.port), nil)
|
||||
err := http.ListenAndServe(fmt.Sprintf("%s:%d", profileDiagnostics.addr, profileDiagnostics.port), nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user