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: 9d9551a1384a4d84bf662a43a1cf042659f8584f (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
package vfs

import (
	"context"
	"strconv"

	log "github.com/sirupsen/logrus"

	"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) (Root, error)
}

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

type instrumentedVFS struct {
	fs   VFS
	name string
}

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

func (i *instrumentedVFS) log() *log.Entry {
	return log.WithField("vfs", i.name)
}

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

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

	if err != nil {
		return nil, err
	}

	return &instrumentedRoot{root: root, name: i.name, rootPath: path}, nil
}