-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a port of GoogleCloudPlatform/cloud-sql-proxy#1564.
- Loading branch information
Showing
4 changed files
with
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,6 +277,21 @@ when you want to proxy this traffic. Otherwise, it is optional. See | |
[`http.ProxyFromEnvironment`](https://pkg.go.dev/net/[email protected]#ProxyFromEnvironment) | ||
for possible values. | ||
|
||
## Localhost Admin Server | ||
|
||
The Proxy includes support for an admin server on localhost. By default, the | ||
admin server is not enabled. To enable the server, pass the `--debug` flag. | ||
This will start the server on localhost at port 9091. To change the port, use | ||
the `--admin-port` flag. | ||
|
||
The admin server includes Go's pprof tool and is available at `/debug/pprof/`. | ||
|
||
See the [documentation on pprof][pprof] for details on how to use the | ||
profiler. | ||
|
||
[pprof]: https://pkg.go.dev/net/http/pprof. | ||
|
||
|
||
## Support policy | ||
|
||
### Major version lifecycle | ||
|
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 |
---|---|---|
|
@@ -78,6 +78,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 | ||
} | ||
|
@@ -296,6 +299,22 @@ func TestNewCommandArguments(t *testing.T) { | |
ImpersonationChain: "[email protected]", | ||
}), | ||
}, | ||
{ | ||
desc: "using the debug flag", | ||
args: []string{"--debug", | ||
"projects/proj/locations/region/clusters/clust/instances/inst"}, | ||
want: withDefaults(&proxy.Config{ | ||
Debug: true, | ||
}), | ||
}, | ||
{ | ||
desc: "using the admin port flag", | ||
args: []string{"--admin-port", "7777", | ||
"projects/proj/locations/region/clusters/clust/instances/inst"}, | ||
want: withDefaults(&proxy.Config{ | ||
AdminPort: "7777", | ||
}), | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
|
@@ -495,6 +514,22 @@ func TestNewCommandWithEnvironmentConfig(t *testing.T) { | |
HTTPPort: "5555", | ||
}), | ||
}, | ||
{ | ||
desc: "using the debug envvar", | ||
envName: "ALLOYDB_PROXY_DEBUG", | ||
envValue: "true", | ||
want: withDefaults(&proxy.Config{ | ||
Debug: true, | ||
}), | ||
}, | ||
{ | ||
desc: "using the admin port envvar", | ||
envName: "ALLOYDB_PROXY_ADMIN_PORT", | ||
envValue: "7777", | ||
want: withDefaults(&proxy.Config{ | ||
AdminPort: "7777", | ||
}), | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
|
@@ -843,6 +878,26 @@ func TestCommandWithCustomDialer(t *testing.T) { | |
}, 10) | ||
} | ||
|
||
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 | ||
|
@@ -857,25 +912,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) | ||
|
@@ -884,3 +920,22 @@ 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", | ||
"projects/proj/locations/region/clusters/clust/instances/inst"}) | ||
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