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 12, 2024
1 parent 4cdb1af commit cbf6866
Show file tree
Hide file tree
Showing 31 changed files with 638 additions and 237 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
3 changes: 3 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,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 Expand Up @@ -704,6 +706,7 @@ go.opentelemetry.io/otel/trace v1.22.0/go.mod h1:RbbHXVqKES9QhzZq/fE5UnOSILqRt40
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
43 changes: 31 additions & 12 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,10 @@ func (a *API) GenerateCode() ([]byte, error) {

pn("client, endpoint, err := htransport.NewClient(ctx, opts...)")
pn("if err != nil { return nil, err }")
pn("s, err := New(client)")
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))
}
pn("if err != nil { return nil, err }")
pn(`if endpoint != "" { s.BasePath = endpoint }`)
pn("return s, nil")
Expand All @@ -908,15 +913,12 @@ func (a *API) GenerateCode() ([]byte, error) {
pn("// If you are using google.golang.org/api/googleapis/transport.APIKey, use option.WithAPIKey with NewService instead.")
pn("func New(client *http.Client) (*%s, error) {", service)
pn("if client == nil { return nil, errors.New(\"client is nil\") }")
pn("s := &%s{client: client, BasePath: basePath}", service)
for _, res := range a.doc.Resources { // add top level resources.
pn("s.%s = New%s(s)", resourceGoField(res, nil), resourceGoType(res))
}
pn("return s, nil")
pn("return NewService(option.WithHTTPClient(client))")
pn("}")

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 @@ -995,6 +997,7 @@ func splitFileHeading(w io.Writer, pkg string) {
"context",
"fmt",
"io",
"log/slog",
"net/http",
} {
pn(" %q", imp)
Expand Down Expand Up @@ -2280,26 +2283,32 @@ func (meth *Method) generateCode() {
pn("}")
}
pn("var body io.Reader = nil")
var hasBody bool
if meth.IsRawRequest() {
pn("body = c.body_")
pn("body := bytes.NewBuffer(nil)")
pn("_, err := body.ReadFrom(c.body_)")
pn("if err != nil { return nil, err }")
hasBody = true
} else if meth.IsProtoStructRequest() {
pn("protoBytes, err := json.Marshal(c.req)")
pn("if err != nil { return nil, err }")
pn("body = bytes.NewReader(protoBytes)")
pn("body := bytes.NewReader(protoBytes)")
hasBody = true
} else {
if ba := args.bodyArg(); ba != nil && httpMethod != "GET" {
if meth.m.ID == "ml.projects.predict" {
// TODO(cbro): move ML API to rawHTTP (it will be a breaking change)
// Skip JSONReader for APIs that require clients to pass in JSON already.
pn("body = strings.NewReader(c.%s.HttpBody.Data)", ba.goname)
// Skip JSONBuffer for APIs that require clients to pass in JSON already.
pn("body := bytes.NewBufferString(c.%s.HttpBody.Data)", ba.goname)
} else {
style := "WithoutDataWrapper"
if a.needsDataWrapper() {
style = "WithDataWrapper"
}
pn("body, err := googleapi.%s.JSONReader(c.%s)", style, ba.goname)
pn("body, err := googleapi.%s.JSONBuffer(c.%s)", style, ba.goname)
pn("if err != nil { return nil, err }")
}
hasBody = true
}
pn(`c.urlParams_.Set("alt", alt)`)
pn(`c.urlParams_.Set("prettyPrint", "false")`)
Expand Down Expand Up @@ -2337,12 +2346,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 @@ -2421,6 +2436,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 @@ -2449,8 +2465,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 cbf6866

Please sign in to comment.