Skip to content

Commit

Permalink
feat(inputs.procstat): Add fields for cached read/write bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Apr 19, 2024
1 parent f5d393c commit 772e0a7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
27 changes: 27 additions & 0 deletions plugins/inputs/procstat/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"context"
"errors"
"fmt"
"os"

"github.com/prometheus/procfs"

"github.com/coreos/go-systemd/v22/dbus"
"github.com/shirou/gopsutil/v3/process"
Expand Down Expand Up @@ -76,3 +79,27 @@ func findBySystemdUnits(units []string) ([]processGroup, error) {
func findByWindowsServices(_ []string) ([]processGroup, error) {
return nil, nil
}

func collectCachedReadWrite(proc Process) (r, w uint64, err error) {
path := procfs.DefaultMountPoint
if hp := os.Getenv("HOST_PROC"); hp != "" {
path = hp
}

fs, err := procfs.NewFS(path)
if err != nil {
return 0, 0, err
}

p, err := fs.Proc(int(proc.PID()))
if err != nil {
return 0, 0, err
}

stat, err := p.IO()
if err != nil {
return 0, 0, err
}

return stat.RChar, stat.WChar, nil
}
4 changes: 4 additions & 0 deletions plugins/inputs/procstat/os_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ func findBySystemdUnits(_ []string) ([]processGroup, error) {
func findByWindowsServices(_ []string) ([]processGroup, error) {
return nil, nil
}

func collectCachedReadWrite(_ Process) (r, w uint64, err error) {
return 0, 0, errors.ErrUnsupported
}
4 changes: 4 additions & 0 deletions plugins/inputs/procstat/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ func findByWindowsServices(services []string) ([]processGroup, error) {

return groups, nil
}

func collectCachedReadWrite(_ Process) (r, w uint64, err error) {
return 0, 0, errors.ErrUnsupported
}
5 changes: 5 additions & 0 deletions plugins/inputs/procstat/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func (p *Proc) Metric(prefix string, tagging map[string]bool, solarisMode bool)
fields[prefix+"write_bytes"] = io.WriteBytes
}

if rc, wc, err := collectCachedReadWrite(p); err == nil {
fields[prefix+"cached_read_bytes"] = rc
fields[prefix+"cached_write_bytes"] = wc
}

createdAt, err := p.CreateTime() // returns epoch in ms
if err == nil {
fields[prefix+"created_at"] = createdAt * 1000000 // ms to ns
Expand Down
2 changes: 0 additions & 2 deletions plugins/inputs/procstat/procstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,6 @@ func TestGather_ProcessName(t *testing.T) {

var acc testutil.Accumulator
require.NoError(t, p.Gather(&acc))

testutil.PrintMetrics(acc.GetTelegrafMetrics())
require.Equal(t, "custom_name", acc.TagValue("procstat", "process_name"))
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
Expand Down

0 comments on commit 772e0a7

Please sign in to comment.