This commit is contained in:
Jakob Borg 2023-07-17 12:19:36 +02:00
parent 89bb36d4f5
commit 76f185adb8

View File

@ -28,6 +28,12 @@ var (
Name: "operations_total", Name: "operations_total",
Help: "Total number of FS operations", Help: "Total number of FS operations",
}, []string{"root", "operation"}) }, []string{"root", "operation"})
metricTotalBytesCount = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "syncthing",
Subsystem: "fs",
Name: "operation_bytes_total",
Help: "Total number of FS bytes",
}, []string{"root", "operation"})
) )
type metricsFS struct { type metricsFS struct {
@ -36,167 +42,182 @@ type metricsFS struct {
var _ Filesystem = (*metricsFS)(nil) var _ Filesystem = (*metricsFS)(nil)
func (m *metricsFS) account(op string) func() { func (m *metricsFS) account(op string) func(bytes int) {
t0 := time.Now() t0 := time.Now()
root := m.next.URI() root := m.next.URI()
return func() { return func(bytes int) {
metricTotalOperationSeconds.WithLabelValues(root, op).Add(time.Since(t0).Seconds()) metricTotalOperationSeconds.WithLabelValues(root, op).Add(time.Since(t0).Seconds())
metricTotalOperationsCount.WithLabelValues(root, op).Inc() metricTotalOperationsCount.WithLabelValues(root, op).Inc()
if bytes >= 0 {
metricTotalBytesCount.WithLabelValues(root, op).Add(float64(bytes))
}
} }
} }
func (m *metricsFS) Chmod(name string, mode FileMode) error { func (m *metricsFS) Chmod(name string, mode FileMode) error {
defer m.account("Chmod")() defer m.account("Chmod")(-1)
return m.next.Chmod(name, mode) return m.next.Chmod(name, mode)
} }
func (m *metricsFS) Lchown(name string, uid, gid string) error { func (m *metricsFS) Lchown(name string, uid, gid string) error {
defer m.account("Lchown")() defer m.account("Lchown")(-1)
return m.next.Lchown(name, uid, gid) return m.next.Lchown(name, uid, gid)
} }
func (m *metricsFS) Chtimes(name string, atime time.Time, mtime time.Time) error { func (m *metricsFS) Chtimes(name string, atime time.Time, mtime time.Time) error {
defer m.account("Chtimes")() defer m.account("Chtimes")(-1)
return m.next.Chtimes(name, atime, mtime) return m.next.Chtimes(name, atime, mtime)
} }
func (m *metricsFS) Create(name string) (File, error) { func (m *metricsFS) Create(name string) (File, error) {
defer m.account("Create")() defer m.account("Create")(-1)
return m.next.Create(name) f, err := m.next.Create(name)
if err != nil {
return nil, err
}
return &metricsFile{next: f, fs: m}, nil
} }
func (m *metricsFS) CreateSymlink(target, name string) error { func (m *metricsFS) CreateSymlink(target, name string) error {
defer m.account("CreateSymlink")() defer m.account("CreateSymlink")(-1)
return m.next.CreateSymlink(target, name) return m.next.CreateSymlink(target, name)
} }
func (m *metricsFS) DirNames(name string) ([]string, error) { func (m *metricsFS) DirNames(name string) ([]string, error) {
defer m.account("DirNames")() defer m.account("DirNames")(-1)
return m.next.DirNames(name) return m.next.DirNames(name)
} }
func (m *metricsFS) Lstat(name string) (FileInfo, error) { func (m *metricsFS) Lstat(name string) (FileInfo, error) {
defer m.account("Lstat")() defer m.account("Lstat")(-1)
return m.next.Lstat(name) return m.next.Lstat(name)
} }
func (m *metricsFS) Mkdir(name string, perm FileMode) error { func (m *metricsFS) Mkdir(name string, perm FileMode) error {
defer m.account("Mkdir")() defer m.account("Mkdir")(-1)
return m.next.Mkdir(name, perm) return m.next.Mkdir(name, perm)
} }
func (m *metricsFS) MkdirAll(name string, perm FileMode) error { func (m *metricsFS) MkdirAll(name string, perm FileMode) error {
defer m.account("MkdirAll")() defer m.account("MkdirAll")(-1)
return m.next.MkdirAll(name, perm) return m.next.MkdirAll(name, perm)
} }
func (m *metricsFS) Open(name string) (File, error) { func (m *metricsFS) Open(name string) (File, error) {
defer m.account("Open")() defer m.account("Open")(-1)
return m.next.Open(name) f, err := m.next.Open(name)
if err != nil {
return nil, err
}
return &metricsFile{next: f, fs: m}, nil
} }
func (m *metricsFS) OpenFile(name string, flags int, mode FileMode) (File, error) { func (m *metricsFS) OpenFile(name string, flags int, mode FileMode) (File, error) {
defer m.account("OpenFile")() defer m.account("OpenFile")(-1)
return m.next.OpenFile(name, flags, mode) f, err := m.next.OpenFile(name, flags, mode)
if err != nil {
return nil, err
}
return &metricsFile{next: f, fs: m}, nil
} }
func (m *metricsFS) ReadSymlink(name string) (string, error) { func (m *metricsFS) ReadSymlink(name string) (string, error) {
defer m.account("ReadSymlink")() defer m.account("ReadSymlink")(-1)
return m.next.ReadSymlink(name) return m.next.ReadSymlink(name)
} }
func (m *metricsFS) Remove(name string) error { func (m *metricsFS) Remove(name string) error {
defer m.account("Remove")() defer m.account("Remove")(-1)
return m.next.Remove(name) return m.next.Remove(name)
} }
func (m *metricsFS) RemoveAll(name string) error { func (m *metricsFS) RemoveAll(name string) error {
defer m.account("RemoveAll")() defer m.account("RemoveAll")(-1)
return m.next.RemoveAll(name) return m.next.RemoveAll(name)
} }
func (m *metricsFS) Rename(oldname, newname string) error { func (m *metricsFS) Rename(oldname, newname string) error {
defer m.account("Rename")() defer m.account("Rename")(-1)
return m.next.Rename(oldname, newname) return m.next.Rename(oldname, newname)
} }
func (m *metricsFS) Stat(name string) (FileInfo, error) { func (m *metricsFS) Stat(name string) (FileInfo, error) {
defer m.account("Stat")() defer m.account("Stat")(-1)
return m.next.Stat(name) return m.next.Stat(name)
} }
func (m *metricsFS) SymlinksSupported() bool { func (m *metricsFS) SymlinksSupported() bool {
defer m.account("SymlinksSupported")() defer m.account("SymlinksSupported")(-1)
return m.next.SymlinksSupported() return m.next.SymlinksSupported()
} }
func (m *metricsFS) Walk(name string, walkFn WalkFunc) error { func (m *metricsFS) Walk(name string, walkFn WalkFunc) error {
defer m.account("Walk")() defer m.account("Walk")(-1)
return m.next.Walk(name, walkFn) return m.next.Walk(name, walkFn)
} }
func (m *metricsFS) Watch(path string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error) { func (m *metricsFS) Watch(path string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error) {
defer m.account("Watch")() defer m.account("Watch")(-1)
return m.next.Watch(path, ignore, ctx, ignorePerms) return m.next.Watch(path, ignore, ctx, ignorePerms)
} }
func (m *metricsFS) Hide(name string) error { func (m *metricsFS) Hide(name string) error {
defer m.account("Hide")() defer m.account("Hide")(-1)
return m.next.Hide(name) return m.next.Hide(name)
} }
func (m *metricsFS) Unhide(name string) error { func (m *metricsFS) Unhide(name string) error {
defer m.account("Unhide")() defer m.account("Unhide")(-1)
return m.next.Unhide(name) return m.next.Unhide(name)
} }
func (m *metricsFS) Glob(pattern string) ([]string, error) { func (m *metricsFS) Glob(pattern string) ([]string, error) {
defer m.account("Glob")() defer m.account("Glob")(-1)
return m.next.Glob(pattern) return m.next.Glob(pattern)
} }
func (m *metricsFS) Roots() ([]string, error) { func (m *metricsFS) Roots() ([]string, error) {
defer m.account("Roots")() defer m.account("Roots")(-1)
return m.next.Roots() return m.next.Roots()
} }
func (m *metricsFS) Usage(name string) (Usage, error) { func (m *metricsFS) Usage(name string) (Usage, error) {
defer m.account("Usage")() defer m.account("Usage")(-1)
return m.next.Usage(name) return m.next.Usage(name)
} }
func (m *metricsFS) Type() FilesystemType { func (m *metricsFS) Type() FilesystemType {
defer m.account("Type")() defer m.account("Type")(-1)
return m.next.Type() return m.next.Type()
} }
func (m *metricsFS) URI() string { func (m *metricsFS) URI() string {
defer m.account("URI")() defer m.account("URI")(-1)
return m.next.URI() return m.next.URI()
} }
func (m *metricsFS) Options() []Option { func (m *metricsFS) Options() []Option {
defer m.account("Options")() defer m.account("Options")(-1)
return m.next.Options() return m.next.Options()
} }
func (m *metricsFS) SameFile(fi1, fi2 FileInfo) bool { func (m *metricsFS) SameFile(fi1, fi2 FileInfo) bool {
defer m.account("SameFile")() defer m.account("SameFile")(-1)
return m.next.SameFile(fi1, fi2) return m.next.SameFile(fi1, fi2)
} }
func (m *metricsFS) PlatformData(name string, withOwnership, withXattrs bool, xattrFilter XattrFilter) (protocol.PlatformData, error) { func (m *metricsFS) PlatformData(name string, withOwnership, withXattrs bool, xattrFilter XattrFilter) (protocol.PlatformData, error) {
defer m.account("PlatformData")() defer m.account("PlatformData")(-1)
return m.next.PlatformData(name, withOwnership, withXattrs, xattrFilter) return m.next.PlatformData(name, withOwnership, withXattrs, xattrFilter)
} }
func (m *metricsFS) GetXattr(name string, xattrFilter XattrFilter) ([]protocol.Xattr, error) { func (m *metricsFS) GetXattr(name string, xattrFilter XattrFilter) ([]protocol.Xattr, error) {
defer m.account("GetXattr")() defer m.account("GetXattr")(-1)
return m.next.GetXattr(name, xattrFilter) return m.next.GetXattr(name, xattrFilter)
} }
func (m *metricsFS) SetXattr(path string, xattrs []protocol.Xattr, xattrFilter XattrFilter) error { func (m *metricsFS) SetXattr(path string, xattrs []protocol.Xattr, xattrFilter XattrFilter) error {
defer m.account("SetXattr")() defer m.account("SetXattr")(-1)
return m.next.SetXattr(path, xattrs, xattrFilter) return m.next.SetXattr(path, xattrs, xattrFilter)
} }
@ -207,3 +228,62 @@ func (m *metricsFS) underlying() (Filesystem, bool) {
func (m *metricsFS) wrapperType() filesystemWrapperType { func (m *metricsFS) wrapperType() filesystemWrapperType {
return filesystemWrapperTypeMetrics return filesystemWrapperTypeMetrics
} }
type metricsFile struct {
fs *metricsFS
next File
}
func (m *metricsFile) Read(p []byte) (n int, err error) {
acc := m.fs.account("Read")
defer func() { acc(n) }()
return m.next.Read(p)
}
func (m *metricsFile) ReadAt(p []byte, off int64) (n int, err error) {
acc := m.fs.account("ReadAt")
defer func() { acc(n) }()
return m.next.ReadAt(p, off)
}
func (m *metricsFile) Seek(offset int64, whence int) (int64, error) {
defer m.fs.account("Seek")(-1)
return m.next.Seek(offset, whence)
}
func (m *metricsFile) Stat() (FileInfo, error) {
defer m.fs.account("Stat")(-1)
return m.next.Stat()
}
func (m *metricsFile) Sync() error {
defer m.fs.account("Sync")(-1)
return m.next.Sync()
}
func (m *metricsFile) Truncate(size int64) error {
defer m.fs.account("Truncate")(-1)
return m.next.Truncate(size)
}
func (m *metricsFile) Write(p []byte) (n int, err error) {
acc := m.fs.account("Write")
defer func() { acc(n) }()
return m.next.Write(p)
}
func (m *metricsFile) WriteAt(p []byte, off int64) (n int, err error) {
acc := m.fs.account("WriteAt")
defer func() { acc(n) }()
return m.next.WriteAt(p, off)
}
func (m *metricsFile) Close() error {
defer m.fs.account("Close")(-1)
return m.next.Close()
}
func (m *metricsFile) Name() string {
defer m.fs.account("Name")(-1)
return m.next.Name()
}