Skip to content

Commit

Permalink
log grpc requests in debug mode
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
butonic committed Oct 29, 2024
1 parent 6f27b4d commit ae3ca01
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/log-grpc-requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Log GRPC requests in debug mode

When log level is set to debug we will now also log grpc requests.

https://github.com/owncloud/ocis/pull/10438
40 changes: 36 additions & 4 deletions ocis-pkg/service/grpc/service.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package grpc

import (
"context"
"crypto/tls"
"fmt"
"strings"
"time"

mgrpcs "github.com/go-micro/plugins/v4/server/grpc"
"github.com/go-micro/plugins/v4/wrapper/monitoring/prometheus"
mtracer "github.com/go-micro/plugins/v4/wrapper/trace/opentelemetry"
ociscrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/rs/zerolog"
"go-micro.dev/v4"
"go-micro.dev/v4/client"
"go-micro.dev/v4/server"
Expand Down Expand Up @@ -54,6 +58,16 @@ func NewServiceWithClient(client client.Client, opts ...Option) (Service, error)
mServer = mgrpcs.NewServer(mgrpcs.Options(keepaliveParams))
}

handlerWrappers := []server.HandlerWrapper{
mtracer.NewHandlerWrapper(
mtracer.WithTraceProvider(sopts.TraceProvider),
),
}
if sopts.Logger.GetLevel() == zerolog.DebugLevel {
handlerWrappers = append(handlerWrappers, LogHandler(&sopts.Logger))
}
handlerWrappers = append(handlerWrappers, sopts.HandlerWrappers...)

mopts := []micro.Option{
// first add a server because it will reset any options
micro.Server(mServer),
Expand All @@ -70,14 +84,32 @@ func NewServiceWithClient(client client.Client, opts ...Option) (Service, error)
micro.WrapClient(mtracer.NewClientWrapper(
mtracer.WithTraceProvider(sopts.TraceProvider),
)),
micro.WrapHandler(mtracer.NewHandlerWrapper(
mtracer.WithTraceProvider(sopts.TraceProvider),
)),
micro.WrapHandler(sopts.HandlerWrappers...),
micro.WrapHandler(handlerWrappers...),
micro.WrapSubscriber(mtracer.NewSubscriberWrapper(
mtracer.WithTraceProvider(sopts.TraceProvider),
)),
}

return Service{micro.NewService(mopts...)}, nil
}

// If used with tracing, please ensure this is registerd (by micro.WrapHandler()) after
// micro-plugin's opentracing wrapper: `opentracing.NewHandlerWrapper()`
func LogHandler(l *log.Logger) func(fn server.HandlerFunc) server.HandlerFunc {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
now := time.Now()
defer func() {
l.Debug().
Str("method", req.Method()).
Str("endpoint", req.Endpoint()).
Str("content-type", req.ContentType()).
Str("service", req.Service()).
Interface("headers", req.Header()).
Dur("duration", time.Since(now)).
Msg("grpc call")
}()
return fn(ctx, req, rsp)
}
}
}

0 comments on commit ae3ca01

Please sign in to comment.