Skip to content

Commit

Permalink
feat: add logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
codyoss committed Dec 13, 2024
1 parent 735a826 commit 9681cae
Show file tree
Hide file tree
Showing 30 changed files with 546 additions and 129 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ require (
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241118233622-e639e219e697 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
google.golang.org/genproto/googleapis/api v0.0.0-20241118233622-e639e219e697 h1:pgr/4QbFyktUv9CtQ/Fq4gzEE6/Xs7iCXbktaGzLHbQ=
google.golang.org/genproto/googleapis/api v0.0.0-20241118233622-e639e219e697/go.mod h1:+D9ySVjN8nY8YCVjc5O7PZDIdZporIDY3KaGfJunh88=
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 h1:M0KvPgPmDZHPlbRbaNU1APr28TvwvvdUPlSv7PUvy8g=
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:dguCy7UOdZhTvLzDyt15+rOrawrpM4q7DD9dQ1P11P4=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241206012308-a4fef0638583 h1:QNxhiucJGWLF/fFpnyTIk8GdEGTa6tUC7/JYG7VN3XU=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241206012308-a4fef0638583/go.mod h1:qUsLYwbwz5ostUWtuFuXPlHmSJodC5NI/88ZlHj4M1o=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241206012308-a4fef0638583 h1:IfdSdTcLFy4lqUQrQJLkLt1PB+AsqVz6lwkWPzWEz10=
Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/enterprise-certificate-proxy v0.2.3/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
github.com/googleapis/gax-go/v2 v2.8.0/go.mod h1:4orTrqY6hXxxaUL4LHIPl6lGo8vAE38/qKbhSAKP6QI=
github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU=
github.com/googleapis/gax-go/v2 v2.14.0 h1:f+jMrjBPl+DL9nI4IQzLUxMq7XrAqFYB7hBPqMNIe8o=
github.com/googleapis/gax-go/v2 v2.14.0/go.mod h1:lhBCnjdLrWRaPvLWhmc8IS24m9mr07qSYnHncrgo+zk=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
Expand Down
19 changes: 17 additions & 2 deletions google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ func (a *API) GenerateCode() ([]byte, error) {
"errors",
"fmt",
"io",
"log/slog",
"net/http",
"net/url",
"strconv",
Expand All @@ -820,6 +821,7 @@ func (a *API) GenerateCode() ([]byte, error) {
if a.Name == "storage" {
pn(" %q", "github.com/googleapis/gax-go/v2")
}
pn(" %q", "github.com/googleapis/gax-go/v2/internallog")
for _, imp := range []struct {
pkg string
lname string
Expand Down Expand Up @@ -895,7 +897,7 @@ func (a *API) GenerateCode() ([]byte, error) {

pn("client, endpoint, err := htransport.NewClient(ctx, opts...)")
pn("if err != nil { return nil, err }")
pn("s := &%s{client: client, BasePath: basePath}", service)
pn("s := &%s{client: client, BasePath: basePath, logger: internaloption.GetLogger(opts)}", service)
for _, res := range a.doc.Resources { // add top level resources.
pn("s.%s = New%s(s)", resourceGoField(res, nil), resourceGoType(res))
}
Expand All @@ -916,6 +918,7 @@ func (a *API) GenerateCode() ([]byte, error) {

pn("\ntype %s struct {", service)
pn(" client *http.Client")
pn(" logger *slog.Logger")
pn(" BasePath string // API endpoint base URL")
pn(" UserAgent string // optional additional User-Agent fragment")

Expand Down Expand Up @@ -993,6 +996,8 @@ func splitFileHeading(w io.Writer, pkg string) {
for _, imp := range []string{
"context",
"fmt",
"io",
"log/slog",
"net/http",
} {
pn(" %q", imp)
Expand Down Expand Up @@ -2342,12 +2347,18 @@ func (meth *Method) generateCode() {
}
pn(`})`)
}
logBody := "nil"
if hasBody {
logBody = "body.Bytes()"
}
if meth.supportsMediaUpload() && meth.api.Name == "storage" {
pn(`c.s.logger.DebugContext(c.ctx_, "api request", "serviceName", apiName, "rpcName", %q, "request", internallog.HTTPRequest(req, %s))`, meth.Id(), logBody)
pn("if c.retry != nil {")
pn(" return gensupport.SendRequestWithRetry(c.ctx_, c.s.client, req, c.retry)")
pn("}")
pn("return gensupport.SendRequest(c.ctx_, c.s.client, req)")
} else {
pn(`c.s.logger.DebugContext(c.ctx_, "api request", "serviceName", apiName, "rpcName", %q, "request", internallog.HTTPRequest(req, %s))`, meth.Id(), logBody)
pn("return gensupport.SendRequest(c.ctx_, c.s.client, req)")
}
pn("}")
Expand Down Expand Up @@ -2426,6 +2437,7 @@ func (meth *Method) generateCode() {
pn("}")
}
if retTypeComma == "" {
pn(`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, nil))`, meth.Id())
pn("return nil")
} else {
if meth.IsProtoStructResponse() {
Expand Down Expand Up @@ -2454,8 +2466,11 @@ func (meth *Method) generateCode() {
pn("if err := res.Body.Close(); err != nil { return nil, err }")
pn("if err := json.NewDecoder(bytes.NewReader(b.Bytes())).Decode(target); err != nil { return nil, err }")
pn("ret.Data = b.String()")
pn(`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, b.Bytes()))`, meth.Id())
} else {
pn("if err := gensupport.DecodeResponse(target, res); err != nil { return nil, err }")
pn("b, err := gensupport.DecodeResponseBytes(target, res)")
pn("if err != nil { return nil, err }")
pn(`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, b))`, meth.Id())
}
pn("return ret, nil")
}
Expand Down
Loading

0 comments on commit 9681cae

Please sign in to comment.