metrics.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright (C) 2023 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "context"
  9. "time"
  10. "github.com/prometheus/client_golang/prometheus"
  11. "github.com/prometheus/client_golang/prometheus/promauto"
  12. "github.com/syncthing/syncthing/lib/protocol"
  13. )
  14. var (
  15. metricTotalOperationSeconds = promauto.NewCounterVec(prometheus.CounterOpts{
  16. Namespace: "syncthing",
  17. Subsystem: "fs",
  18. Name: "operation_seconds_total",
  19. Help: "Total time spent in filesystem operations, per filesystem root and operation",
  20. }, []string{"root", "operation"})
  21. metricTotalOperationsCount = promauto.NewCounterVec(prometheus.CounterOpts{
  22. Namespace: "syncthing",
  23. Subsystem: "fs",
  24. Name: "operations_total",
  25. Help: "Total number of filesystem operations, per filesystem root and operation",
  26. }, []string{"root", "operation"})
  27. metricTotalBytesCount = promauto.NewCounterVec(prometheus.CounterOpts{
  28. Namespace: "syncthing",
  29. Subsystem: "fs",
  30. Name: "operation_bytes_total",
  31. Help: "Total number of filesystem bytes transferred, per filesystem root and operation",
  32. }, []string{"root", "operation"})
  33. )
  34. const (
  35. // fs operations
  36. metricOpChmod = "chmod"
  37. metricOpLchmod = "lchmod"
  38. metricOpChtimes = "chtimes"
  39. metricOpCreate = "create"
  40. metricOpCreateSymlink = "createsymlink"
  41. metricOpDirNames = "dirnames"
  42. metricOpLstat = "lstat"
  43. metricOpMkdir = "mdkir"
  44. metricOpMkdirAll = "mkdirall"
  45. metricOpOpen = "open"
  46. metricOpOpenFile = "openfile"
  47. metricOpReadSymlink = "readsymlink"
  48. metricOpRemove = "remove"
  49. metricOpRemoveAll = "removeall"
  50. metricOpRename = "rename"
  51. metricOpStat = "stat"
  52. metricOpSymlinksSupported = "symlinkssupported"
  53. metricOpWalk = "walk"
  54. metricOpWatch = "watch"
  55. metricOpHide = "hide"
  56. metricOpUnhide = "unhide"
  57. metricOpGlob = "glob"
  58. metricOpRoots = "roots"
  59. metricOpUsage = "usage"
  60. metricOpType = "type"
  61. metricOpURI = "uri"
  62. metricOpOptions = "options"
  63. metricOpSameFile = "samefile"
  64. metricOpPlatformData = "platformdata"
  65. metricOpGetXattr = "getxattr"
  66. metricOpSetXattr = "setxattr"
  67. // file operations
  68. metricOpRead = "read"
  69. metricOpReadAt = "readat"
  70. metricOpWrite = "write"
  71. metricOpWriteAt = "writeat"
  72. metricOpTruncate = "truncate"
  73. metricOpSeek = "seek"
  74. metricOpSync = "sync"
  75. metricOpClose = "close"
  76. metricOpName = "name"
  77. )
  78. type metricsFS struct {
  79. next Filesystem
  80. }
  81. var _ Filesystem = (*metricsFS)(nil)
  82. func (m *metricsFS) account(op string) func(bytes int) {
  83. t0 := time.Now()
  84. root := m.next.URI()
  85. return func(bytes int) {
  86. if dur := time.Since(t0).Seconds(); dur > 0 {
  87. metricTotalOperationSeconds.WithLabelValues(root, op).Add(dur)
  88. }
  89. metricTotalOperationsCount.WithLabelValues(root, op).Inc()
  90. if bytes >= 0 {
  91. metricTotalBytesCount.WithLabelValues(root, op).Add(float64(bytes))
  92. }
  93. }
  94. }
  95. func (m *metricsFS) Chmod(name string, mode FileMode) error {
  96. defer m.account(metricOpChmod)(-1)
  97. return m.next.Chmod(name, mode)
  98. }
  99. func (m *metricsFS) Lchown(name string, uid, gid string) error {
  100. defer m.account(metricOpLchmod)(-1)
  101. return m.next.Lchown(name, uid, gid)
  102. }
  103. func (m *metricsFS) Chtimes(name string, atime time.Time, mtime time.Time) error {
  104. defer m.account(metricOpChtimes)(-1)
  105. return m.next.Chtimes(name, atime, mtime)
  106. }
  107. func (m *metricsFS) Create(name string) (File, error) {
  108. defer m.account(metricOpCreate)(-1)
  109. f, err := m.next.Create(name)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return &metricsFile{next: f, fs: m}, nil
  114. }
  115. func (m *metricsFS) CreateSymlink(target, name string) error {
  116. defer m.account(metricOpCreateSymlink)(-1)
  117. return m.next.CreateSymlink(target, name)
  118. }
  119. func (m *metricsFS) DirNames(name string) ([]string, error) {
  120. defer m.account(metricOpDirNames)(-1)
  121. return m.next.DirNames(name)
  122. }
  123. func (m *metricsFS) Lstat(name string) (FileInfo, error) {
  124. defer m.account(metricOpLstat)(-1)
  125. return m.next.Lstat(name)
  126. }
  127. func (m *metricsFS) Mkdir(name string, perm FileMode) error {
  128. defer m.account(metricOpMkdir)(-1)
  129. return m.next.Mkdir(name, perm)
  130. }
  131. func (m *metricsFS) MkdirAll(name string, perm FileMode) error {
  132. defer m.account(metricOpMkdirAll)(-1)
  133. return m.next.MkdirAll(name, perm)
  134. }
  135. func (m *metricsFS) Open(name string) (File, error) {
  136. defer m.account(metricOpOpen)(-1)
  137. f, err := m.next.Open(name)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return &metricsFile{next: f, fs: m}, nil
  142. }
  143. func (m *metricsFS) OpenFile(name string, flags int, mode FileMode) (File, error) {
  144. defer m.account(metricOpOpenFile)(-1)
  145. f, err := m.next.OpenFile(name, flags, mode)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return &metricsFile{next: f, fs: m}, nil
  150. }
  151. func (m *metricsFS) ReadSymlink(name string) (string, error) {
  152. defer m.account(metricOpReadSymlink)(-1)
  153. return m.next.ReadSymlink(name)
  154. }
  155. func (m *metricsFS) Remove(name string) error {
  156. defer m.account(metricOpRemove)(-1)
  157. return m.next.Remove(name)
  158. }
  159. func (m *metricsFS) RemoveAll(name string) error {
  160. defer m.account(metricOpRemoveAll)(-1)
  161. return m.next.RemoveAll(name)
  162. }
  163. func (m *metricsFS) Rename(oldname, newname string) error {
  164. defer m.account(metricOpRename)(-1)
  165. return m.next.Rename(oldname, newname)
  166. }
  167. func (m *metricsFS) Stat(name string) (FileInfo, error) {
  168. defer m.account(metricOpStat)(-1)
  169. return m.next.Stat(name)
  170. }
  171. func (m *metricsFS) SymlinksSupported() bool {
  172. defer m.account(metricOpSymlinksSupported)(-1)
  173. return m.next.SymlinksSupported()
  174. }
  175. func (m *metricsFS) Walk(name string, walkFn WalkFunc) error {
  176. defer m.account(metricOpWalk)(-1)
  177. return m.next.Walk(name, walkFn)
  178. }
  179. func (m *metricsFS) Watch(path string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error) {
  180. defer m.account(metricOpWatch)(-1)
  181. return m.next.Watch(path, ignore, ctx, ignorePerms)
  182. }
  183. func (m *metricsFS) Hide(name string) error {
  184. defer m.account(metricOpHide)(-1)
  185. return m.next.Hide(name)
  186. }
  187. func (m *metricsFS) Unhide(name string) error {
  188. defer m.account(metricOpUnhide)(-1)
  189. return m.next.Unhide(name)
  190. }
  191. func (m *metricsFS) Glob(pattern string) ([]string, error) {
  192. defer m.account(metricOpGlob)(-1)
  193. return m.next.Glob(pattern)
  194. }
  195. func (m *metricsFS) Roots() ([]string, error) {
  196. defer m.account(metricOpRoots)(-1)
  197. return m.next.Roots()
  198. }
  199. func (m *metricsFS) Usage(name string) (Usage, error) {
  200. defer m.account(metricOpUsage)(-1)
  201. return m.next.Usage(name)
  202. }
  203. func (m *metricsFS) Type() FilesystemType {
  204. defer m.account(metricOpType)(-1)
  205. return m.next.Type()
  206. }
  207. func (m *metricsFS) URI() string {
  208. defer m.account(metricOpURI)(-1)
  209. return m.next.URI()
  210. }
  211. func (m *metricsFS) Options() []Option {
  212. defer m.account(metricOpOptions)(-1)
  213. return m.next.Options()
  214. }
  215. func (m *metricsFS) SameFile(fi1, fi2 FileInfo) bool {
  216. defer m.account(metricOpSameFile)(-1)
  217. return m.next.SameFile(fi1, fi2)
  218. }
  219. func (m *metricsFS) PlatformData(name string, withOwnership, withXattrs bool, xattrFilter XattrFilter) (protocol.PlatformData, error) {
  220. defer m.account(metricOpPlatformData)(-1)
  221. return m.next.PlatformData(name, withOwnership, withXattrs, xattrFilter)
  222. }
  223. func (m *metricsFS) GetXattr(name string, xattrFilter XattrFilter) ([]protocol.Xattr, error) {
  224. defer m.account(metricOpGetXattr)(-1)
  225. return m.next.GetXattr(name, xattrFilter)
  226. }
  227. func (m *metricsFS) SetXattr(path string, xattrs []protocol.Xattr, xattrFilter XattrFilter) error {
  228. defer m.account(metricOpSetXattr)(-1)
  229. return m.next.SetXattr(path, xattrs, xattrFilter)
  230. }
  231. func (m *metricsFS) underlying() (Filesystem, bool) {
  232. return m.next, true
  233. }
  234. type metricsFile struct {
  235. fs *metricsFS
  236. next File
  237. }
  238. func (m *metricsFile) Read(p []byte) (n int, err error) {
  239. acc := m.fs.account(metricOpRead)
  240. defer func() { acc(n) }()
  241. return m.next.Read(p)
  242. }
  243. func (m *metricsFile) ReadAt(p []byte, off int64) (n int, err error) {
  244. acc := m.fs.account(metricOpReadAt)
  245. defer func() { acc(n) }()
  246. return m.next.ReadAt(p, off)
  247. }
  248. func (m *metricsFile) Seek(offset int64, whence int) (int64, error) {
  249. defer m.fs.account(metricOpSeek)(-1)
  250. return m.next.Seek(offset, whence)
  251. }
  252. func (m *metricsFile) Stat() (FileInfo, error) {
  253. defer m.fs.account(metricOpStat)(-1)
  254. return m.next.Stat()
  255. }
  256. func (m *metricsFile) Sync() error {
  257. defer m.fs.account(metricOpSync)(-1)
  258. return m.next.Sync()
  259. }
  260. func (m *metricsFile) Truncate(size int64) error {
  261. defer m.fs.account(metricOpTruncate)(-1)
  262. return m.next.Truncate(size)
  263. }
  264. func (m *metricsFile) Write(p []byte) (n int, err error) {
  265. acc := m.fs.account(metricOpWrite)
  266. defer func() { acc(n) }()
  267. return m.next.Write(p)
  268. }
  269. func (m *metricsFile) WriteAt(p []byte, off int64) (n int, err error) {
  270. acc := m.fs.account(metricOpWriteAt)
  271. defer func() { acc(n) }()
  272. return m.next.WriteAt(p, off)
  273. }
  274. func (m *metricsFile) Close() error {
  275. defer m.fs.account(metricOpClose)(-1)
  276. return m.next.Close()
  277. }
  278. func (m *metricsFile) Name() string {
  279. defer m.fs.account(metricOpName)(-1)
  280. return m.next.Name()
  281. }
  282. func (m *metricsFile) unwrap() File {
  283. return m.next
  284. }