This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_interceptor.go
123 lines (103 loc) · 4.04 KB
/
logging_interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// dbaas-controller
// Copyright (C) 2020 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package servers
import (
"context"
"runtime/debug"
"runtime/pprof"
"time"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/percona-platform/dbaas-controller/utils/logger"
)
// logGRPCRequest wraps f (gRPC handler) invocation with logging and panic recovery.
func logGRPCRequest(l logger.Logger, prefix string, warnD time.Duration, f func() error) (err error) {
start := time.Now()
l.Infof("Starting %s ...", prefix)
defer func() {
dur := time.Since(start)
if p := recover(); p != nil {
// Always log with %+v - there can be inner stacktraces
// produced by panic(errors.WithStack(err)).
// Also always log debug.Stack() for all panics.
l.Debugf("%s done in %s with panic: %+v\nStack: %s", prefix, dur, p, debug.Stack())
err = status.Error(codes.Internal, "Internal server error.")
return
}
// log gRPC errors as warning, not errors, even if they are wrapped
_, gRPCError := status.FromError(errors.Cause(err))
switch {
case err == nil:
if warnD == 0 || dur < warnD {
l.Infof("%s done in %s.", prefix, dur)
} else {
l.Warnf("%s done in %s (quite long).", prefix, dur)
}
case gRPCError:
// %+v for inner stacktraces produced by errors.WithStack(err)
l.Warnf("%s done in %s with gRPC error: %+v", prefix, dur, err)
default:
// %+v for inner stacktraces produced by errors.WithStack(err)
l.Errorf("%s done in %s with unexpected error: %+v", prefix, dur, err)
err = status.Error(codes.Internal, "Internal server error.")
}
}()
err = f()
return //nolint:nakedret
}
// unaryLoggingInterceptor returns a new unary server interceptor that logs incoming requests.
func unaryLoggingInterceptor(warnDuration time.Duration) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { //nolint:lll
// add pprof labels for more useful profiles
defer pprof.SetGoroutineLabels(ctx)
ctx = pprof.WithLabels(ctx, pprof.Labels("method", info.FullMethod))
pprof.SetGoroutineLabels(ctx)
// make context with logger
var l logger.Logger
ctx, l = getCtxForRequest(ctx)
var res interface{}
err := logGRPCRequest(l, "RPC "+info.FullMethod, warnDuration, func() error {
var origErr error
res, origErr = handler(ctx, req)
return origErr
})
// err is already logged by logRequest
l.Debugf("\nRequest:\n%s\nResponse:\n%s\n", req, res)
return res, err
}
}
// streamLoggingInterceptor returns a new stream server interceptor that logs incoming messages.
func streamLoggingInterceptor(warnDuration time.Duration) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := ss.Context()
// add pprof labels for more useful profiles
defer pprof.SetGoroutineLabels(ctx)
ctx = pprof.WithLabels(ctx, pprof.Labels("method", info.FullMethod))
pprof.SetGoroutineLabels(ctx)
// make context with logger
var l logger.Logger
ctx, l = getCtxForRequest(ctx)
err := logGRPCRequest(l, "Stream "+info.FullMethod, warnDuration, func() error {
wrapped := grpc_middleware.WrapServerStream(ss)
wrapped.WrappedContext = ctx
return handler(srv, ss)
})
return err
}
}