Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vfs.go « vfs « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a93f9e2629b282e94ee395ae06e00b7dbe4cbb86 (plain)
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
package vfs

import (
	"context"
	"strconv"

	"github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/labkit/log"

	"gitlab.com/gitlab-org/gitlab-pages/internal/config"
	"gitlab.com/gitlab-org/gitlab-pages/metrics"
)

// VFS abstracts the things Pages needs to serve a static site from disk.
type VFS interface {
	Root(ctx context.Context, path string, cacheKey string) (Root, error)
	Name() string
	Reconfigure(config *config.Config) error
}

func Instrumented(fs VFS) VFS {
	return &instrumentedVFS{fs: fs}
}

type instrumentedVFS struct {
	fs VFS
}

func (i *instrumentedVFS) increment(operation string, err error) {
	metrics.VFSOperations.WithLabelValues(i.fs.Name(), operation, strconv.FormatBool(err == nil)).Inc()
}

func (i *instrumentedVFS) log(ctx context.Context) *logrus.Entry {
	return log.ContextLogger(ctx).WithField("vfs", i.fs.Name())
}

func (i *instrumentedVFS) Root(ctx context.Context, path string, cacheKey string) (Root, error) {
	root, err := i.fs.Root(ctx, path, cacheKey)

	i.increment("Root", err)
	i.log(ctx).
		WithField("path", path).
		WithError(err).
		Traceln("Root call")

	if err != nil {
		return nil, err
	}

	return &instrumentedRoot{root: root, name: i.fs.Name(), rootPath: path}, nil
}

func (i *instrumentedVFS) Name() string {
	return i.fs.Name()
}

func (i *instrumentedVFS) Reconfigure(cfg *config.Config) error {
	return i.fs.Reconfigure(cfg)
}