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:
Victor Cinaglia 2021-08-18 07:28:39 -04:00 committed by GitHub
parent 552ecfeda3
commit 0819d15942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 13 deletions

View File

@ -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:
The `grafana-server` can be started with the 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 <trace file>
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/).

View File

@ -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)

View File

@ -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)

View File

@ -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)
}