forked from sajari/storage
/
log.go
188 lines (162 loc) · 4.87 KB
/
log.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
package storage
import (
"expvar"
"io"
"log"
"golang.org/x/net/context"
"golang.org/x/net/trace"
)
// NewLogFS creates a new FS which logs all calls to FS.
func NewLogFS(fs FS, name string, l *log.Logger) *LogFS {
return &LogFS{
fs: fs,
name: name,
logger: l,
}
}
// LogFS is an FS implementation which logs all filesystem calls.
type LogFS struct {
fs FS
name string
logger *log.Logger
}
// Open implements FS. All calls to Open are logged and errors are logged seperately.
func (l *LogFS) Open(ctx context.Context, path string) (*File, error) {
l.logger.Printf("%v: open: %v", l.name, path)
f, err := l.fs.Open(ctx, path)
if err != nil {
l.logger.Printf("%v: open error: %v: %v", l.name, path, err)
}
return f, err
}
// Create implements FS. All calls to Create are logged and errors are logged seperately.
func (l *LogFS) Create(ctx context.Context, path string) (io.WriteCloser, error) {
l.logger.Printf("%v: create: %v", l.name, path)
wc, err := l.fs.Create(ctx, path)
if err != nil {
l.logger.Printf("%v: create error: %v: %v", l.name, path, err)
}
return wc, err
}
// Delete implements FS. All calls to Delete are logged and errors are logged seperately.
func (l *LogFS) Delete(ctx context.Context, path string) error {
l.logger.Printf("%v: delete: %v", l.name, path)
err := l.fs.Delete(ctx, path)
if err != nil {
l.logger.Printf("%v: delete error: %v: %v", l.name, path, err)
}
return err
}
// Walk implements FS. No logs are written at this time.
func (l *LogFS) Walk(ctx context.Context, path string, fn WalkFn) error {
return l.fs.Walk(ctx, path, fn)
}
// NewTraceFS creates a new FS which wraps an FS and records calls using
// golang.org/x/net/trace.
func NewTraceFS(fs FS, name string) *TraceFS {
return &TraceFS{
fs: fs,
name: name,
}
}
// TraceFS is a FS implementation which wraps an FS and records
// calls using golang.org/x/net/trace.
type TraceFS struct {
fs FS
name string
}
// Open implements FS. All calls to Open are logged via golang.org/x/net/trace.
func (t *TraceFS) Open(ctx context.Context, path string) (f *File, err error) {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("%v: open: %v", t.name, path)
defer func() {
if err != nil {
tr.LazyPrintf("%v: error: %v", t.name, err)
tr.SetError()
}
}()
}
return t.fs.Open(ctx, path)
}
// Create implements FS. All calls to Create are logged via golang.org/x/net/trace.
func (t *TraceFS) Create(ctx context.Context, path string) (wc io.WriteCloser, err error) {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("%v: create: %v", t.name, path)
defer func() {
if err != nil {
tr.LazyPrintf("%v: error: %v", t.name, err)
tr.SetError()
}
}()
}
return t.fs.Create(ctx, path)
}
// Delete implements FS. All calls to Delete are logged via golang.org/x/net/trace.
func (t *TraceFS) Delete(ctx context.Context, path string) (err error) {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("%v: delete: %v", t.name, path)
defer func() {
if err != nil {
tr.LazyPrintf("%v: error: %v", t.name, err)
tr.SetError()
}
}()
}
return t.fs.Delete(ctx, path)
}
// Walk implements FS. Nothing is traced at this time.
func (t *TraceFS) Walk(ctx context.Context, path string, fn WalkFn) error {
return t.fs.Walk(ctx, path, fn)
}
// NewErrCountFS creates an FS which records stats based on usage.
func NewErrCountFS(fs FS, name string, err error) *ErrCountFS {
status := expvar.NewMap(name)
status.Set("open.total", new(expvar.Int))
status.Set("open.count", new(expvar.Int))
status.Set("create.total", new(expvar.Int))
status.Set("create.count", new(expvar.Int))
status.Set("delete.total", new(expvar.Int))
status.Set("delete.count", new(expvar.Int))
return &ErrCountFS{
fs: fs,
status: status,
err: err,
}
}
// ErrCountFS is an FS which records error counts for an FS.
type ErrCountFS struct {
fs FS
err error
status *expvar.Map
}
// Open implements FS. All errors from Open are counted.
func (s ErrCountFS) Open(ctx context.Context, path string) (*File, error) {
f, err := s.fs.Open(ctx, path)
if err == s.err {
s.status.Add("open.count", 1)
}
s.status.Add("open.total", 1)
return f, err
}
// Create implements FS. All errors from Create are counted.
func (s ErrCountFS) Create(ctx context.Context, path string) (io.WriteCloser, error) {
wc, err := s.fs.Create(ctx, path)
if err == s.err {
s.status.Add("create.count", 1)
}
s.status.Add("create.total", 1)
return wc, err
}
// Delete implements FS. All errors from Delete are counted.
func (s ErrCountFS) Delete(ctx context.Context, path string) error {
err := s.fs.Delete(ctx, path)
if err == s.err {
s.status.Add("delete.count", 1)
}
s.status.Add("delete.total", 1)
return err
}
// Walk implements FS. No stats are recorded at this time.
func (s ErrCountFS) Walk(ctx context.Context, path string, fn WalkFn) error {
return s.fs.Walk(ctx, path, fn)
}