Skip to content
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
8 changes: 0 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ linters:
capital: true
misspell:
locale: US
revive:
rules:
- name: var-naming
# TODO(SuperQ): See: https://github.com/prometheus/prometheus/issues/17766
arguments:
- []
- []
- - skip-package-name-checks: true
exclusions:
presets:
- comments
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ The `proc` and `sys` filesystems are pseudo file systems and work a bit differen
Many of the files are changing continuously and the data being read can in some cases change between subsequent
reads in the same file. Also, most of the files are relatively small (less than a few KBs), and system calls
to the `stat` function will often return the wrong size. Therefore, for most files it's recommended to read the
full file in a single operation using an internal utility function called `util.ReadFileNoStat`.
full file in a single operation using an internal utility function called `parsers.ReadFileNoStat`.
This function is similar to `os.ReadFile`, but it avoids the system call to `stat` to get the current size of
the file.

Note that parsing the file's contents can still be performed one line at a time. This is done by first reading
the full file, and then using a scanner on the `[]byte` or `string` containing the data.

```
data, err := util.ReadFileNoStat("/proc/cpuinfo")
data, err := parsers.ReadFileNoStat("/proc/cpuinfo")
if err != nil {
return err
}
Expand All @@ -113,9 +113,9 @@ the full file, and then using a scanner on the `[]byte` or `string` containing t
```

The `/sys` filesystem contains many very small files which contain only a single numeric or text value. These files
can be read using an internal function called `util.SysReadFile` which is similar to `os.ReadFile` but does
can be read using an internal function called `parsers.SysReadFile` which is similar to `os.ReadFile` but does
not bother to check the size of the file before reading.
```
data, err := util.SysReadFile("/sys/class/power_supply/BAT0/capacity")
data, err := parsers.SysReadFile("/sys/class/power_supply/BAT0/capacity")
```

8 changes: 4 additions & 4 deletions bcachefs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"

"github.com/prometheus/procfs/internal/fs"
"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

