/
middleware.go
194 lines (165 loc) · 6.36 KB
/
middleware.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package observability
import (
"context"
"sync"
"go.uber.org/net/metrics"
"go.uber.org/yarpc/api/transport"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var _writerPool = sync.Pool{New: func() interface{} {
return &writer{}
}}
// writer wraps a transport.ResponseWriter so the observing middleware can
// detect application errors.
type writer struct {
transport.ResponseWriter
isApplicationError bool
}
func newWriter(rw transport.ResponseWriter) *writer {
w := _writerPool.Get().(*writer)
w.isApplicationError = false
w.ResponseWriter = rw
return w
}
func (w *writer) SetApplicationError() {
w.isApplicationError = true
w.ResponseWriter.SetApplicationError()
}
func (w *writer) free() {
_writerPool.Put(w)
}
// Middleware is logging and metrics middleware for all RPC types.
type Middleware struct {
graph graph
}
// Config configures the observability middleware.
type Config struct {
// Logger to which messages will be logged.
Logger *zap.Logger
// Scope to which metrics are emitted.
Scope *metrics.Scope
// Extracts request-scoped information from the context for logging.
ContextExtractor ContextExtractor
// Levels specify log levels for various classes of requests.
Levels LevelsConfig
}
// LevelsConfig specifies log level overrides for inbound traffic, outbound
// traffic, or the defaults for either.
type LevelsConfig struct {
Default DirectionalLevelsConfig
Inbound DirectionalLevelsConfig
Outbound DirectionalLevelsConfig
}
// DirectionalLevelsConfig may override the log levels for any combination of
// successes, failures, and application errors.
type DirectionalLevelsConfig struct {
// Log level used to log successful calls.
//
// Defaults to DebugLevel.
Success *zapcore.Level
// Log level used to log failed calls.
// This includes low-level network errors, TChannel error frames, etc.
//
// Defaults to ErrorLevel.
Failure *zapcore.Level
// Log level used to log calls that failed with an application error.
// All Thrift exceptions are considered application errors.
//
// Defaults to ErrorLevel.
ApplicationError *zapcore.Level
}
// NewMiddleware constructs an observability middleware with the provided
// configuration.
func NewMiddleware(cfg Config) *Middleware {
m := &Middleware{newGraph(cfg.Scope, cfg.Logger, cfg.ContextExtractor)}
// Apply the default levels
applyLogLevelsConfig(&m.graph.inboundLevels, &cfg.Levels.Default)
applyLogLevelsConfig(&m.graph.outboundLevels, &cfg.Levels.Default)
// Override with direction-specific levels
applyLogLevelsConfig(&m.graph.inboundLevels, &cfg.Levels.Inbound)
applyLogLevelsConfig(&m.graph.outboundLevels, &cfg.Levels.Outbound)
return m
}
func applyLogLevelsConfig(dst *levels, src *DirectionalLevelsConfig) {
if level := src.Success; level != nil {
dst.success = *src.Success
}
if level := src.Failure; level != nil {
dst.failure = *src.Failure
}
if level := src.ApplicationError; level != nil {
dst.applicationError = *src.ApplicationError
}
}
// Handle implements middleware.UnaryInbound.
func (m *Middleware) Handle(ctx context.Context, req *transport.Request, w transport.ResponseWriter, h transport.UnaryHandler) error {
call := m.graph.begin(ctx, transport.Unary, _directionInbound, req)
wrappedWriter := newWriter(w)
err := h.Handle(ctx, req, wrappedWriter)
call.EndWithAppError(err, wrappedWriter.isApplicationError)
wrappedWriter.free()
return err
}
// Call implements middleware.UnaryOutbound.
func (m *Middleware) Call(ctx context.Context, req *transport.Request, out transport.UnaryOutbound) (*transport.Response, error) {
call := m.graph.begin(ctx, transport.Unary, _directionOutbound, req)
res, err := out.Call(ctx, req)
isApplicationError := false
if res != nil {
isApplicationError = res.ApplicationError
}
call.EndWithAppError(err, isApplicationError)
return res, err
}
// HandleOneway implements middleware.OnewayInbound.
func (m *Middleware) HandleOneway(ctx context.Context, req *transport.Request, h transport.OnewayHandler) error {
call := m.graph.begin(ctx, transport.Oneway, _directionInbound, req)
err := h.HandleOneway(ctx, req)
call.End(err)
return err
}
// CallOneway implements middleware.OnewayOutbound.
func (m *Middleware) CallOneway(ctx context.Context, req *transport.Request, out transport.OnewayOutbound) (transport.Ack, error) {
call := m.graph.begin(ctx, transport.Oneway, _directionOutbound, req)
ack, err := out.CallOneway(ctx, req)
call.End(err)
return ack, err
}
// HandleStream implements middleware.StreamInbound.
func (m *Middleware) HandleStream(serverStream *transport.ServerStream, h transport.StreamHandler) error {
call := m.graph.begin(serverStream.Context(), transport.Streaming, _directionInbound, serverStream.Request().Meta.ToRequest())
call.EndStreamHandshake()
err := h.HandleStream(call.WrapServerStream(serverStream))
call.EndStream(err)
return err
}
// CallStream implements middleware.StreamOutbound.
func (m *Middleware) CallStream(ctx context.Context, request *transport.StreamRequest, out transport.StreamOutbound) (*transport.ClientStream, error) {
call := m.graph.begin(ctx, transport.Streaming, _directionOutbound, request.Meta.ToRequest())
clientStream, err := out.CallStream(ctx, request)
call.EndStreamHandshakeWithError(err)
if err != nil {
return nil, err
}
return call.WrapClientStream(clientStream), nil
}