Skip to content

Commit

Permalink
add metric to count how many files were read from disk (#110)
Browse files Browse the repository at this point in the history
Co-authored-by: Rick Bijkerk <[email protected]>
Co-authored-by: Lars de Bruijn <[email protected]>
  • Loading branch information
3 people authored Aug 7, 2024
1 parent 1556181 commit cd11d6d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion internal/business/persistedoperations/dir_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ package persistedoperations // nolint:revive
import (
"context"
"encoding/json"
"github.com/prometheus/client_golang/prometheus"
"log/slog"
"os"
"path/filepath"
)

var (
fileLoaderGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "graphql_protect",
Subsystem: "dir_loader",
Name: "files_loaded_gauge",
Help: "number of files loaded from disk",
ConstLabels: nil,
}, []string{})
)

// DirLoader loads persisted operations from a filesystem directory
// It looks at all files in the directory, but doesn't traverse subdirectories
// If it finds a file with a `.json` extension it attempts to unmarshall it and use it as
Expand All @@ -25,8 +36,13 @@ func NewLocalDirLoader(cfg Config, log *slog.Logger) *DirLoader {
}
}

func init() {
prometheus.MustRegister(fileLoaderGauge)
}

func (d *DirLoader) Load(_ context.Context) (map[string]PersistedOperation, error) {
files, err := os.ReadDir(d.path)

if err != nil {
// if we can't read the dir, try creating it
err := os.Mkdir(d.path, 0750)
Expand All @@ -36,7 +52,7 @@ func (d *DirLoader) Load(_ context.Context) (map[string]PersistedOperation, erro
}

result := map[string]PersistedOperation{}

var filesProcessed = 0
for _, file := range files {
if file.IsDir() {
continue
Expand All @@ -49,6 +65,8 @@ func (d *DirLoader) Load(_ context.Context) (map[string]PersistedOperation, erro
continue
}

filesProcessed++

var manifestHashes map[string]string
err = json.Unmarshal(contents, &manifestHashes)
if err != nil {
Expand All @@ -62,5 +80,7 @@ func (d *DirLoader) Load(_ context.Context) (map[string]PersistedOperation, erro
}
}

fileLoaderGauge.WithLabelValues().Set(float64(filesProcessed))

return result, nil
}

0 comments on commit cd11d6d

Please sign in to comment.