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

Add debug logging interceptor for RPC logs #3496

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions private/buf/bufcli/connectclient_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func newConnectClientConfigWithOptions(container appext.Container, opts ...conne
bufconnect.NewAugmentedConnectErrorInterceptor(),
bufconnect.NewSetCLIVersionInterceptor(Version),
bufconnect.NewCLIWarningInterceptor(container),
bufconnect.NewDebugLoggingInterceptor(container),
otelconnectInterceptor,
},
),
Expand Down
56 changes: 56 additions & 0 deletions private/bufpkg/bufconnect/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"strings"
"time"

"connectrpc.com/connect"
"github.com/bufbuild/buf/private/pkg/app/appext"
"google.golang.org/protobuf/proto"
)

const (
Expand Down Expand Up @@ -140,3 +144,55 @@ func NewAuthorizationInterceptorProvider(tokenProviders ...TokenProvider) func(s
return interceptor
}
}

// NewDebugLoggingInterceptor returns a new Connect Interceptor that adds debug log
// statements for each rpc call.
//
// The following information is collected for logging: duration, status code, peer name,
// rpc system, request size, and response size.
func NewDebugLoggingInterceptor(container appext.LoggerContainer) connect.UnaryInterceptorFunc {
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
var requestSize int
if req.Any() != nil {
msg, ok := req.Any().(proto.Message)
if ok {
requestSize = proto.Size(msg)
}
}
startTime := time.Now()
resp, err := next(ctx, req)
duration := time.Since(startTime)
var status connect.Code
if err != nil {
if connectErr := new(connect.Error); errors.As(err, &connectErr) {
status = connectErr.Code()
}
}
doriable marked this conversation as resolved.
Show resolved Hide resolved
var responseSize int
if resp != nil && resp.Any() != nil {
msg, ok := resp.Any().(proto.Message)
if ok {
responseSize = proto.Size(msg)
}
}
attrs := []slog.Attr{
slog.Duration("duration", duration),
slog.String("status", status.String()),
slog.String("net.peer.name", req.Peer().Addr),
slog.String("rpc.system", req.Peer().Protocol),
slog.Int("message.sent.uncompressed_size", requestSize),
slog.Int("message.received.uncompressed_size", responseSize),
}
container.Logger().LogAttrs(
ctx,
slog.LevelDebug,
// Remove the leading "/" from Procedure name
strings.TrimPrefix(req.Spec().Procedure, "/"),
attrs...,
)
return resp, err
}
}
return interceptor
}