-
Notifications
You must be signed in to change notification settings - Fork 9
/
logging.go
133 lines (109 loc) · 3.79 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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package logging
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"namespacelabs.dev/foundation/std/grpc/requestid"
)
const maxOutputToTerminal = 1024
var Log = log.New(os.Stderr, "[grpclog] ", log.Ldate|log.Ltime|log.Lmicroseconds)
type interceptor struct{}
func (interceptor) unary(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
rdata, has := requestid.RequestDataFromContext(ctx)
if !has {
return handler(ctx, req)
}
logHeader(ctx, rdata.RequestID, "request", info.FullMethod, req)
resp, err := handler(ctx, req)
if err == nil {
Log.Printf("%s: id=%s: took %v; response: %s", info.FullMethod, rdata.RequestID, time.Since(rdata.Started), serializeMessage(resp))
} else {
Log.Printf("%s: id=%s: took %v; error: %v", info.FullMethod, rdata.RequestID, time.Since(rdata.Started), err)
}
return resp, err
}
func (interceptor) streaming(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
rdata, has := requestid.RequestDataFromContext(stream.Context())
if !has {
return handler(srv, stream)
}
logHeader(stream.Context(), rdata.RequestID, "stream", info.FullMethod, nil)
err := handler(srv, stream)
if err == nil {
Log.Printf("%s: id=%s: took %v, finished ok", info.FullMethod, rdata.RequestID, time.Since(rdata.Started))
} else {
Log.Printf("%s: id=%s: took %v; error: %v", info.FullMethod, rdata.RequestID, time.Since(rdata.Started), err)
}
return err
}
func logHeader(ctx context.Context, reqid, what, fullMethod string, req interface{}) {
peerAddr := "unknown"
authType := "none"
deadline := "none"
if p, has := peer.FromContext(ctx); has {
peerAddr = p.Addr.String()
if p.AuthInfo != nil {
authType = p.AuthInfo.AuthType()
}
}
md, _ := metadata.FromIncomingContext(ctx)
delete(md, "accept-encoding")
delete(md, "content-type")
authority := single(md, ":authority")
delete(md, ":authority")
if t, ok := ctx.Deadline(); ok {
left := time.Until(t)
deadline = fmt.Sprintf("%v", left)
}
if realIp := single(md, "x-real-ip"); realIp != "" {
peerAddr = fmt.Sprintf("%s (saw %s)", realIp, peerAddr)
// XXX use conditional printing instead.
delete(md, "x-real-ip")
delete(md, "x-forwarded-for")
delete(md, "x-forwarded-host")
delete(md, "x-forwarded-port")
delete(md, "x-forwarded-proto")
delete(md, "x-forwarded-scheme")
delete(md, "x-scheme")
}
if _, ok := md["authorization"]; ok {
authType = fmt.Sprintf("bearer (was %s)", authType)
delete(md, "authorization")
}
if req != nil {
Log.Printf("%s: id=%s: request from %s to %s (auth: %s, deadline: %s, metadata: %+v): %s", fullMethod, reqid, peerAddr, authority, authType, deadline, md, serializeMessage(req))
} else {
Log.Printf("%s: id=%s: request from %s to %s (auth: %s, deadline: %s, metadata: %+v)", fullMethod, reqid, peerAddr, authority, authType, deadline, md)
}
}
func single(md metadata.MD, key string) string {
if value, ok := md[key]; ok && len(value) == 1 {
return value[0]
}
return ""
}
func Prepare(ctx context.Context, deps ExtensionDeps) error {
var interceptor interceptor
deps.Interceptors.ForServer(interceptor.unary, interceptor.streaming)
return nil
}
func serializeMessage(msg interface{}) string {
if msg == nil {
return "<nil>"
}
reqBytes, _ := json.Marshal(msg)
reqStr := string(reqBytes)
if len(reqStr) > maxOutputToTerminal {
return fmt.Sprintf("%s [...%d chars truncated]", reqStr[:maxOutputToTerminal], len(reqStr)-maxOutputToTerminal)
}
return reqStr
}