diff --git a/docs/sources/troubleshooting/diagnostics.md b/docs/sources/troubleshooting/diagnostics.md index e3cf5dd9fd7..1eb2e7c5e4f 100644 --- a/docs/sources/troubleshooting/diagnostics.md +++ b/docs/sources/troubleshooting/diagnostics.md @@ -10,11 +10,11 @@ when investigating certain performance problems. It's _not_ recommended to have ## Turn on profiling -The `grafana-server` can be started with the arguments `-profile` to enable profiling and `-profile-port` to override -the default HTTP port (`6060`) where the `pprof` debugging endpoints are available, for example: +You can start the `grafana-server` with the following arguments: `-profile` to enable profiling, `-profile-addr` to override the default HTTP address (`localhost`), and + `-profile-port` to override the default HTTP port (`6060`) where the `pprof` debugging endpoints are available. For example: ```bash -./grafana-server -profile -profile-port=8080 +./grafana-server -profile -profile-addr=0.0.0.0 -profile-port=8080 ``` Note that `pprof` debugging endpoints are served on a different port than the Grafana HTTP server. @@ -23,6 +23,7 @@ You can configure or override profiling settings using environment variables: ```bash export GF_DIAGNOSTICS_PROFILING_ENABLED=true +export GF_DIAGNOSTICS_PROFILING_ADDR=0.0.0.0 export GF_DIAGNOSTICS_PROFILING_PORT=8080 ``` @@ -52,4 +53,4 @@ go tool trace 2019/11/24 22:20:42 Opening browser. Trace viewer is listening on http://127.0.0.1:39735 ``` -See [Go command trace](https://golang.org/cmd/trace/) for more information about how to analyze trace files. +For more information about how to analyze trace files, refer to [Go command trace](https://golang.org/cmd/trace/). \ No newline at end of file diff --git a/pkg/cmd/grafana-server/diagnostics.go b/pkg/cmd/grafana-server/diagnostics.go index 8e8b640c73d..c01b032d343 100644 --- a/pkg/cmd/grafana-server/diagnostics.go +++ b/pkg/cmd/grafana-server/diagnostics.go @@ -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) diff --git a/pkg/cmd/grafana-server/diagnostics_test.go b/pkg/cmd/grafana-server/diagnostics_test.go index f7e7b9874aa..2f09694be6d 100644 --- a/pkg/cmd/grafana-server/diagnostics_test.go +++ b/pkg/cmd/grafana-server/diagnostics_test.go @@ -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) diff --git a/pkg/cmd/grafana-server/main.go b/pkg/cmd/grafana-server/main.go index bdf0de65298..3ba31b35366 100644 --- a/pkg/cmd/grafana-server/main.go +++ b/pkg/cmd/grafana-server/main.go @@ -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) }