Skip to content

Commit 922e59d

Browse files
authoredNov 14, 2024··
feat(option): add support for user loggers (#2873)
Also add an intenral option that will be used by our clients to extract a logger from the options, or a default logger if one is not specified. More changes will be needed to make this pass through logger to auth layers in a future iteration.
1 parent 20de5d2 commit 922e59d

File tree

6 files changed

+149
-5
lines changed

6 files changed

+149
-5
lines changed
 

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/google/s2a-go v0.1.8
1212
github.com/google/uuid v1.6.0
1313
github.com/googleapis/enterprise-certificate-proxy v0.3.4
14-
github.com/googleapis/gax-go/v2 v2.13.0
14+
github.com/googleapis/gax-go/v2 v2.14.0
1515
go.opencensus.io v0.24.0
1616
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0
1717
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0

‎go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5656
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5757
github.com/googleapis/enterprise-certificate-proxy v0.3.4 h1:XYIDZApgAnrN1c855gTgghdIA6Stxb52D5RnLI1SLyw=
5858
github.com/googleapis/enterprise-certificate-proxy v0.3.4/go.mod h1:YKe7cfqYXjKGpGvmSg28/fFvhNzinZQm8DGnaburhGA=
59-
github.com/googleapis/gax-go/v2 v2.13.0 h1:yitjD5f7jQHhyDsnhKEBU52NdvvdSeGzlAnDPT0hH1s=
60-
github.com/googleapis/gax-go/v2 v2.13.0/go.mod h1:Z/fvTZXF8/uw7Xu5GuslPw+bplx6SS338j1Is2S+B7A=
59+
github.com/googleapis/gax-go/v2 v2.14.0 h1:f+jMrjBPl+DL9nI4IQzLUxMq7XrAqFYB7hBPqMNIe8o=
60+
github.com/googleapis/gax-go/v2 v2.14.0/go.mod h1:lhBCnjdLrWRaPvLWhmc8IS24m9mr07qSYnHncrgo+zk=
6161
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6262
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6363
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@@ -130,8 +130,8 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
130130
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
131131
google.golang.org/genproto v0.0.0-20241104194629-dd2ea8efbc28 h1:KJjNNclfpIkVqrZlTWcgOOaVQ00LdBnoEaRfkUx760s=
132132
google.golang.org/genproto v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:mt9/MofW7AWQ+Gy179ChOnvmJatV8YHUmrcedo9CIFI=
133-
google.golang.org/genproto/googleapis/api v0.0.0-20241021214115-324edc3d5d38 h1:2oV8dfuIkM1Ti7DwXc0BJfnwr9csz4TDXI9EmiI+Rbw=
134-
google.golang.org/genproto/googleapis/api v0.0.0-20241021214115-324edc3d5d38/go.mod h1:vuAjtvlwkDKF6L1GQ0SokiRLCGFfeBUXWr/aFFkHACc=
133+
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 h1:M0KvPgPmDZHPlbRbaNU1APr28TvwvvdUPlSv7PUvy8g=
134+
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:dguCy7UOdZhTvLzDyt15+rOrawrpM4q7DD9dQ1P11P4=
135135
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241104194629-dd2ea8efbc28 h1:Gmri4d/ZvKxZsbeZ/JvBHhAIRWXYwhvPyuS+LDQbnZk=
136136
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:T8O3fECQbif8cez15vxAcjbwXxvL2xbnvbQ7ZfiMAMs=
137137
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 h1:XVhgTWWV3kGQlwJHR3upFWZeTsei6Oks1apkZSeonIE=

‎go.work.sum

+111
Large diffs are not rendered by default.

‎internal/settings.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package internal
88
import (
99
"crypto/tls"
1010
"errors"
11+
"log/slog"
1112
"net/http"
1213
"os"
1314
"strconv"
@@ -62,6 +63,7 @@ type DialSettings struct {
6263
AllowNonDefaultServiceAccount bool
6364
DefaultUniverseDomain string
6465
UniverseDomain string
66+
Logger *slog.Logger
6567
// Google API system parameters. For more information please read:
6668
// https://cloud.google.com/apis/docs/system-parameters
6769
QuotaProject string

‎option/internaloption/internaloption.go

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package internaloption
77

88
import (
9+
"log/slog"
10+
11+
"github.com/googleapis/gax-go/v2/internallog"
912
"golang.org/x/oauth2/google"
1013
"google.golang.org/api/internal"
1114
"google.golang.org/api/option"
@@ -212,3 +215,16 @@ func (w enableNewAuthLibrary) Apply(o *internal.DialSettings) {
212215
type EmbeddableAdapter struct{}
213216

214217
func (*EmbeddableAdapter) Apply(_ *internal.DialSettings) {}
218+
219+
// GetLogger is a helper for client libraries to extract the [slog.Logger] from
220+
// the provided options or return a default logger if one is not found.
221+
//
222+
// It should only be used internally by generated clients. This is an EXPERIMENTAL API
223+
// and may be changed or removed in the future.
224+
func GetLogger(opts []option.ClientOption) *slog.Logger {
225+
var ds internal.DialSettings
226+
for _, opt := range opts {
227+
opt.Apply(&ds)
228+
}
229+
return internallog.New(ds.Logger)
230+
}

‎option/option.go

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package option
77

88
import (
99
"crypto/tls"
10+
"log/slog"
1011
"net/http"
1112

1213
"cloud.google.com/go/auth"
@@ -368,3 +369,17 @@ type withUniverseDomain string
368369
func (w withUniverseDomain) Apply(o *internal.DialSettings) {
369370
o.UniverseDomain = string(w)
370371
}
372+
373+
// WithLogger returns a ClientOption that sets the logger used throughout the
374+
// client library call stack. If this option is provided it takes precedence
375+
// over the value set in GOOGLE_SDK_GO_LOGGING_LEVEL. Specifying this option
376+
// enables logging at the provided logger's configured level.
377+
func WithLogger(l *slog.Logger) ClientOption {
378+
return withLogger{l}
379+
}
380+
381+
type withLogger struct{ l *slog.Logger }
382+
383+
func (w withLogger) Apply(o *internal.DialSettings) {
384+
o.Logger = w.l
385+
}

0 commit comments

Comments
 (0)
Please sign in to comment.