// FS represents the pseudo-filesystem sys, which provides an interface to
Expand Down Expand Up @@ -96,7 +96,7 @@ func (r *reader) readFile(n string) string {
if r.err != nil {
return ""
}
b, err := util.ReadFileNoStat(filepath.Join(r.path, n))
b, err := parsers.ReadFileNoStat(filepath.Join(r.path, n))
if err != nil {
if !os.IsNotExist(err) {
r.err = err
Expand Down Expand Up @@ -467,15 +467,15 @@ func parseDevices(fsPath string) (map[string]*DeviceStats, error) {
}

func readSysfsFile(path string) string {
data, err := util.ReadFileNoStat(path)
data, err := parsers.ReadFileNoStat(path)
if err != nil {
return ""
}
return strings.TrimSpace(string(data))
}

func readUintFile(path string) (uint64, error) {
data, err := util.ReadFileNoStat(path)
data, err := parsers.ReadFileNoStat(path)
if err != nil {
if os.IsNotExist(err) {
return 0, nil
Expand Down
12 changes: 6 additions & 6 deletions blockdevice/dm_multipath.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"

"github.com/prometheus/procfs"
"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

// DMMultipathDevice contains information about a single DM-multipath device
Expand Down Expand Up @@ -68,7 +68,7 @@ func (fs FS) DMMultipathDevices() ([]DMMultipathDevice, error) {
continue
}

uuid, err := util.SysReadFile(fs.sys.Path(sysBlockPath, entry.Name(), sysBlockDM, "uuid"))
uuid, err := parsers.SysReadFile(fs.sys.Path(sysBlockPath, entry.Name(), sysBlockDM, "uuid"))
if err != nil {
// dm/uuid missing means this is not a device-mapper device; skip it.
if os.IsNotExist(err) {
Expand All @@ -80,17 +80,17 @@ func (fs FS) DMMultipathDevices() ([]DMMultipathDevice, error) {
continue
}

name, err := util.SysReadFile(fs.sys.Path(sysBlockPath, entry.Name(), sysBlockDM, "name"))
name, err := parsers.SysReadFile(fs.sys.Path(sysBlockPath, entry.Name(), sysBlockDM, "name"))
if err != nil {
return nil, fmt.Errorf("failed to read dm/name for %s: %w", entry.Name(), err)
}

suspendedVal, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, entry.Name(), sysBlockDM, "suspended"))
suspendedVal, err := parsers.ReadUintFromFile(fs.sys.Path(sysBlockPath, entry.Name(), sysBlockDM, "suspended"))
if err != nil {
return nil, fmt.Errorf("failed to read dm/suspended for %s: %w", entry.Name(), err)
}

sectors, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, entry.Name(), sysBlockSize))
sectors, err := parsers.ReadUintFromFile(fs.sys.Path(sysBlockPath, entry.Name(), sysBlockSize))
if err != nil {
return nil, fmt.Errorf("failed to read size for %s: %w", entry.Name(), err)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (fs FS) dmMultipathPaths(dmDevice string) ([]DMMultipathPath, error) {

paths := make([]DMMultipathPath, 0, len(entries))
for _, entry := range entries {
state, err := util.SysReadFile(fs.sys.Path(sysBlockPath, entry.Name(), sysDevicePath, "state"))
state, err := parsers.SysReadFile(fs.sys.Path(sysBlockPath, entry.Name(), sysDevicePath, "state"))
if err != nil {
return nil, fmt.Errorf("failed to read device/state for %s: %w", entry.Name(), err)
}
Expand Down
22 changes: 11 additions & 11 deletions blockdevice/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/prometheus/procfs"
"github.com/prometheus/procfs/internal/fs"
"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

// Info contains identifying information for a block device such as a disk drive.
Expand Down Expand Up @@ -392,7 +392,7 @@ func (fs FS) SysBlockDeviceQueueStats(device string) (BlockQueueStats, error) {
"max_discard_segments": &stat.MaxDiscardSegments,
"write_zeroes_max_bytes": &stat.WriteZeroesMaxBytes,
} {
val, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, file))
val, err := parsers.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, file))
if err != nil {
return BlockQueueStats{}, err
}
Expand All @@ -403,7 +403,7 @@ func (fs FS) SysBlockDeviceQueueStats(device string) (BlockQueueStats, error) {
"io_poll_delay": &stat.IOPollDelay,
"wbt_lat_usec": &stat.WBTLatUSec,
} {
val, err := util.ReadIntFromFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, file))
val, err := parsers.ReadIntFromFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, file))
if err != nil {
return BlockQueueStats{}, err
}
Expand All @@ -414,13 +414,13 @@ func (fs FS) SysBlockDeviceQueueStats(device string) (BlockQueueStats, error) {
"write_cache": &stat.WriteCache,
"zoned": &stat.Zoned,
} {
val, err := util.SysReadFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, file))
val, err := parsers.SysReadFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, file))
if err != nil {
return BlockQueueStats{}, err
}
*p = val
}
scheduler, err := util.SysReadFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, "scheduler"))
scheduler, err := parsers.SysReadFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, "scheduler"))
if err != nil {
return BlockQueueStats{}, err
}
Expand All @@ -434,7 +434,7 @@ func (fs FS) SysBlockDeviceQueueStats(device string) (BlockQueueStats, error) {
}
stat.SchedulerList = schedulers
// optional
throttleSampleTime, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, "throttle_sample_time"))
throttleSampleTime, err := parsers.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, "throttle_sample_time"))
if err == nil {
stat.ThrottleSampleTime = &throttleSampleTime
}
Expand All @@ -449,7 +449,7 @@ func (fs FS) SysBlockDeviceMapperInfo(device string) (DeviceMapperInfo, error) {
"suspended": &info.Suspended,
"use_blk_mq": &info.UseBlkMQ,
} {
val, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockDM, file))
val, err := parsers.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockDM, file))
if err != nil {
return DeviceMapperInfo{}, err
}
Expand All @@ -460,7 +460,7 @@ func (fs FS) SysBlockDeviceMapperInfo(device string) (DeviceMapperInfo, error) {
"name": &info.Name,
"uuid": &info.UUID,
} {
val, err := util.SysReadFile(fs.sys.Path(sysBlockPath, device, sysBlockDM, file))
val, err := parsers.SysReadFile(fs.sys.Path(sysBlockPath, device, sysBlockDM, file))
if err != nil {
return DeviceMapperInfo{}, err
}
Expand All @@ -485,7 +485,7 @@ func (fs FS) SysBlockDeviceUnderlyingDevices(device string) (UnderlyingDeviceInf
// SysBlockDeviceSize returns the size of the block device from /sys/block/<device>/size
// in bytes by multiplying the value by the Linux sector length of 512.
func (fs FS) SysBlockDeviceSize(device string) (uint64, error) {
size, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize))
size, err := parsers.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize))
if err != nil {
return 0, err
}
Expand All @@ -498,7 +498,7 @@ func (fs FS) SysBlockDeviceSize(device string) (uint64, error) {
// non-rotational device (SSD, NVMe). An error is returned if the file
// cannot be read or does not contain a valid integer.
func (fs FS) SysBlockDeviceRotational(device string) (uint64, error) {
return util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, "rotational"))
return parsers.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, "rotational"))
}

