From 8daa39a27c9d34865504fb997b2c00f478439e50 Mon Sep 17 00:00:00 2001 From: Mikhail Mazurskiy Date: Tue, 10 Oct 2023 19:58:58 +1100 Subject: [PATCH 1/4] Replace glog with grpclog --- README.md | 5 +- .../development/installation_for_cygwin.md | 1 - .../cmd/example-gateway-server/main.go | 5 +- .../internal/cmd/example-grpc-server/main.go | 5 +- examples/internal/gateway/handlers.go | 8 ++-- examples/internal/gateway/main.go | 12 ++--- examples/internal/integration/main_test.go | 9 ++-- .../internal/server/a_bit_of_everything.go | 30 ++++++------ examples/internal/server/echo.go | 12 ++--- examples/internal/server/main.go | 10 ++-- .../internal/server/non_standard_names.go | 10 ++-- examples/internal/server/unannotatedecho.go | 8 ++-- go.mod | 1 - go.sum | 2 - internal/descriptor/registry.go | 18 +++++-- internal/descriptor/services.go | 48 +++++++++++++------ internal/httprule/parse_test.go | 15 ++---- 17 files changed, 105 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index 0a2dd73d6c1..ae4bd65bcc5 100644 --- a/README.md +++ b/README.md @@ -357,10 +357,10 @@ import ( "flag" "net/http" - "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/grpclog" gw "github.com/yourorg/yourrepo/proto/gen/go/your/service/v1/your_service" // Update ) @@ -391,10 +391,9 @@ func run() error { func main() { flag.Parse() - defer glog.Flush() if err := run(); err != nil { - glog.Fatal(err) + grpclog.Fatal(err) } } ``` diff --git a/docs/docs/development/installation_for_cygwin.md b/docs/docs/development/installation_for_cygwin.md index 2ed9ed8aefa..2e55615342c 100644 --- a/docs/docs/development/installation_for_cygwin.md +++ b/docs/docs/development/installation_for_cygwin.md @@ -54,7 +54,6 @@ This will probably fail with a similar output to this: To fix this you need to run the `go get -u -v` commands and look for all lines starting with `# cd .;`. Copy and paste these lines into your shell and change the clone destination directories. git clone https://github.com/grpc-ecosystem/grpc-gateway $(cygpath -u $GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway - git clone https://github.com/golang/glog $(cygpath -u $GOPATH)/src/github.com/golang/glog git clone https://github.com/golang/protobuf $(cygpath -u $GOPATH)/src/github.com/golang/protobuf git clone https://github.com/google/go-genproto $(cygpath -u $GOPATH)/src/google.golang.org/genproto diff --git a/examples/internal/cmd/example-gateway-server/main.go b/examples/internal/cmd/example-gateway-server/main.go index dca90a6aa99..821a8e8db81 100644 --- a/examples/internal/cmd/example-gateway-server/main.go +++ b/examples/internal/cmd/example-gateway-server/main.go @@ -8,8 +8,8 @@ import ( "context" "flag" - "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/gateway" + "google.golang.org/grpc/grpclog" ) var ( @@ -20,7 +20,6 @@ var ( func main() { flag.Parse() - defer glog.Flush() ctx := context.Background() opts := gateway.Options{ @@ -32,6 +31,6 @@ func main() { OpenAPIDir: *openAPIDir, } if err := gateway.Run(ctx, opts); err != nil { - glog.Fatal(err) + grpclog.Fatal(err) } } diff --git a/examples/internal/cmd/example-grpc-server/main.go b/examples/internal/cmd/example-grpc-server/main.go index 72819588836..965d113d7e7 100644 --- a/examples/internal/cmd/example-grpc-server/main.go +++ b/examples/internal/cmd/example-grpc-server/main.go @@ -8,8 +8,8 @@ import ( "context" "flag" - "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/server" + "google.golang.org/grpc/grpclog" ) var ( @@ -19,10 +19,9 @@ var ( func main() { flag.Parse() - defer glog.Flush() ctx := context.Background() if err := server.Run(ctx, *network, *addr); err != nil { - glog.Fatal(err) + grpclog.Fatal(err) } } diff --git a/examples/internal/gateway/handlers.go b/examples/internal/gateway/handlers.go index 1f617b1cc2f..6fdb3cec102 100644 --- a/examples/internal/gateway/handlers.go +++ b/examples/internal/gateway/handlers.go @@ -6,21 +6,21 @@ import ( "path" "strings" - "github.com/golang/glog" "google.golang.org/grpc" "google.golang.org/grpc/connectivity" + "google.golang.org/grpc/grpclog" ) // openAPIServer returns OpenAPI specification files located under "/openapiv2/" func openAPIServer(dir string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if !strings.HasSuffix(r.URL.Path, ".swagger.json") { - glog.Errorf("Not Found: %s", r.URL.Path) + grpclog.Errorf("Not Found: %s", r.URL.Path) http.NotFound(w, r) return } - glog.Infof("Serving %s", r.URL.Path) + grpclog.Infof("Serving %s", r.URL.Path) p := strings.TrimPrefix(r.URL.Path, "/openapiv2/") p = path.Join(dir, p) http.ServeFile(w, r, p) @@ -50,7 +50,7 @@ func preflightHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ",")) methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"} w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ",")) - glog.Infof("preflight request for %s", r.URL.Path) + grpclog.Infof("Preflight request for %s", r.URL.Path) } // healthzServer returns a simple health handler which returns ok. diff --git a/examples/internal/gateway/main.go b/examples/internal/gateway/main.go index cacd2e4072b..c7035b40fc4 100644 --- a/examples/internal/gateway/main.go +++ b/examples/internal/gateway/main.go @@ -4,8 +4,8 @@ import ( "context" "net/http" - "github.com/golang/glog" gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" + "google.golang.org/grpc/grpclog" ) // Endpoint describes a gRPC endpoint @@ -42,7 +42,7 @@ func Run(ctx context.Context, opts Options) error { go func() { <-ctx.Done() if err := conn.Close(); err != nil { - glog.Errorf("Failed to close a client connection to the gRPC server: %v", err) + grpclog.Errorf("Failed to close a client connection to the gRPC server: %v", err) } }() @@ -62,15 +62,15 @@ func Run(ctx context.Context, opts Options) error { } go func() { <-ctx.Done() - glog.Infof("Shutting down the http server") + grpclog.Infof("Shutting down the http server") if err := s.Shutdown(context.Background()); err != nil { - glog.Errorf("Failed to shutdown http server: %v", err) + grpclog.Errorf("Failed to shutdown http server: %v", err) } }() - glog.Infof("Starting listening at %s", opts.Addr) + grpclog.Infof("Starting listening at %s", opts.Addr) if err := s.ListenAndServe(); err != http.ErrServerClosed { - glog.Errorf("Failed to listen and serve: %v", err) + grpclog.Errorf("Failed to listen and serve: %v", err) return err } return nil diff --git a/examples/internal/integration/main_test.go b/examples/internal/integration/main_test.go index e110a5462c1..634d08e4238 100644 --- a/examples/internal/integration/main_test.go +++ b/examples/internal/integration/main_test.go @@ -9,10 +9,10 @@ import ( "testing" "time" - "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/gateway" - server "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/server" + "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/server" gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" + "google.golang.org/grpc/grpclog" ) var ( @@ -42,7 +42,7 @@ func waitForGateway(ctx context.Context, port uint16) error { return nil } - glog.Infof("Waiting for localhost:%d to get ready", port) + grpclog.Infof("Waiting for localhost:%d to get ready", port) select { case <-ctx.Done(): return err @@ -75,7 +75,6 @@ func runServers(ctx context.Context) <-chan error { func TestMain(m *testing.M) { flag.Parse() - defer glog.Flush() ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -84,7 +83,7 @@ func TestMain(m *testing.M) { ch := make(chan int, 1) go func() { if err := waitForGateway(ctx, 8088); err != nil { - glog.Errorf("waitForGateway(ctx, 8088) failed with %v; want success", err) + grpclog.Errorf("waitForGateway(ctx, 8088) failed with %v; want success", err) } ch <- m.Run() }() diff --git a/examples/internal/server/a_bit_of_everything.go b/examples/internal/server/a_bit_of_everything.go index fc94846a2d0..b2583baccee 100644 --- a/examples/internal/server/a_bit_of_everything.go +++ b/examples/internal/server/a_bit_of_everything.go @@ -7,7 +7,6 @@ import ( "sync" "time" - "github.com/golang/glog" examples "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb" "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/oneofenum" "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/pathenum" @@ -19,6 +18,7 @@ import ( statuspb "google.golang.org/genproto/googleapis/rpc/status" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/durationpb" @@ -50,7 +50,7 @@ func (s *_ABitOfEverythingServer) Create(ctx context.Context, msg *examples.ABit s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) var uuid string for { uuid = fmt.Sprintf("%x", uuidgen.Next()) @@ -60,7 +60,7 @@ func (s *_ABitOfEverythingServer) Create(ctx context.Context, msg *examples.ABit } s.v[uuid] = msg s.v[uuid].Uuid = uuid - glog.Infof("%v", s.v[uuid]) + grpclog.Infof("%v", s.v[uuid]) return s.v[uuid], nil } @@ -95,7 +95,7 @@ func (s *_ABitOfEverythingServer) BulkCreate(stream examples.StreamService_BulkC return err } count++ - glog.Info(msg) + grpclog.Info(msg) if _, err = s.Create(ctx, msg); err != nil { return err } @@ -118,7 +118,7 @@ func (s *_ABitOfEverythingServer) BulkCreate(stream examples.StreamService_BulkC func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *sub2.IdMessage) (*examples.ABitOfEverything, error) { s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) err := grpc.SendHeader(ctx, metadata.New(map[string]string{ "uuid": msg.Uuid, @@ -196,7 +196,7 @@ func (s *_ABitOfEverythingServer) Custom(ctx context.Context, msg *examples.ABit s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) if _, ok := s.v[msg.Uuid]; ok { s.v[msg.Uuid] = msg } else { @@ -209,7 +209,7 @@ func (s *_ABitOfEverythingServer) DoubleColon(ctx context.Context, msg *examples s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) if _, ok := s.v[msg.Uuid]; ok { s.v[msg.Uuid] = msg } else { @@ -222,7 +222,7 @@ func (s *_ABitOfEverythingServer) Update(ctx context.Context, msg *examples.ABit s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) if _, ok := s.v[msg.Uuid]; ok { s.v[msg.Uuid] = msg } else { @@ -232,7 +232,7 @@ func (s *_ABitOfEverythingServer) Update(ctx context.Context, msg *examples.ABit } func (s *_ABitOfEverythingServer) UpdateV2(ctx context.Context, msg *examples.UpdateV2Request) (*emptypb.Empty, error) { - glog.Info(msg) + grpclog.Info(msg) // If there is no update mask do a regular update if msg.UpdateMask == nil || len(msg.UpdateMask.GetPaths()) == 0 { return s.Update(ctx, msg.Abe) @@ -252,7 +252,7 @@ func (s *_ABitOfEverythingServer) Delete(ctx context.Context, msg *sub2.IdMessag s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) if _, ok := s.v[msg.Uuid]; ok { delete(s.v, msg.Uuid) } else { @@ -265,7 +265,7 @@ func (s *_ABitOfEverythingServer) GetQuery(ctx context.Context, msg *examples.AB s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) if _, ok := s.v[msg.Uuid]; ok { s.v[msg.Uuid] = msg } else { @@ -278,7 +278,7 @@ func (s *_ABitOfEverythingServer) GetRepeatedQuery(ctx context.Context, msg *exa s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) return msg, nil } @@ -286,7 +286,7 @@ func (s *_ABitOfEverythingServer) Echo(ctx context.Context, msg *sub.StringMessa s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) return msg, nil } @@ -312,7 +312,7 @@ func (s *_ABitOfEverythingServer) BulkEcho(stream examples.StreamService_BulkEch } for _, msg := range msgs { - glog.Info(msg) + grpclog.Info(msg) if err := stream.Send(msg); err != nil { return err } @@ -329,7 +329,7 @@ func (s *_ABitOfEverythingServer) DeepPathEcho(ctx context.Context, msg *example s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + grpclog.Info(msg) return msg, nil } diff --git a/examples/internal/server/echo.go b/examples/internal/server/echo.go index 0f795ce52ad..ac65925c035 100644 --- a/examples/internal/server/echo.go +++ b/examples/internal/server/echo.go @@ -3,10 +3,10 @@ package server import ( "context" - "github.com/golang/glog" examples "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" ) @@ -20,12 +20,12 @@ func newEchoServer() examples.EchoServiceServer { } func (s *echoServer) Echo(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { - glog.Info(msg) + grpclog.Info(msg) return msg, nil } func (s *echoServer) EchoBody(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { - glog.Info(msg) + grpclog.Info(msg) grpc.SendHeader(ctx, metadata.New(map[string]string{ "foo": "foo1", "bar": "bar1", @@ -38,16 +38,16 @@ func (s *echoServer) EchoBody(ctx context.Context, msg *examples.SimpleMessage) } func (s *echoServer) EchoDelete(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { - glog.Info(msg) + grpclog.Info(msg) return msg, nil } func (s *echoServer) EchoPatch(ctx context.Context, msg *examples.DynamicMessageUpdate) (*examples.DynamicMessageUpdate, error) { - glog.Info(msg) + grpclog.Info(msg) return msg, nil } func (s *echoServer) EchoUnauthorized(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { - glog.Info(msg) + grpclog.Info(msg) return nil, status.Error(codes.Unauthenticated, "unauthorized err") } diff --git a/examples/internal/server/main.go b/examples/internal/server/main.go index 8c42443ed45..eea684359d2 100644 --- a/examples/internal/server/main.go +++ b/examples/internal/server/main.go @@ -5,11 +5,11 @@ import ( "net" "net/http" - "github.com/golang/glog" examples "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb" standalone "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/standalone" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "google.golang.org/grpc" + "google.golang.org/grpc/grpclog" ) // Run starts the example gRPC service. @@ -21,7 +21,7 @@ func Run(ctx context.Context, network, address string) error { } defer func() { if err := l.Close(); err != nil { - glog.Errorf("Failed to close %s %s: %v", network, address, err) + grpclog.Errorf("Failed to close %s %s: %v", network, address, err) } }() @@ -64,14 +64,14 @@ func RunInProcessGateway(ctx context.Context, addr string, opts ...runtime.Serve go func() { <-ctx.Done() - glog.Infof("Shutting down the http gateway server") + grpclog.Infof("Shutting down the http gateway server") if err := s.Shutdown(context.Background()); err != nil { - glog.Errorf("Failed to shutdown http gateway server: %v", err) + grpclog.Errorf("Failed to shutdown http gateway server: %v", err) } }() if err := s.ListenAndServe(); err != http.ErrServerClosed { - glog.Errorf("Failed to listen and serve: %v", err) + grpclog.Errorf("Failed to listen and serve: %v", err) return err } return nil diff --git a/examples/internal/server/non_standard_names.go b/examples/internal/server/non_standard_names.go index 0305470c627..09a5d74bd21 100644 --- a/examples/internal/server/non_standard_names.go +++ b/examples/internal/server/non_standard_names.go @@ -3,8 +3,8 @@ package server import ( "context" - "github.com/golang/glog" examples "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb" + "google.golang.org/grpc/grpclog" ) // Implements NonStandardServiceServer @@ -16,25 +16,25 @@ func newNonStandardServer() examples.NonStandardServiceServer { } func (s *nonStandardServer) Update(ctx context.Context, msg *examples.NonStandardUpdateRequest) (*examples.NonStandardMessage, error) { - glog.Info(msg) + grpclog.Info(msg) newMsg := &examples.NonStandardMessage{ Thing: &examples.NonStandardMessage_Thing{SubThing: &examples.NonStandardMessage_Thing_SubThing{}}, // The fieldmask_helper doesn't generate nested structs if they are nil } applyFieldMask(newMsg, msg.Body, msg.UpdateMask) - glog.Info(newMsg) + grpclog.Info(newMsg) return newMsg, nil } func (s *nonStandardServer) UpdateWithJSONNames(ctx context.Context, msg *examples.NonStandardWithJSONNamesUpdateRequest) (*examples.NonStandardMessageWithJSONNames, error) { - glog.Info(msg) + grpclog.Info(msg) newMsg := &examples.NonStandardMessageWithJSONNames{ Thing: &examples.NonStandardMessageWithJSONNames_Thing{SubThing: &examples.NonStandardMessageWithJSONNames_Thing_SubThing{}}, // The fieldmask_helper doesn't generate nested structs if they are nil } applyFieldMask(newMsg, msg.Body, msg.UpdateMask) - glog.Info(newMsg) + grpclog.Info(newMsg) return newMsg, nil } diff --git a/examples/internal/server/unannotatedecho.go b/examples/internal/server/unannotatedecho.go index 2342be1084a..a755e2d94d6 100644 --- a/examples/internal/server/unannotatedecho.go +++ b/examples/internal/server/unannotatedecho.go @@ -3,9 +3,9 @@ package server import ( "context" - "github.com/golang/glog" examples "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb" "google.golang.org/grpc" + "google.golang.org/grpc/grpclog" "google.golang.org/grpc/metadata" ) @@ -18,12 +18,12 @@ func newUnannotatedEchoServer() examples.UnannotatedEchoServiceServer { } func (s *unannotatedEchoServer) Echo(ctx context.Context, msg *examples.UnannotatedSimpleMessage) (*examples.UnannotatedSimpleMessage, error) { - glog.Info(msg) + grpclog.Info(msg) return msg, nil } func (s *unannotatedEchoServer) EchoBody(ctx context.Context, msg *examples.UnannotatedSimpleMessage) (*examples.UnannotatedSimpleMessage, error) { - glog.Info(msg) + grpclog.Info(msg) grpc.SendHeader(ctx, metadata.New(map[string]string{ "foo": "foo1", "bar": "bar1", @@ -36,6 +36,6 @@ func (s *unannotatedEchoServer) EchoBody(ctx context.Context, msg *examples.Unan } func (s *unannotatedEchoServer) EchoDelete(ctx context.Context, msg *examples.UnannotatedSimpleMessage) (*examples.UnannotatedSimpleMessage, error) { - glog.Info(msg) + grpclog.Info(msg) return msg, nil } diff --git a/go.mod b/go.mod index f69ea1f9531..cffba9d784c 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.19 require ( github.com/antihax/optional v1.0.0 - github.com/golang/glog v1.1.0 github.com/google/go-cmp v0.6.0 github.com/rogpeppe/fastuuid v1.2.0 golang.org/x/oauth2 v0.13.0 diff --git a/go.sum b/go.sum index ad5c0236200..9da32cd7238 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,6 @@ github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE= -github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= diff --git a/internal/descriptor/registry.go b/internal/descriptor/registry.go index e9d4b2f7f0a..1cfc4b02fba 100644 --- a/internal/descriptor/registry.go +++ b/internal/descriptor/registry.go @@ -5,13 +5,13 @@ import ( "sort" "strings" - "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/v2/internal/codegenerator" "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/openapiconfig" "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options" "golang.org/x/text/cases" "golang.org/x/text/language" "google.golang.org/genproto/googleapis/api/annotations" + "google.golang.org/grpc/grpclog" "google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/types/descriptorpb" "google.golang.org/protobuf/types/pluginpb" @@ -283,7 +283,9 @@ func (r *Registry) registerMsg(file *File, outerPath []string, msgs []*descripto } file.Messages = append(file.Messages, m) r.msgs[m.FQMN()] = m - glog.V(1).Infof("register name: %s", m.FQMN()) + if grpclog.V(1) { + grpclog.Infof("Register name: %s", m.FQMN()) + } var outers []string outers = append(outers, outerPath...) @@ -304,14 +306,18 @@ func (r *Registry) registerEnum(file *File, outerPath []string, enums []*descrip } file.Enums = append(file.Enums, e) r.enums[e.FQEN()] = e - glog.V(1).Infof("register enum name: %s", e.FQEN()) + if grpclog.V(1) { + grpclog.Infof("Register enum name: %s", e.FQEN()) + } } } // LookupMsg looks up a message type by "name". // It tries to resolve "name" from "location" if "name" is a relative message name. func (r *Registry) LookupMsg(location, name string) (*Message, error) { - glog.V(1).Infof("lookup %s from %s", name, location) + if grpclog.V(1) { + grpclog.Infof("Lookup %s from %s", name, location) + } if strings.HasPrefix(name, ".") { m, ok := r.msgs[name] if !ok { @@ -337,7 +343,9 @@ func (r *Registry) LookupMsg(location, name string) (*Message, error) { // LookupEnum looks up a enum type by "name". // It tries to resolve "name" from "location" if "name" is a relative enum name. func (r *Registry) LookupEnum(location, name string) (*Enum, error) { - glog.V(1).Infof("lookup enum %s from %s", name, location) + if grpclog.V(1) { + grpclog.Infof("Lookup enum %s from %s", name, location) + } if strings.HasPrefix(name, ".") { e, ok := r.enums[name] if !ok { diff --git a/internal/descriptor/services.go b/internal/descriptor/services.go index 8936a78222e..0ff28d99939 100644 --- a/internal/descriptor/services.go +++ b/internal/descriptor/services.go @@ -5,9 +5,9 @@ import ( "fmt" "strings" - "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/v2/internal/httprule" options "google.golang.org/genproto/googleapis/api/annotations" + "google.golang.org/grpc/grpclog" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/descriptorpb" ) @@ -16,20 +16,26 @@ import ( // It must be called after loadFile is called for all files so that loadServices // can resolve names of message types and their fields. func (r *Registry) loadServices(file *File) error { - glog.V(1).Infof("Loading services from %s", file.GetName()) + if grpclog.V(1) { + grpclog.Infof("Loading services from %s", file.GetName()) + } var svcs []*Service for _, sd := range file.GetService() { - glog.V(2).Infof("Registering %s", sd.GetName()) + if grpclog.V(2) { + grpclog.Infof("Registering %s", sd.GetName()) + } svc := &Service{ File: file, ServiceDescriptorProto: sd, ForcePrefixedName: r.standalone, } for _, md := range sd.GetMethod() { - glog.V(2).Infof("Processing %s.%s", sd.GetName(), md.GetName()) + if grpclog.V(2) { + grpclog.Infof("Processing %s.%s", sd.GetName(), md.GetName()) + } opts, err := extractAPIOptions(md) if err != nil { - glog.Errorf("Failed to extract HttpRule from %s.%s: %v", svc.GetName(), md.GetName(), err) + grpclog.Errorf("Failed to extract HttpRule from %s.%s: %v", svc.GetName(), md.GetName(), err) return err } optsList := r.LookupExternalHTTPRules((&Method{Service: svc, MethodDescriptorProto: md}).FQMN()) @@ -40,16 +46,18 @@ func (r *Registry) loadServices(file *File) error { if r.generateUnboundMethods { defaultOpts, err := defaultAPIOptions(svc, md) if err != nil { - glog.Errorf("Failed to generate default HttpRule from %s.%s: %v", svc.GetName(), md.GetName(), err) + grpclog.Errorf("Failed to generate default HttpRule from %s.%s: %v", svc.GetName(), md.GetName(), err) return err } optsList = append(optsList, defaultOpts) } else { - logFn := glog.V(1).Infof - if r.warnOnUnboundMethods { - logFn = glog.Warningf + if grpclog.V(1) { + logFn := grpclog.Infof + if r.warnOnUnboundMethods { + logFn = grpclog.Warningf + } + logFn("No HttpRule found for method: %s.%s", svc.GetName(), md.GetName()) } - logFn("No HttpRule found for method: %s.%s", svc.GetName(), md.GetName()) } } meth, err := r.newMethod(svc, md, optsList) @@ -61,7 +69,9 @@ func (r *Registry) loadServices(file *File) error { if len(svc.Methods) == 0 { continue } - glog.V(2).Infof("Registered %s with %d method(s)", svc.GetName(), len(svc.Methods)) + if grpclog.V(2) { + grpclog.Infof("Registered %s with %d method(s)", svc.GetName(), len(svc.Methods)) + } svcs = append(svcs, svc) } file.Services = svcs @@ -122,7 +132,9 @@ func (r *Registry) newMethod(svc *Service, md *descriptorpb.MethodDescriptorProt pathTemplate = custom.Path default: - glog.V(1).Infof("No pattern specified in google.api.HttpRule: %s", md.GetName()) + if grpclog.V(1) { + grpclog.Infof("No pattern specified in google.api.HttpRule: %s", md.GetName()) + } return nil, nil } @@ -245,9 +257,13 @@ func (r *Registry) newParam(meth *Method, path string) (Parameter, error) { target := fields[l-1].Target switch target.GetType() { case descriptorpb.FieldDescriptorProto_TYPE_MESSAGE, descriptorpb.FieldDescriptorProto_TYPE_GROUP: - glog.V(2).Infoln("found aggregate type:", target, target.TypeName) + if grpclog.V(2) { + grpclog.Infoln("found aggregate type:", target, target.TypeName) + } if IsWellKnownType(*target.TypeName) { - glog.V(2).Infoln("found well known aggregate type:", target) + if grpclog.V(2) { + grpclog.Infoln("found well known aggregate type:", target) + } } else { return Parameter{}, fmt.Errorf("%s.%s: %s is a protobuf message type. Protobuf message types cannot be used as path parameters, use a scalar value type (such as string) instead", meth.Service.GetName(), meth.GetName(), path) } @@ -321,7 +337,9 @@ func (r *Registry) resolveFieldPath(msg *Message, path string, isPathParam bool) } } - glog.V(2).Infof("Lookup %s in %s", c, msg.FQMN()) + if grpclog.V(2) { + grpclog.Infof("Lookup %s in %s", c, msg.FQMN()) + } f := lookupField(msg, c) if f == nil { return nil, fmt.Errorf("no field %q found in %s", path, root.GetName()) diff --git a/internal/httprule/parse_test.go b/internal/httprule/parse_test.go index 25cca5d8b45..7bf11a35a3e 100644 --- a/internal/httprule/parse_test.go +++ b/internal/httprule/parse_test.go @@ -2,12 +2,11 @@ package httprule import ( "errors" - "flag" "fmt" "reflect" "testing" - "github.com/golang/glog" + "google.golang.org/grpc/grpclog" ) func TestTokenize(t *testing.T) { @@ -133,10 +132,6 @@ func TestTokenize(t *testing.T) { } func TestParseSegments(t *testing.T) { - err := flag.Set("v", "3") - if err != nil { - t.Fatalf("failed to set flag: %v", err) - } for _, spec := range []struct { tokens []string want []segment @@ -374,10 +369,6 @@ func TestParseError(t *testing.T) { } func TestParseSegmentsWithErrors(t *testing.T) { - err := flag.Set("v", "3") - if err != nil { - t.Fatalf("failed to set flag: %v", err) - } for _, spec := range []struct { tokens []string }{ @@ -440,6 +431,8 @@ func TestParseSegmentsWithErrors(t *testing.T) { t.Errorf("parser{%q}.segments() succeeded; want InvalidTemplateError; accepted %#v", spec.tokens, segs) continue } - glog.V(1).Info(err) + if grpclog.V(1) { + grpclog.Info(err) + } } } From cc2bd20d90ad0866567154cc5fb8b659112e884e Mon Sep 17 00:00:00 2001 From: Mikhail Mazurskiy Date: Tue, 10 Oct 2023 20:10:19 +1100 Subject: [PATCH 2/4] Update build files --- examples/internal/cmd/example-gateway-server/BUILD.bazel | 2 +- examples/internal/cmd/example-grpc-server/BUILD.bazel | 2 +- examples/internal/gateway/BUILD.bazel | 2 +- examples/internal/integration/BUILD.bazel | 2 +- examples/internal/server/BUILD.bazel | 2 +- internal/descriptor/BUILD.bazel | 2 +- internal/httprule/BUILD.bazel | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/internal/cmd/example-gateway-server/BUILD.bazel b/examples/internal/cmd/example-gateway-server/BUILD.bazel index a56015d065c..91f93321010 100644 --- a/examples/internal/cmd/example-gateway-server/BUILD.bazel +++ b/examples/internal/cmd/example-gateway-server/BUILD.bazel @@ -7,7 +7,7 @@ go_library( visibility = ["//visibility:private"], deps = [ "//examples/internal/gateway", - "@com_github_golang_glog//:glog", + "@org_golang_google_grpc//grpclog", ], ) diff --git a/examples/internal/cmd/example-grpc-server/BUILD.bazel b/examples/internal/cmd/example-grpc-server/BUILD.bazel index 7f216a10520..95143a71e36 100644 --- a/examples/internal/cmd/example-grpc-server/BUILD.bazel +++ b/examples/internal/cmd/example-grpc-server/BUILD.bazel @@ -8,7 +8,7 @@ go_library( importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/cmd/example-grpc-server", deps = [ "//examples/internal/server", - "@com_github_golang_glog//:glog", + "@org_golang_google_grpc//grpclog", ], ) diff --git a/examples/internal/gateway/BUILD.bazel b/examples/internal/gateway/BUILD.bazel index 3b6ba2969e6..d5df8622fb6 100644 --- a/examples/internal/gateway/BUILD.bazel +++ b/examples/internal/gateway/BUILD.bazel @@ -14,11 +14,11 @@ go_library( "//examples/internal/proto/examplepb", "//examples/internal/proto/standalone", "//runtime", - "@com_github_golang_glog//:glog", "@org_golang_google_genproto_googleapis_rpc//errdetails", "@org_golang_google_grpc//:go_default_library", "@org_golang_google_grpc//connectivity", "@org_golang_google_grpc//credentials/insecure", + "@org_golang_google_grpc//grpclog", ], ) diff --git a/examples/internal/integration/BUILD.bazel b/examples/internal/integration/BUILD.bazel index b10eeecf770..08d1a021385 100644 --- a/examples/internal/integration/BUILD.bazel +++ b/examples/internal/integration/BUILD.bazel @@ -17,11 +17,11 @@ go_test( "//examples/internal/proto/sub", "//examples/internal/server", "//runtime", - "@com_github_golang_glog//:glog", "@com_github_google_go_cmp//cmp", "@com_github_rogpeppe_fastuuid//:fastuuid", "@org_golang_google_genproto_googleapis_rpc//status", "@org_golang_google_grpc//codes", + "@org_golang_google_grpc//grpclog", "@org_golang_google_protobuf//encoding/protojson", "@org_golang_google_protobuf//proto", "@org_golang_google_protobuf//testing/protocmp", diff --git a/examples/internal/server/BUILD.bazel b/examples/internal/server/BUILD.bazel index 956604c19bd..a7d2365771a 100644 --- a/examples/internal/server/BUILD.bazel +++ b/examples/internal/server/BUILD.bazel @@ -23,13 +23,13 @@ go_library( "//examples/internal/proto/sub", "//examples/internal/proto/sub2", "//runtime", - "@com_github_golang_glog//:glog", "@com_github_rogpeppe_fastuuid//:fastuuid", "@org_golang_google_genproto_googleapis_api//httpbody", "@org_golang_google_genproto_googleapis_rpc//errdetails", "@org_golang_google_genproto_googleapis_rpc//status", "@org_golang_google_grpc//:go_default_library", "@org_golang_google_grpc//codes", + "@org_golang_google_grpc//grpclog", "@org_golang_google_grpc//metadata", "@org_golang_google_grpc//status", "@org_golang_google_protobuf//proto", diff --git a/internal/descriptor/BUILD.bazel b/internal/descriptor/BUILD.bazel index ba0383f7cb8..28b6e25834b 100644 --- a/internal/descriptor/BUILD.bazel +++ b/internal/descriptor/BUILD.bazel @@ -19,9 +19,9 @@ go_library( "//internal/descriptor/openapiconfig", "//internal/httprule", "//protoc-gen-openapiv2/options", - "@com_github_golang_glog//:glog", "@in_gopkg_yaml_v3//:yaml_v3", "@org_golang_google_genproto_googleapis_api//annotations", + "@org_golang_google_grpc//grpclog", "@org_golang_google_protobuf//compiler/protogen", "@org_golang_google_protobuf//encoding/protojson", "@org_golang_google_protobuf//proto", diff --git a/internal/httprule/BUILD.bazel b/internal/httprule/BUILD.bazel index f694f3c0d03..b8fbb2b77c4 100644 --- a/internal/httprule/BUILD.bazel +++ b/internal/httprule/BUILD.bazel @@ -24,7 +24,7 @@ go_test( embed = [":httprule"], deps = [ "//utilities", - "@com_github_golang_glog//:glog", + "@org_golang_google_grpc//grpclog", ], ) From 01b4806d8291c40e00a6b727016fa79d2c0c571a Mon Sep 17 00:00:00 2001 From: Mikhail Mazurskiy Date: Sat, 14 Oct 2023 11:41:37 +1100 Subject: [PATCH 3/4] Remove mentions of --logtostderr flag --- README.md | 16 ++++------------ docs/docs/development/installation_for_cygwin.md | 4 ++-- docs/docs/mapping/customizing_openapi_output.md | 3 +-- docs/docs/mapping/grpc_api_configuration.md | 2 -- examples/internal/browser/gulpfile.js | 6 ++---- protoc-gen-grpc-gateway/BUILD.bazel | 1 - protoc-gen-openapiv2/defs.bzl | 2 -- 7 files changed, 9 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index ae4bd65bcc5..b4926522169 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,6 @@ With `protoc` (just the grpc-gateway stubs): ```sh protoc -I . --grpc-gateway_out ./gen/go \ - --grpc-gateway_opt logtostderr=true \ --grpc-gateway_opt paths=source_relative \ --grpc-gateway_opt generate_unbound_methods=true \ your/service/v1/your_service.proto @@ -300,7 +299,6 @@ Here's what a `protoc` execution might look like: ```sh protoc -I . --grpc-gateway_out ./gen/go \ - --grpc-gateway_opt logtostderr=true \ --grpc-gateway_opt paths=source_relative \ your/service/v1/your_service.proto ``` @@ -340,7 +338,6 @@ With `protoc` (just the grpc-gateway stubs): ```sh protoc -I . --grpc-gateway_out ./gen/go \ - --grpc-gateway_opt logtostderr=true \ --grpc-gateway_opt paths=source_relative \ --grpc-gateway_opt grpc_api_configuration=path/to/config.yaml \ --grpc-gateway_opt standalone=true \ @@ -438,7 +435,6 @@ With `protoc` (just the swagger file): ```sh protoc -I . --openapiv2_out ./gen/openapiv2 \ - --openapiv2_opt logtostderr=true \ your/service/v1/your_service.proto ``` @@ -531,19 +527,15 @@ through `protoc` using one of 2 patterns: - as part of the `--_out` `protoc` parameter: `--_out=:` ```sh ---grpc-gateway_out=logtostderr=true,repeated_path_param_separator=ssv:. ---openapiv2_out=logtostderr=true,repeated_path_param_separator=ssv:. +--grpc-gateway_out=repeated_path_param_separator=ssv:. +--openapiv2_out=repeated_path_param_separator=ssv:. ``` - using additional `--_opt` parameters: `--_opt=[,]*` ```sh ---grpc-gateway_opt logtostderr=true,repeated_path_param_separator=ssv -# or separately ---grpc-gateway_opt logtostderr=true --grpc-gateway_opt repeated_path_param_separator=ssv ---openapiv2_opt logtostderr=true,repeated_path_param_separator=ssv -# or separately ---openapiv2_opt logtostderr=true --openapiv2_opt repeated_path_param_separator=ssv +--grpc-gateway_opt repeated_path_param_separator=ssv +--openapiv2_opt repeated_path_param_separator=ssv ``` ## More examples diff --git a/docs/docs/development/installation_for_cygwin.md b/docs/docs/development/installation_for_cygwin.md index 2e55615342c..6419c9b70d6 100644 --- a/docs/docs/development/installation_for_cygwin.md +++ b/docs/docs/development/installation_for_cygwin.md @@ -66,8 +66,8 @@ Follow the [instructions](https://github.com/grpc-ecosystem/grpc-gateway#usage) Adjust steps 3, 5 and 7 like this. `protoc` expects native Windows paths. protoc -I. -I$(cygpath -w /usr/local/include) -I${GOPATH}/src --go_out=. --go-grpc_out=. ./path/to/your_service.proto - protoc -I. -I$(cygpath -w /usr/local/include) -I${GOPATH}/src --grpc-gateway_out=logtostderr=true:. ./path/to/your_service.proto - protoc -I. -I$(cygpath -w /usr/local/include) -I${GOPATH}/src --openapiv2_out=logtostderr=true:. ./path/to/your_service.proto + protoc -I. -I$(cygpath -w /usr/local/include) -I${GOPATH}/src ./path/to/your_service.proto + protoc -I. -I$(cygpath -w /usr/local/include) -I${GOPATH}/src ./path/to/your_service.proto Then `cd` into the directory where your entry-point `main.go` file is located and run: diff --git a/docs/docs/mapping/customizing_openapi_output.md b/docs/docs/mapping/customizing_openapi_output.md index 03e15f702f2..5e9dfc88856 100644 --- a/docs/docs/mapping/customizing_openapi_output.md +++ b/docs/docs/mapping/customizing_openapi_output.md @@ -162,9 +162,8 @@ Example of a bash script with the `use_go_templates` flag set to true: ```sh $ protoc -I. \ --go_out . --go-grpc_out . \ - --grpc-gateway_out . --grpc-gateway_opt logtostderr=true \ + --grpc-gateway_out . \ --openapiv2_out . \ - --openapiv2_opt logtostderr=true \ --openapiv2_opt use_go_templates=true \ path/to/my/proto/v1/myproto.proto ``` diff --git a/docs/docs/mapping/grpc_api_configuration.md b/docs/docs/mapping/grpc_api_configuration.md index 9f9e9eb5f0b..d253df70d20 100644 --- a/docs/docs/mapping/grpc_api_configuration.md +++ b/docs/docs/mapping/grpc_api_configuration.md @@ -94,7 +94,6 @@ It will generate a stub file with path `./gen/go/your/service/v1/your_service.pb ```sh protoc -I . \ --grpc-gateway_out ./gen/go \ - --grpc-gateway_opt logtostderr=true \ --grpc-gateway_opt paths=source_relative \ --grpc-gateway_opt grpc_api_configuration=path/to/your_service.yaml \ your/service/v1/your_service.proto @@ -107,7 +106,6 @@ It will generate a stub file with path `./gen/go/your/service/v1/your_service.pb ``` protoc -I . \ --grpc-gateway_out ./gen/go \ - --grpc-gateway_opt logtostderr=true \ --grpc-gateway_opt paths=source_relative \ --grpc-gateway_opt standalone=true \ --grpc-gateway_opt grpc_api_configuration=path/to/your_service.yaml \ diff --git a/examples/internal/browser/gulpfile.js b/examples/internal/browser/gulpfile.js index 3ead9bc6be1..db4149eed33 100644 --- a/examples/internal/browser/gulpfile.js +++ b/examples/internal/browser/gulpfile.js @@ -24,9 +24,7 @@ gulp.task('gateway', shell.task([ ])); gulp.task('serve-server', ['server'], function () { - let server = child.spawn('bin/example-server', [ - '--logtostderr', - ], { stdio: 'inherit' }); + let server = child.spawn('bin/example-server', [], { stdio: 'inherit' }); process.on('exit', function () { server.kill(); }); @@ -34,7 +32,7 @@ gulp.task('serve-server', ['server'], function () { gulp.task('serve-gateway', ['gateway', 'serve-server'], function () { let gw = child.spawn('bin/example-gw', [ - '--logtostderr', '--openapi_dir', path.join(__dirname, "../proto/examplepb"), + '--openapi_dir', path.join(__dirname, "../proto/examplepb"), ], { stdio: 'inherit' }); process.on('exit', function () { gw.kill(); diff --git a/protoc-gen-grpc-gateway/BUILD.bazel b/protoc-gen-grpc-gateway/BUILD.bazel index ea5b2b61dc6..065370350a9 100644 --- a/protoc-gen-grpc-gateway/BUILD.bazel +++ b/protoc-gen-grpc-gateway/BUILD.bazel @@ -24,7 +24,6 @@ go_binary( go_proto_compiler( name = "go_gen_grpc_gateway", - options = ["logtostderr=true"], plugin = ":protoc-gen-grpc-gateway", suffix = ".pb.gw.go", visibility = ["//visibility:public"], diff --git a/protoc-gen-openapiv2/defs.bzl b/protoc-gen-openapiv2/defs.bzl index cb569810ba2..bde4978f612 100644 --- a/protoc-gen-openapiv2/defs.bzl +++ b/protoc-gen-openapiv2/defs.bzl @@ -77,8 +77,6 @@ def _run_proto_gen_openapi( args.add("--plugin", "protoc-gen-openapiv2=%s" % protoc_gen_openapiv2.path) - args.add("--openapiv2_opt", "logtostderr=true") - extra_inputs = [] if grpc_api_configuration: extra_inputs.append(grpc_api_configuration) From 30a06a51dbba8e333abee9db43a7d636e340720e Mon Sep 17 00:00:00 2001 From: Mikhail Mazurskiy Date: Mon, 16 Oct 2023 11:46:28 +1100 Subject: [PATCH 4/4] Add compatibility with glog's logtostderr flag --- protoc-gen-grpc-gateway/main.go | 2 ++ protoc-gen-openapiv2/main.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/protoc-gen-grpc-gateway/main.go b/protoc-gen-grpc-gateway/main.go index 80e0ef6ef5a..5a1e6977e2a 100644 --- a/protoc-gen-grpc-gateway/main.go +++ b/protoc-gen-grpc-gateway/main.go @@ -35,6 +35,8 @@ var ( versionFlag = flag.Bool("version", false, "print the current version") warnOnUnboundMethods = flag.Bool("warn_on_unbound_methods", false, "emit a warning message if an RPC method has no HttpRule annotation") generateUnboundMethods = flag.Bool("generate_unbound_methods", false, "generate proxy methods even for RPC methods that have no HttpRule annotation") + + _ = flag.Bool("logtostderr", false, "Legacy glog compatibility. This flag is a no-op, you can safely remove it") ) // Variables set by goreleaser at build time diff --git a/protoc-gen-openapiv2/main.go b/protoc-gen-openapiv2/main.go index 6f3ee3a24fe..5270cae23fa 100644 --- a/protoc-gen-openapiv2/main.go +++ b/protoc-gen-openapiv2/main.go @@ -47,6 +47,8 @@ var ( useAllOfForRefs = flag.Bool("use_allof_for_refs", false, "if set, will use allOf as container for $ref to preserve same-level properties.") allowPatchFeature = flag.Bool("allow_patch_feature", true, "whether to hide update_mask fields in PATCH requests from the generated swagger file.") preserveRPCOrder = flag.Bool("preserve_rpc_order", false, "if true, will ensure the order of paths emitted in openapi swagger files mirror the order of RPC methods found in proto files. If false, emitted paths will be ordered alphabetically.") + + _ = flag.Bool("logtostderr", false, "Legacy glog compatibility. This flag is a no-op, you can safely remove it") ) // Variables set by goreleaser at build time