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
|
package vfs
import (
"context"
"os"
"strconv"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/labkit/log"
"gitlab.com/gitlab-org/gitlab-pages/metrics"
)
// Root abstracts the things Pages needs to serve a static site from a given root rootPath.
type Root interface {
Lstat(ctx context.Context, name string) (os.FileInfo, error)
Readlink(ctx context.Context, name string) (string, error)
Open(ctx context.Context, name string) (File, error)
IsCompressed(name string) (bool, error)
}
type instrumentedRoot struct {
root Root
name string
rootPath string
}
func (i *instrumentedRoot) increment(operation string, err error) {
metrics.VFSOperations.WithLabelValues(i.name, operation, strconv.FormatBool(err == nil)).Inc()
}
func (i *instrumentedRoot) log(ctx context.Context) *logrus.Entry {
return log.ContextLogger(ctx).WithField("vfs", i.name).WithField("root-path", i.rootPath)
}
func (i *instrumentedRoot) Lstat(ctx context.Context, name string) (os.FileInfo, error) {
fi, err := i.root.Lstat(ctx, name)
i.increment("Lstat", err)
i.log(ctx).
WithField("name", name).
WithError(err).
Traceln("Lstat call")
return fi, err
}
func (i *instrumentedRoot) Readlink(ctx context.Context, name string) (string, error) {
target, err := i.root.Readlink(ctx, name)
i.increment("Readlink", err)
i.log(ctx).
WithField("name", name).
WithField("ret-target", target).
WithError(err).
Traceln("Readlink call")
return target, err
}
func (i *instrumentedRoot) Open(ctx context.Context, name string) (File, error) {
f, err := i.root.Open(ctx, name)
i.increment("Open", err)
i.log(ctx).
WithField("name", name).
WithError(err).
Traceln("Open call")
return f, err
}
func (i *instrumentedRoot) IsCompressed(name string) (bool, error) {
return i.root.IsCompressed(name)
}
|