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 1efd6c4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 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
4 changes: 2 additions & 2 deletions 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.20240311180616-a9b8557ffb4e
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 Expand Up @@ -84,7 +84,7 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20240205150955-31a09d347014 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/grpc v1.61.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ 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/alloydbconn v1.7.1-0.20240311180616-a9b8557ffb4e h1:3SglE3sq2Bf2Oy1cJbuDh8k2vPZxpR1nuxYdnssaHD4=
cloud.google.com/go/alloydbconn v1.7.1-0.20240311180616-a9b8557ffb4e/go.mod h1:jt41sRKNEgy2LmQwH/1QOe2zOEmqLbdjtvYJ8EthA7k=
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 Expand Up @@ -1813,6 +1817,8 @@ google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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 1efd6c4

Please sign in to comment.