Skip to content

Commit

Permalink
feat: enable grpc plugin logging (#413)
Browse files Browse the repository at this point in the history
* Enable grpc plugin logging

Signed-off-by: Vladimir Lavor <[email protected]>

* updated example, readme and deps

Signed-off-by: Vladimir Lavor <[email protected]>
  • Loading branch information
VladoLavor authored and ondrej-fabry committed Oct 30, 2019
1 parent 5099d74 commit 42c7431
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ examples/etcd-lib/election/election
examples/etcd-lib/view/view
examples/etcd-lib/watcher/watcher
examples/flags-lib/flags-lib
examples/grpc-plugin/grpc-client/grpc-client
examples/grpc-plugin/grpc-server/grpc-server
examples/kafka-lib/asyncproducer/asyncproducer
examples/kafka-lib/consumer/consumer
Expand Down
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions examples/grpc-plugin/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# grpc-plugin

To run the `grpc-server` example, simply type in grpc-server folder:
Start the `grpc-server` example typing following in the grpc-server folder:
```
go run main.go deps.go [--grpc-config=<config-filepath>]
go run main.go
```

To run the `grpc-client` example, simply type in grpc-client folder:
In order to pass custom configuration file (for example the `grpc.conf` located in the same directory), start grpc-server with following parameter:
```
go run main.go deps.go
go run main --grpc-config=<config-filepath>
```
Note: `main.go` must be edited in this case because it makes use of the direct config injection via `grpc.UseConf` method, which overrides any provided config file. To enable it, remove method mentioned.

Start the `grpc-client` exampl typing following in the grpc-client folder:
```
go run main.go
```

3 changes: 2 additions & 1 deletion examples/grpc-plugin/grpc-server/grpc.conf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Endpoint: "0.0.0.0:9111"
endpoint: "0.0.0.0:9111"
extended-logging: true
2 changes: 2 additions & 0 deletions examples/grpc-plugin/grpc-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ const PluginName = "myPlugin"
func main() {
grpcPlug := grpc.NewPlugin(
grpc.UseHTTP(&rest.DefaultPlugin),
// Remove 'UseConf' in order to allow GRPC config file
grpc.UseConf(grpc.Config{
Endpoint: "localhost:9111",
ExtendedLogging: true,
}),
grpc.UseAuth(&grpc.Authenticator{
Username: "testuser",
Expand Down
3 changes: 3 additions & 0 deletions rpc/grpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type Config struct {
Keyfile string `json:"key-file"`
CAfiles []string `json:"ca-files"`

// ExtendedLogging enables detailed GRPC logging
ExtendedLogging bool `json:"extended-logging"`

// Compression for inbound/outbound messages.
// Supported only gzip.
//TODO Compression string
Expand Down
5 changes: 4 additions & 1 deletion rpc/grpc/grpc.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ max-concurrent-streams: 0

# Set custom CA to verify client's certificate.
# If not set, client's certificate is not required.
#ca-files:
#ca-files:
# - /path/to/ca1.pem
# - /path/to/ca2.pem

# Enables logging additional GRPC transport messages
extended-logging: false
13 changes: 12 additions & 1 deletion rpc/grpc/plugin_impl_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"net/http"
"strconv"

"google.golang.org/grpc/grpclog"

grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/ligato/cn-infra/infra"
"github.com/ligato/cn-infra/rpc/rest"
Expand All @@ -28,6 +30,10 @@ import (
"google.golang.org/grpc/credentials"
)

// set according to google.golang.org/grpc/internal/transport/log.go
// transport package only logs to verbose level 2 by default
const logLevel = 2

// Plugin maintains the GRPC netListener (see Init, AfterInit, Close methods)
type Plugin struct {
Deps
Expand Down Expand Up @@ -85,9 +91,14 @@ func (p *Plugin) Init() (err error) {
opts = append(opts, grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(p.auther.Authenticate)))
}
p.grpcServer = grpc.NewServer(opts...)
}

//grpclog.SetLogger(p.Log.NewLogger("grpc-server"))
grpcLogger := p.Log.NewLogger("grpc-server")
if p.Config != nil && p.Config.ExtendedLogging {
p.Log.Info("GRPC transport logging enabled")
grpcLogger.SetVerbosity(logLevel)
}
grpclog.SetLoggerV2(grpcLogger)

return nil
}
Expand Down

0 comments on commit 42c7431

Please sign in to comment.