Skip to content

Commit

Permalink
metric for deleted and unclosed file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryRomanov committed Apr 26, 2024
1 parent 953aa11 commit 4408aa0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
52 changes: 52 additions & 0 deletions fd/fds_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fd

import (
"os"
"strings"

"github.com/ozontech/file.d/logger"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
)

func newFdsCollector() prometheus.Collector {
c := &fdsCollector{
processDeletedFds: prometheus.NewDesc(
"process_deleted_fds",
"Number of deleted and unclosed file descriptors.",
nil, nil,
),
}
return c
}

type fdsCollector struct {
processDeletedFds *prometheus.Desc
}

func (f *fdsCollector) Collect(ch chan<- prometheus.Metric) {
p, err := procfs.NewProc(os.Getpid())
if err != nil {
logger.Errorf("failed to get procfs: %s", err.Error())
return
}

targets, err := p.FileDescriptorTargets()
if err != nil {
logger.Errorf("failed to read file descriptor targets: %s", err.Error())
return
}

processDeletedFds := 0
for _, target := range targets {
if strings.HasSuffix(target, " (deleted)") {
processDeletedFds++
}
}

ch <- prometheus.MustNewConstMetric(f.processDeletedFds, prometheus.GaugeValue, float64(processDeletedFds))
}

func (f *fdsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- f.processDeletedFds
}
1 change: 1 addition & 0 deletions fd/file.d.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (f *FileD) createRegistry() {
f.registry = prometheus.NewRegistry()
f.registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
f.registry.MustRegister(prometheus.NewGoCollector())
f.registry.MustRegister(newFdsCollector())
}

func (f *FileD) startPipelines() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/klauspost/compress v1.16.7
github.com/minio/minio-go v6.0.14+incompatible
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/procfs v0.10.1
github.com/rjeczalik/notify v0.9.3
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -120,7 +121,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
Expand Down

0 comments on commit 4408aa0

Please sign in to comment.