forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor.go
36 lines (31 loc) · 1.29 KB
/
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
package auth
import (
"context"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
// LogrusUnaryServerInterceptor returns grpc.UnaryServerInterceptor which populates request-scoped logrus logger with account_id field
func LogrusUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
addAccountIDToLogger(ctx)
return handler(ctx, req)
}
}
// LogrusStreamServerInterceptor returns grpc.StreamServerInterceptor which populates request-scoped logrus logger with account_id field
func LogrusStreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
ctx := stream.Context()
addAccountIDToLogger(ctx)
wrapped := grpc_middleware.WrapServerStream(stream)
wrapped.WrappedContext = ctx
err = handler(srv, wrapped)
return
}
}
func addAccountIDToLogger(ctx context.Context) {
if accountID, err := GetAccountID(ctx, nil); err == nil {
ctxlogrus.AddFields(ctx, logrus.Fields{MultiTenancyField: accountID})
}
}