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

extract logging middleware #9

Merged
merged 12 commits into from
May 17, 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
Prev Previous commit
Next Next commit
unify tracing and logging
  • Loading branch information
gimmyxd committed Apr 26, 2024
commit eebeeef8df21483a1733972d9abfc03ca06aedf9
63 changes: 0 additions & 63 deletions grpcutil/middlewares/logging/logging_middleware.go

This file was deleted.

46 changes: 30 additions & 16 deletions grpcutil/middlewares/tracing/tracing_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,44 @@ import (
)

type TracingMiddleware struct {
logger *zerolog.Logger
}

func NewTracingMiddleware() *TracingMiddleware {
return &TracingMiddleware{}
type TracingHook struct{}

type TracingFields struct{}

func (h TracingHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
ctx := e.GetCtx()

e.Fields(header.KnownContextValueStrings(ctx))

serviceMethod, ok := grpc.Method(ctx)
if ok {
e.Str("service", serviceMethod)
}

tracingFields := ctx.Value(TracingFields{})
e.Fields(tracingFields)
}

func NewTracingMiddleware(logger *zerolog.Logger) *TracingMiddleware {
return &TracingMiddleware{
logger: logger,
}
}

var _ grpcutil.Middleware = &TracingMiddleware{}

func (m *TracingMiddleware) Unary() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
apiLogger := zerolog.Ctx(ctx).With().
Fields(header.KnownContextValueStrings(ctx)).
Logger()
apiLogger := m.logger.Hook(TracingHook{})
apiLoggerCtx := apiLogger.WithContext(ctx)

apiLogger.Trace().Interface("request", req).Msg("grpc call start")

newCtx := apiLogger.WithContext(ctx)

start := time.Now()
result, err := handler(newCtx, req)
result, err := handler(apiLoggerCtx, req)
apiLogger.Trace().Dur("duration-ms", time.Since(start)).Msg("grpc call end")

return result, err
Expand All @@ -41,17 +59,13 @@ func (m *TracingMiddleware) Unary() grpc.UnaryServerInterceptor {
func (m *TracingMiddleware) Stream() grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := stream.Context()
apiLogger := m.logger.Hook(TracingHook{})
apiLoggerCtx := apiLogger.WithContext(ctx)

apiLogger := zerolog.Ctx(ctx)

apiLogger.Trace().
Fields(header.KnownContextValueStrings(ctx)).
Msg("grpc stream call")

newCtx := apiLogger.WithContext(ctx)
apiLogger.Trace().Msg("grpc stream call")

wrapped := grpcmiddleware.WrapServerStream(stream)
wrapped.WrappedContext = newCtx
wrapped.WrappedContext = apiLoggerCtx

return handler(srv, wrapped)
}
Expand Down
Loading