Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(inputs.procstat): Report consistent I/O on Linux #15186

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<!-- markdownlint-disable MD024 -->
# Changelog

## Unreleased

### Important Changes

- [PR #15186](https://github.com/influxdata/telegraf/pull/15186) changes the
meaning of `inputs.procstat` fields `read_bytes` and `write_bytes` on Linux
to now contain _all_ I/O operations for consistency with other
operating-systems. The previous values are output as `disk_read_bytes` and
`disk_write_bytes` measuring _only_ the I/O on the storage layer.

## v1.30.2 [2024-04-22]

### Important Changes
Expand Down
2 changes: 2 additions & 0 deletions plugins/inputs/procstat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ Below are an example set of tags and fields:
- cpu_time_system (float)
- cpu_time_user (float)
- cpu_usage (float)
- disk_read_bytes (int, Linux only, *telegraf* may need to be ran as **root**)
- disk_write_bytes (int, Linux only, *telegraf* may need to be ran as **root**)
- involuntary_context_switches (int)
- major_faults (int)
- memory_anonymous (int)
Expand Down
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 collectTotalReadWrite(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 collectTotalReadWrite(_ 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 collectTotalReadWrite(_ Process) (r, w uint64, err error) {
return 0, 0, errors.ErrUnsupported
}
9 changes: 9 additions & 0 deletions plugins/inputs/procstat/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ func (p *Proc) Metric(prefix string, tagging map[string]bool, solarisMode bool)
fields[prefix+"write_bytes"] = io.WriteBytes
}

// Linux fixup for gopsutils exposing the disk-only-IO instead of the total
// I/O as for example on Windows
if rc, wc, err := collectTotalReadWrite(p); err == nil {
fields[prefix+"read_bytes"] = rc
fields[prefix+"write_bytes"] = wc
fields[prefix+"disk_read_bytes"] = io.ReadBytes
fields[prefix+"disk_write_bytes"] = io.WriteBytes
}

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
Loading