// SysBlockDeviceIO returns stats for the block device io counters
Expand All @@ -514,7 +514,7 @@ func (fs FS) SysBlockDeviceIOStat(device string) (IODeviceStats, error) {
"ioerr_cnt": &ioDeviceStats.IOErrCount,
} {
var val uint64
val, err = util.ReadHexFromFile(fs.sys.Path(sysBlockPath, device, sysDevicePath, file))
val, err = parsers.ReadHexFromFile(fs.sys.Path(sysBlockPath, device, sysDevicePath, file))
if err != nil {
return IODeviceStats{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions btrfs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/prometheus/procfs"
"github.com/prometheus/procfs/internal/fs"
"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

// FS represents the pseudo-filesystem sys, which provides an interface to
Expand Down Expand Up @@ -97,7 +97,7 @@ type reader struct {
// readFile reads a file relative to the path of the reader.
// Non-existing files are ignored.
func (r *reader) readFile(n string) string {
b, err := util.SysReadFile(path.Join(r.path, n))
b, err := parsers.SysReadFile(path.Join(r.path, n))
if err != nil && !os.IsNotExist(err) {
r.err = err
}
Expand Down
4 changes: 2 additions & 2 deletions cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package procfs
import (
"strings"

"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

// CmdLine returns the command line of the kernel.
func (fs FS) CmdLine() ([]string, error) {
data, err := util.ReadFileNoStat(fs.proc.Path("cmdline"))
data, err := parsers.ReadFileNoStat(fs.proc.Path("cmdline"))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cpuinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strconv"
"strings"

"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

// CPUInfo contains general information about a system CPU found in /proc/cpuinfo.
Expand Down Expand Up @@ -65,7 +65,7 @@ var (
// CPUInfo returns information about current system CPUs.
// See https://www.kernel.org/doc/Documentation/filesystems/proc.txt
func (fs FS) CPUInfo() ([]CPUInfo, error) {
data, err := util.ReadFileNoStat(fs.proc.Path("cpuinfo"))
data, err := parsers.ReadFileNoStat(fs.proc.Path("cpuinfo"))
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"io"
"strings"

"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

// Crypto holds info parsed from /proc/crypto.
Expand Down Expand Up @@ -55,7 +55,7 @@ var cryptoFile = "crypto"
// https://kernel.readthedocs.io/en/sphinx-samples/crypto-API.html
func (fs FS) Crypto() ([]Crypto, error) {
path := fs.proc.Path(cryptoFile)
b, err := util.ReadFileNoStat(path)
b, err := parsers.ReadFileNoStat(path)
if err != nil {
return nil, fmt.Errorf("%w: Cannot read file %v: %w", ErrFileRead, b, err)

Expand Down Expand Up @@ -112,7 +112,7 @@ func parseCrypto(r io.Reader) ([]Crypto, error) {

// parseKV parses a key/value pair into the appropriate field of c.
func (c *Crypto) parseKV(k, v string) error {
vp := util.NewValueParser(v)
vp := parsers.NewValueParser(v)

switch k {
case "async":
Expand Down
4 changes: 2 additions & 2 deletions ext4/ext4.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"

"github.com/prometheus/procfs/internal/fs"
"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

const (
Expand Down Expand Up @@ -90,7 +90,7 @@ func (fs FS) ProcStat() ([]*Stats, error) {
"msg_count": &s.Messages,
} {
var val uint64
val, err = util.ReadUintFromFile(fs.sys.Path(sysFSPath, sysFSExt4Path, name, file))
val, err = parsers.ReadUintFromFile(fs.sys.Path(sysFSPath, sysFSExt4Path, name, file))
if err == nil {
*p = val
}
Expand Down
4 changes: 2 additions & 2 deletions fscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strconv"
"strings"

"github.com/prometheus/procfs/internal/util"
"github.com/prometheus/procfs/internal/parsers"
)

// Fscacheinfo represents fscache statistics.
Expand Down Expand Up @@ -229,7 +229,7 @@ type Fscacheinfo struct {
// Fscacheinfo returns information about current fscache statistics.
// See https://www.kernel.org/doc/Documentation/filesystems/caching/fscache.txt
func (fs FS) Fscacheinfo() (Fscacheinfo, error) {
b, err := util.ReadFileNoStat(fs.proc.Path("fs/fscache/stats"))
b, err := parsers.ReadFileNoStat(fs.proc.Path("fs/fscache/stats"))
if err != nil {
return Fscacheinfo{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/util/parse.go → internal/parsers/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package parsers

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion internal/util/readfile.go → internal/parsers/readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package parsers

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

//go:build (linux || darwin) && !appengine

package util
package parsers

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

//go:build (linux && appengine) || (!linux && !darwin)

package util
package parsers

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package parsers

import (
"strconv"
Expand Down
Loading