This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
interceptors.go
132 lines (111 loc) · 4.19 KB
/
interceptors.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
124
125
126
127
128
129
130
131
132
// qan-api2
// Copyright (C) 2019 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 interceptors contains gRPC wrappers for logging and Prometheus metrics.
package interceptors
import (
"context"
"runtime/debug"
"runtime/pprof"
"time"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/percona/qan-api2/utils/logger"
)
const responseTimeout = 2 * time.Minute
func logRequest(l *logrus.Entry, prefix string, 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, even before re-panic - there can be inner stacktraces
// produced by panic(errors.WithStack(err)).
// Also always log debug.Stack() for all panics.
l.Errorf("%s done in %s with panic: %+v\nStack: %s", prefix, dur, p, debug.Stack())
if l.Logger.GetLevel() == logrus.TraceLevel {
panic(p)
}
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 dur < time.Second {
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
}
// Unary adds context logger and Prometheus metrics to unary server RPC.
func Unary(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx, cancel := context.WithTimeout(ctx, responseTimeout)
defer cancel()
// add pprof labels for more useful profiles
defer pprof.SetGoroutineLabels(ctx)
ctx = pprof.WithLabels(ctx, pprof.Labels("method", info.FullMethod))
pprof.SetGoroutineLabels(ctx)
// set logger
l := logrus.WithField("request", logger.MakeRequestID())
ctx = logger.SetEntry(ctx, l)
var res interface{}
err := logRequest(l, "RPC "+info.FullMethod, func() error {
var origErr error
res, origErr = grpc_prometheus.UnaryServerInterceptor(ctx, req, info, handler)
l.Debugf("\nRequest:\n%s\nResponse:\n%s\n", req, res)
return origErr
})
return res, err
}
// Stream adds context logger and Prometheus metrics to stream server RPC.
func Stream(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)
// set logger
l := logrus.WithField("request", logger.MakeRequestID())
ctx = logger.SetEntry(ctx, l)
err := logRequest(l, "Stream "+info.FullMethod, func() error {
wrapped := grpc_middleware.WrapServerStream(ss)
wrapped.WrappedContext = ctx
return grpc_prometheus.StreamServerInterceptor(srv, wrapped, info, handler)
})
return err
}
// check interfaces
var (
_ grpc.UnaryServerInterceptor = Unary
_ grpc.StreamServerInterceptor = Stream
)