Skip to content

Commit

Permalink
feat: add support for debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom committed Mar 11, 2024
1 parent ab64481 commit 26770eb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ Localhost Admin Server
/quitquitquit. The admin server exits gracefully when it receives a POST
request at /quitquitquit.
Debug logging
On occasion, it can help to enable debug logging which will report on
internal certificate refresh operations. To enable debug logging, use:
./cloud-sql-proxy <INSTANCE_CONNECTION_NAME> --debug-logs
Waiting for Startup
See the wait subcommand's help for details.
Expand Down Expand Up @@ -509,6 +517,8 @@ Instead prefer Application Default Credentials
the Proxy will then pick-up automatically.`)
localFlags.BoolVarP(&c.conf.StructuredLogs, "structured-logs", "l", false,
"Enable structured logs using the LogEntry format")
localFlags.BoolVar(&c.conf.DebugLogs, "debug-logs", false,
"Enable debug logging")
localFlags.Uint64Var(&c.conf.MaxConnections, "max-connections", 0,
`Limits the number of connections by refusing any additional connections.
When this flag is not set, there is no limit.`)
Expand Down
8 changes: 8 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,14 @@ func TestNewCommandArguments(t *testing.T) {
RunConnectionTest: true,
}),
},
{
desc: "using the debug logs flag",
args: []string{"--debug-logs",
"projects/proj/locations/region/clusters/clust/instances/inst"},
want: withDefaults(&proxy.Config{
DebugLogs: true,
}),
},
}

for _, tc := range tcs {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/GoogleCloudPlatform/alloydb-auth-proxy
go 1.22

require (
cloud.google.com/go/alloydbconn v1.7.0
cloud.google.com/go/alloydbconn v1.7.1-0.20240311014225-6ca502909c02
contrib.go.opencensus.io/exporter/prometheus v0.4.2
contrib.go.opencensus.io/exporter/stackdriver v0.13.14
github.com/coreos/go-systemd/v22 v22.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ cloud.google.com/go/alloydb v1.8.1 h1:H7bNDEoqc9IvtxCiA1oOJqoAAsVFB4BXPZ//6Qp/v7
cloud.google.com/go/alloydb v1.8.1/go.mod h1:GTzEf72qrkivvTH1GNkCe9lkJp8zXOZyoC6Zjy1SG/s=
cloud.google.com/go/alloydbconn v1.7.0 h1:gKK2Ky355yP6wxd7yDKvY5MSsGqk2R6X4YGH3Wo7Khk=
cloud.google.com/go/alloydbconn v1.7.0/go.mod h1:JcGl5vwN9ndUOqbmmNKrFs/Wlw8c2Bwmz+jHzShGAJc=
cloud.google.com/go/alloydbconn v1.7.1-0.20240311014225-6ca502909c02 h1:Mgn60Ou4Ufut751Noe5YgyKp+yS11OrN/LxAsLaDQCU=
cloud.google.com/go/alloydbconn v1.7.1-0.20240311014225-6ca502909c02/go.mod h1:JcGl5vwN9ndUOqbmmNKrFs/Wlw8c2Bwmz+jHzShGAJc=
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc=
Expand Down
10 changes: 6 additions & 4 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ import (
// logs.
type StdLogger struct {
infoLog *llog.Logger
debugLog *llog.Logger
errLog *llog.Logger
}

// NewStdLogger create a Logger that uses out and err for informational and
// error messages.
func NewStdLogger(out, err io.Writer) alloydb.Logger {
return &StdLogger{
infoLog: llog.New(out, "", llog.LstdFlags),
errLog: llog.New(err, "", llog.LstdFlags),
infoLog: llog.New(out, "", llog.LstdFlags),
debugLog: llog.New(out, "", llog.LstdFlags),
errLog: llog.New(err, "", llog.LstdFlags),
}
}

Expand All @@ -52,7 +54,7 @@ func (l *StdLogger) Errorf(format string, v ...interface{}) {

// Debugf logs debug messages.
func (l *StdLogger) Debugf(format string, v ...interface{}) {
l.infoLog.Printf(format, v...)
l.debugLog.Printf(format, v...)
}

// StructuredLogger writes log messages in JSON.
Expand All @@ -72,7 +74,7 @@ func (l *StructuredLogger) Errorf(format string, v ...interface{}) {

// Debugf logs debug messages.
func (l *StructuredLogger) Debugf(format string, v ...interface{}) {
l.logger.Infof(format, v...)
l.logger.Debugf(format, v...)
}

// NewStructuredLogger creates a Logger that logs messages using JSON.
Expand Down
8 changes: 8 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ type Config struct {
// See https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
StructuredLogs bool

// DebugLogs enables debug logging and is useful when diagnosing surprising
// Proxy behavior.
DebugLogs bool

// Quiet controls whether only error messages are logged.
Quiet bool

Expand Down Expand Up @@ -306,6 +310,10 @@ func (c *Config) DialerOptions(l alloydb.Logger) ([]alloydbconn.Option, error) {
opts = append(opts, alloydbconn.WithIAMAuthN())
}

if c.DebugLogs {
opts = append(opts, alloydbconn.WithDebugLogger(l))
}

return opts, nil
}

Expand Down

0 comments on commit 26770eb

Please sign in to comment.