-
Notifications
You must be signed in to change notification settings - Fork 0
/
debuglog.go
263 lines (231 loc) · 7.71 KB
/
debuglog.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"net"
"net/http"
"net/url"
"os"
"strconv"
"syscall"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/juju/apiserver/httpcontext"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/apiserver/websocket"
"github.com/juju/juju/state"
)
// debugLogHandler takes requests to watch the debug log.
//
// It provides the underlying framework for the 2 debug-log
// variants. The supplied handle func allows for varied handling of
// requests.
type debugLogHandler struct {
ctxt httpContext
authenticator httpcontext.Authenticator
authorizer httpcontext.Authorizer
handle debugLogHandlerFunc
}
type debugLogHandlerFunc func(
clock.Clock,
time.Duration,
state.LogTailerState,
debugLogParams,
debugLogSocket,
<-chan struct{},
) error
func newDebugLogHandler(
ctxt httpContext,
authenticator httpcontext.Authenticator,
authorizer httpcontext.Authorizer,
handle debugLogHandlerFunc,
) *debugLogHandler {
return &debugLogHandler{
ctxt: ctxt,
authenticator: authenticator,
authorizer: authorizer,
handle: handle,
}
}
// ServeHTTP will serve up connections as a websocket for the
// debug-log API.
//
// The authentication and authorization have to be done after the http request
// has been upgraded to a websocket as we may be sending back a discharge
// required error. This error contains the macaroon that needs to be
// discharged by the user. In order for this error to be deserialized
// correctly any auth failure will come back in the initial error that is
// returned over the websocket. This is consumed by the ConnectStream function
// on the apiclient.
//
// Args for the HTTP request are as follows:
// includeEntity -> []string - lists entity tags to include in the response
// - tags may finish with a '*' to match a prefix e.g.: unit-mysql-*, machine-2
// - if none are set, then all lines are considered included
// includeModule -> []string - lists logging modules to include in the response
// - if none are set, then all lines are considered included
// excludeEntity -> []string - lists entity tags to exclude from the response
// - as with include, it may finish with a '*'
// excludeModule -> []string - lists logging modules to exclude from the response
// limit -> uint - show *at most* this many lines
// backlog -> uint
// - go back this many lines from the end before starting to filter
// - has no meaning if 'replay' is true
// level -> string one of [TRACE, DEBUG, INFO, WARNING, ERROR]
// replay -> string - one of [true, false], if true, start the file from the start
// noTail -> string - one of [true, false], if true, existing logs are sent back,
// - but the command does not wait for new ones.
func (h *debugLogHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
handler := func(conn *websocket.Conn) {
socket := &debugLogSocketImpl{conn}
defer conn.Close()
// Authentication and authorization has to be done after the http
// connection has been upgraded to a websocket.
authInfo, err := h.authenticator.Authenticate(req)
if err != nil {
socket.sendError(errors.Annotate(err, "authentication failed"))
return
}
if err := h.authorizer.Authorize(authInfo); err != nil {
socket.sendError(errors.Annotate(err, "authorization failed"))
return
}
st, err := h.ctxt.stateForRequestUnauthenticated(req)
if err != nil {
socket.sendError(err)
return
}
defer st.Release()
params, err := readDebugLogParams(req.URL.Query())
if err != nil {
socket.sendError(err)
return
}
clock := h.ctxt.srv.clock
maxDuration := h.ctxt.srv.shared.maxDebugLogDuration()
if err := h.handle(clock, maxDuration, st, params, socket, h.ctxt.stop()); err != nil {
if isBrokenPipe(err) {
logger.Tracef("debug-log handler stopped (client disconnected)")
} else {
logger.Errorf("debug-log handler error: %v", err)
}
}
}
websocket.Serve(w, req, handler)
}
func isBrokenPipe(err error) bool {
err = errors.Cause(err)
if opErr, ok := err.(*net.OpError); ok {
if sysCallErr, ok := opErr.Err.(*os.SyscallError); ok {
return sysCallErr.Err == syscall.EPIPE
}
return opErr.Err == syscall.EPIPE
}
return false
}
// debugLogSocket describes the functionality required for the
// debuglog handlers to send logs to the client.
type debugLogSocket interface {
// sendOk sends a nil error response, indicating there were no errors.
sendOk()
// sendError sends a JSON-encoded error response.
sendError(err error)
// sendLogRecord sends record JSON encoded.
sendLogRecord(record *params.LogMessage) error
}
// debugLogSocketImpl implements the debugLogSocket interface. It
// wraps a websocket.Conn and provides a few debug-log specific helper
// methods.
type debugLogSocketImpl struct {
conn *websocket.Conn
}
// sendOk implements debugLogSocket.
func (s *debugLogSocketImpl) sendOk() {
s.sendError(nil)
}
// sendError implements debugLogSocket.
func (s *debugLogSocketImpl) sendError(err error) {
if sendErr := s.conn.SendInitialErrorV0(err); sendErr != nil {
logger.Errorf("closing websocket, %v", err)
s.conn.Close()
return
}
}
func (s *debugLogSocketImpl) sendLogRecord(record *params.LogMessage) error {
return s.conn.WriteJSON(record)
}
// debugLogParams contains the parsed debuglog API request parameters.
type debugLogParams struct {
startTime time.Time
maxLines uint
fromTheStart bool
noTail bool
backlog uint
filterLevel loggo.Level
includeEntity []string
excludeEntity []string
includeModule []string
excludeModule []string
includeLabel []string
excludeLabel []string
}
func readDebugLogParams(queryMap url.Values) (debugLogParams, error) {
var params debugLogParams
if value := queryMap.Get("maxLines"); value != "" {
num, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return params, errors.Errorf("maxLines value %q is not a valid unsigned number", value)
}
params.maxLines = uint(num)
}
if value := queryMap.Get("replay"); value != "" {
replay, err := strconv.ParseBool(value)
if err != nil {
return params, errors.Errorf("replay value %q is not a valid boolean", value)
}
params.fromTheStart = replay
}
if value := queryMap.Get("noTail"); value != "" {
noTail, err := strconv.ParseBool(value)
if err != nil {
return params, errors.Errorf("noTail value %q is not a valid boolean", value)
}
params.noTail = noTail
}
if value := queryMap.Get("backlog"); value != "" {
num, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return params, errors.Errorf("backlog value %q is not a valid unsigned number", value)
}
params.backlog = uint(num)
}
if value := queryMap.Get("level"); value != "" {
var ok bool
level, ok := loggo.ParseLevel(value)
if !ok || level < loggo.TRACE || level > loggo.ERROR {
return params, errors.Errorf("level value %q is not one of %q, %q, %q, %q, %q",
value, loggo.TRACE, loggo.DEBUG, loggo.INFO, loggo.WARNING, loggo.ERROR)
}
params.filterLevel = level
}
if value := queryMap.Get("startTime"); value != "" {
startTime, err := time.Parse(time.RFC3339Nano, value)
if err != nil {
return params, errors.Errorf("start time %q is not a valid time in RFC3339 format", value)
}
params.startTime = startTime
}
params.includeEntity = queryMap["includeEntity"]
params.excludeEntity = queryMap["excludeEntity"]
params.includeModule = queryMap["includeModule"]
params.excludeModule = queryMap["excludeModule"]
if label, ok := queryMap["includeLabel"]; ok {
params.includeLabel = label
}
if label, ok := queryMap["excludeLabel"]; ok {
params.excludeLabel = label
}
return params, nil
}