Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log grpc requests in debug mode #10438

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 39 additions & 4 deletions ocis-pkg/service/grpc/service.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
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"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
Expand Down Expand Up @@ -54,6 +59,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 +85,34 @@ 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()
spanContext := trace.SpanContextFromContext(ctx)
defer func() {
l.Debug().
Str("traceid", spanContext.TraceID().String()).
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)
}
}
}