-
Notifications
You must be signed in to change notification settings - Fork 168
/
logging.go
151 lines (125 loc) · 4.37 KB
/
logging.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Package logging contains logging middleware
package logging
import (
"context"
"encoding/json"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"github.com/openfga/openfga/pkg/logger"
"github.com/openfga/openfga/pkg/middleware/requestid"
serverErrors "github.com/openfga/openfga/pkg/server/errors"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
)
const (
grpcServiceKey = "grpc_service"
grpcMethodKey = "grpc_method"
grpcTypeKey = "grpc_type"
grpcCodeKey = "grpc_code"
requestIDKey = "request_id"
traceIDKey = "trace_id"
rawRequestKey = "raw_request"
rawResponseKey = "raw_response"
internalErrorKey = "internal_error"
grpcReqCompleteKey = "grpc_req_complete"
userAgentKey = "user_agent"
gatewayUserAgentHeader string = "grpcgateway-user-agent"
userAgentHeader string = "user-agent"
)
func NewLoggingInterceptor(logger logger.Logger) grpc.UnaryServerInterceptor {
return interceptors.UnaryServerInterceptor(reportable(logger))
}
func NewStreamingLoggingInterceptor(logger logger.Logger) grpc.StreamServerInterceptor {
return interceptors.StreamServerInterceptor(reportable(logger))
}
type reporter struct {
ctx context.Context
logger logger.Logger
fields []zap.Field
protomarshaler protojson.MarshalOptions
}
func (r *reporter) PostCall(err error, _ time.Duration) {
r.fields = append(r.fields, ctxzap.TagsToFields(r.ctx)...)
code := serverErrors.ConvertToEncodedErrorCode(status.Convert(err))
r.fields = append(r.fields, zap.Int32(grpcCodeKey, code))
if err != nil {
if internalError, ok := err.(serverErrors.InternalError); ok {
r.fields = append(r.fields, zap.String(internalErrorKey, internalError.Internal().Error()))
}
if isInternalError(code) {
r.logger.Error(err.Error(), r.fields...)
} else {
r.fields = append(r.fields, zap.Error(err))
r.logger.Info(grpcReqCompleteKey, r.fields...)
}
return
}
r.logger.Info(grpcReqCompleteKey, r.fields...)
}
func (r *reporter) PostMsgSend(msg interface{}, err error, _ time.Duration) {
protomsg, ok := msg.(protoreflect.ProtoMessage)
if ok {
if resp, err := r.protomarshaler.Marshal(protomsg); err == nil {
r.fields = append(r.fields, zap.Any(rawResponseKey, json.RawMessage(resp)))
}
}
}
func (r *reporter) PostMsgReceive(msg interface{}, _ error, _ time.Duration) {
protomsg, ok := msg.(protoreflect.ProtoMessage)
if ok {
if req, err := r.protomarshaler.Marshal(protomsg); err == nil {
r.fields = append(r.fields, zap.Any(rawRequestKey, json.RawMessage(req)))
}
}
}
// userAgentFromContext returns the user agent field stored in context.
// If context does not have user agent field, function will return empty string and false.
func userAgentFromContext(ctx context.Context) (string, bool) {
if headers, ok := metadata.FromIncomingContext(ctx); ok {
if header := headers.Get(gatewayUserAgentHeader); len(header) > 0 {
return header[0], true
}
if header := headers.Get(userAgentHeader); len(header) > 0 {
return header[0], true
}
}
return "", false
}
func reportable(l logger.Logger) interceptors.CommonReportableFunc {
return func(ctx context.Context, c interceptors.CallMeta) (interceptors.Reporter, context.Context) {
fields := []zap.Field{
zap.String(grpcServiceKey, c.Service),
zap.String(grpcMethodKey, c.Method),
zap.String(grpcTypeKey, string(c.Typ)),
}
spanCtx := trace.SpanContextFromContext(ctx)
if spanCtx.HasTraceID() {
fields = append(fields, zap.String(traceIDKey, spanCtx.TraceID().String()))
}
if requestID, ok := requestid.FromContext(ctx); ok {
fields = append(fields, zap.String(requestIDKey, requestID))
}
if userAgent, ok := userAgentFromContext(ctx); ok {
fields = append(fields, zap.String(userAgentKey, userAgent))
}
zapLogger := l.(*logger.ZapLogger)
return &reporter{
ctx: ctxzap.ToContext(ctx, zapLogger.Logger),
logger: l,
fields: fields,
protomarshaler: protojson.MarshalOptions{EmitUnpopulated: true},
}, ctx
}
}
func isInternalError(code int32) bool {
if code >= 4000 && code < 5000 {
return true
}
return false
}