-
Notifications
You must be signed in to change notification settings - Fork 0
/
debuglog_db.go
105 lines (90 loc) · 2.44 KB
/
debuglog_db.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"net/http"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/juju/apiserver/httpcontext"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)
func newDebugLogDBHandler(
ctxt httpContext,
authenticator httpcontext.Authenticator,
authorizer httpcontext.Authorizer,
) http.Handler {
return newDebugLogHandler(ctxt, authenticator, authorizer, handleDebugLogDBRequest)
}
func handleDebugLogDBRequest(
clock clock.Clock,
maxDuration time.Duration,
st state.LogTailerState,
reqParams debugLogParams,
socket debugLogSocket,
stop <-chan struct{},
) error {
params := makeLogTailerParams(reqParams)
tailer, err := newLogTailer(st, params)
if err != nil {
return errors.Trace(err)
}
defer func() { _ = tailer.Stop() }()
// Indicate that all is well.
socket.sendOk()
timeout := clock.After(maxDuration)
var lineCount uint
for {
select {
case <-stop:
return nil
case <-timeout:
return nil
case rec, ok := <-tailer.Logs():
if !ok {
return errors.Annotate(tailer.Err(), "tailer stopped")
}
if err := socket.sendLogRecord(formatLogRecord(rec)); err != nil {
return errors.Annotate(err, "sending failed")
}
lineCount++
if reqParams.maxLines > 0 && lineCount == reqParams.maxLines {
return nil
}
}
}
}
func makeLogTailerParams(reqParams debugLogParams) state.LogTailerParams {
params := state.LogTailerParams{
MinLevel: reqParams.filterLevel,
NoTail: reqParams.noTail,
StartTime: reqParams.startTime,
InitialLines: int(reqParams.backlog),
IncludeEntity: reqParams.includeEntity,
ExcludeEntity: reqParams.excludeEntity,
IncludeModule: reqParams.includeModule,
ExcludeModule: reqParams.excludeModule,
IncludeLabel: reqParams.includeLabel,
ExcludeLabel: reqParams.excludeLabel,
}
if reqParams.fromTheStart {
params.InitialLines = 0
}
return params
}
func formatLogRecord(r *state.LogRecord) *params.LogMessage {
return ¶ms.LogMessage{
Entity: r.Entity,
Timestamp: r.Time,
Severity: r.Level.String(),
Module: r.Module,
Location: r.Location,
Message: r.Message,
Labels: r.Labels,
}
}
var newLogTailer = _newLogTailer // For replacing in tests
func _newLogTailer(st state.LogTailerState, params state.LogTailerParams) (state.LogTailer, error) {
return state.NewLogTailer(st, params)
}