-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add admin server with pprof (#1564)
- Loading branch information
Showing
4 changed files
with
124 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,9 @@ func withDefaults(c *proxy.Config) *proxy.Config { | |
if c.HTTPPort == "" { | ||
c.HTTPPort = "9090" | ||
} | ||
if c.AdminPort == "" { | ||
c.AdminPort = "9091" | ||
} | ||
if c.TelemetryTracingSampleRate == 0 { | ||
c.TelemetryTracingSampleRate = 10_000 | ||
} | ||
|
@@ -359,6 +362,20 @@ func TestNewCommandArguments(t *testing.T) { | |
ImpersonationChain: "[email protected]", | ||
}), | ||
}, | ||
{ | ||
desc: "using the debug flag", | ||
args: []string{"--debug", "proj:region:inst"}, | ||
want: withDefaults(&proxy.Config{ | ||
Debug: true, | ||
}), | ||
}, | ||
{ | ||
desc: "using the admin port flag", | ||
args: []string{"--admin-port", "7777", "proj:region:inst"}, | ||
want: withDefaults(&proxy.Config{ | ||
AdminPort: "7777", | ||
}), | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
|
@@ -698,6 +715,22 @@ func TestNewCommandWithEnvironmentConfig(t *testing.T) { | |
HTTPPort: "5555", | ||
}), | ||
}, | ||
{ | ||
desc: "using the debug envvar", | ||
envName: "CSQL_PROXY_DEBUG", | ||
envValue: "true", | ||
want: withDefaults(&proxy.Config{ | ||
Debug: true, | ||
}), | ||
}, | ||
{ | ||
desc: "using the admin port envvar", | ||
envName: "CSQL_PROXY_ADMIN_PORT", | ||
envValue: "7777", | ||
want: withDefaults(&proxy.Config{ | ||
AdminPort: "7777", | ||
}), | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
|
@@ -1010,6 +1043,26 @@ func TestCommandWithCustomDialer(t *testing.T) { | |
} | ||
} | ||
|
||
func tryDial(addr string) (*http.Response, error) { | ||
var ( | ||
resp *http.Response | ||
attempts int | ||
err error | ||
) | ||
for { | ||
if attempts > 10 { | ||
return resp, err | ||
} | ||
resp, err = http.Get(addr) | ||
if err != nil { | ||
attempts++ | ||
time.Sleep(time.Second) | ||
continue | ||
} | ||
return resp, err | ||
} | ||
} | ||
|
||
func TestPrometheusMetricsEndpoint(t *testing.T) { | ||
c := NewCommand(WithDialer(&spyDialer{})) | ||
// Keep the test output quiet | ||
|
@@ -1024,25 +1077,6 @@ func TestPrometheusMetricsEndpoint(t *testing.T) { | |
|
||
// try to dial metrics server for a max of ~10s to give the proxy time to | ||
// start up. | ||
tryDial := func(addr string) (*http.Response, error) { | ||
var ( | ||
resp *http.Response | ||
attempts int | ||
err error | ||
) | ||
for { | ||
if attempts > 10 { | ||
return resp, err | ||
} | ||
resp, err = http.Get(addr) | ||
if err != nil { | ||
attempts++ | ||
time.Sleep(time.Second) | ||
continue | ||
} | ||
return resp, err | ||
} | ||
} | ||
resp, err := tryDial("http://localhost:9090/metrics") // default port set by http-port flag | ||
if err != nil { | ||
t.Fatalf("failed to dial metrics endpoint: %v", err) | ||
|
@@ -1051,3 +1085,21 @@ func TestPrometheusMetricsEndpoint(t *testing.T) { | |
t.Fatalf("expected a 200 status, got = %v", resp.StatusCode) | ||
} | ||
} | ||
|
||
func TestPProfServer(t *testing.T) { | ||
c := NewCommand(WithDialer(&spyDialer{})) | ||
c.SilenceUsage = true | ||
c.SilenceErrors = true | ||
c.SetArgs([]string{"--debug", "--admin-port", "9191", "my-project:my-region:my-instance"}) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
go c.ExecuteContext(ctx) | ||
resp, err := tryDial("http://localhost:9191/debug/pprof/") | ||
if err != nil { | ||
t.Fatalf("failed to dial endpoint: %v", err) | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
t.Fatalf("expected a 200 status, got = %v", resp.StatusCode) | ||
} | ||
} |
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