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

add feature to ignore disk labels by regexp in 'disk' of system metrics #898

Merged
merged 7 commits into from
May 18, 2023
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
2 changes: 1 addition & 1 deletion command/command_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func metricsGenerators(conf *config.Config) []metrics.Generator {
&metricsLinux.CPUUsageGenerator{Interval: metricsInterval},
&metricsLinux.MemoryGenerator{},
&metrics.InterfaceGenerator{IgnoreRegexp: conf.Interfaces.Ignore.Regexp, Interval: metricsInterval},
&metricsLinux.DiskGenerator{Interval: metricsInterval, UseMountpoint: conf.Filesystems.UseMountpoint},
&metricsLinux.DiskGenerator{IgnoreRegexp: conf.Disks.Ignore.Regexp, Interval: metricsInterval, UseMountpoint: conf.Filesystems.UseMountpoint},
&metrics.FilesystemGenerator{IgnoreRegexp: conf.Filesystems.Ignore.Regexp, UseMountpoint: conf.Filesystems.UseMountpoint},
}

Expand Down
2 changes: 1 addition & 1 deletion command/command_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func metricsGenerators(conf *config.Config) []metrics.Generator {
if g, err = metricsWindows.NewInterfaceGenerator(conf.Interfaces.Ignore.Regexp, metricsInterval); err == nil {
generators = append(generators, g)
}
if g, err = metricsWindows.NewDiskGenerator(metricsInterval); err == nil {
if g, err = metricsWindows.NewDiskGenerator(conf.Disks.Ignore.Regexp, metricsInterval); err == nil {
generators = append(generators, g)
}

Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Config struct {
Diagnostic bool `toml:"diagnostic"`
DisplayName string `toml:"display_name"`
HostStatus HostStatus `toml:"host_status" conf:"parent"`
Disks Disks `toml:"disks" conf:"parent"`
Filesystems Filesystems `toml:"filesystems" conf:"parent"`
Interfaces Interfaces `toml:"interfaces" conf:"parent"`
HTTPProxy string `toml:"http_proxy"`
Expand Down Expand Up @@ -370,6 +371,11 @@ type HostStatus struct {
OnStop string `toml:"on_stop"`
}

// Disks configure disks related settings
type Disks struct {
Ignore Regexpwrapper `toml:"ignore"`
}

// Filesystems configure filesystem related settings
type Filesystems struct {
Ignore Regexpwrapper `toml:"ignore"`
Expand Down
9 changes: 7 additions & 2 deletions metrics/linux/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Note that there are more columns in Linux 4.18+, see https://github.com/torvalds

// DiskGenerator XXX
type DiskGenerator struct {
IgnoreRegexp *regexp.Regexp
Interval time.Duration
UseMountpoint bool
}
Expand Down Expand Up @@ -102,10 +103,10 @@ func (g *DiskGenerator) collectDiskstatValues() (metrics.Values, error) {
diskLogger.Warningf("Failed to prepare device name mapping: %s", err)
}
}
return parseDiskStats(out, nameMapping)
return g.parseDiskStats(out, nameMapping)
}

func parseDiskStats(out []byte, mapping map[string]string) (metrics.Values, error) {
func (g *DiskGenerator) parseDiskStats(out []byte, mapping map[string]string) (metrics.Values, error) {
lineScanner := bufio.NewScanner(bytes.NewReader(out))
results := make(map[string]float64)
for lineScanner.Scan() {
Expand All @@ -127,6 +128,10 @@ func parseDiskStats(out []byte, mapping map[string]string) (metrics.Values, erro
if strings.HasPrefix(deviceLabel, "dm-") {
continue
}
if g.IgnoreRegexp != nil && g.IgnoreRegexp.MatchString(deviceLabel) {
continue
}

mountpoint, exists := mapping[device]
if exists {
deviceLabel = util.SanitizeMetricKey(mountpoint)
Expand Down
11 changes: 7 additions & 4 deletions metrics/linux/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ func TestDiskGenerator(t *testing.T) {
}

func TestParseDiskStats(t *testing.T) {
g := &DiskGenerator{Interval: 1 * time.Second}
// insert empty line intentionally
out := []byte(`202 1 xvda1 750193 3037 28116978 368712 16600606 7233846 424712632 23987908 0 2355636 24345740

202 2 xvda2 1641 9310 87552 1252 6365 3717 80664 24192 0 15040 25428`)

var emptyMapping map[string]string
result, err := parseDiskStats(out, emptyMapping)
result, err := g.parseDiskStats(out, emptyMapping)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestParseDiskStats(t *testing.T) {
"xvda1": "_some_mount",
"xvda3": "_nonused_mount",
}
resultWithMapping, err := parseDiskStats(out, mapping)
resultWithMapping, err := g.parseDiskStats(out, mapping)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
Expand Down Expand Up @@ -114,12 +115,13 @@ func TestParseDiskStats(t *testing.T) {
}

func TestParseDiskStats_MoreFields(t *testing.T) {
g := &DiskGenerator{Interval: 1 * time.Second}
// There are 18 columns since Linux 4.18+.
out := []byte(`202 1 xvda1 750193 3037 28116978 368712 16600606 7233846 424712632 23987908 0 2355636 24345740 0 0 0 0
7 0 loop0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0`)

var emptyMapping map[string]string
result, err := parseDiskStats(out, emptyMapping)
result, err := g.parseDiskStats(out, emptyMapping)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
Expand Down Expand Up @@ -154,12 +156,13 @@ func TestParseDiskStats_MoreFields(t *testing.T) {
}

func TestParseDiskStats_ShouldIgnoreIfAllFieldsAreZeroOrSpecificDeviceName(t *testing.T) {
g := &DiskGenerator{Interval: 1 * time.Second}
out := []byte(`253 0 dm-0 2 0 40 0 314 0 2512 2136 0 236 2136
253 1 dm-1 964 0 57886 944 74855 0 644512 5421192 0 1580 5422136
7 0 loop0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0`)

var emptyMapping map[string]string
result, err := parseDiskStats(out, emptyMapping)
result, err := g.parseDiskStats(out, emptyMapping)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
Expand Down
11 changes: 8 additions & 3 deletions metrics/windows/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package windows
import (
"errors"
"fmt"
"regexp"
"time"

"github.com/mackerelio/golib/logging"
Expand All @@ -15,14 +16,15 @@ import (

// DiskGenerator XXX
type DiskGenerator struct {
Interval time.Duration
IgnoreRegexp *regexp.Regexp
Interval time.Duration
}

var diskLogger = logging.GetLogger("metrics.disk")

// NewDiskGenerator XXX
func NewDiskGenerator(interval time.Duration) (*DiskGenerator, error) {
return &DiskGenerator{interval}, nil
func NewDiskGenerator(ignoreReg *regexp.Regexp, interval time.Duration) (*DiskGenerator, error) {
return &DiskGenerator{ignoreReg, interval}, nil
}

type win32PerfFormattedDataPerfDiskPhysicalDisk struct {
Expand All @@ -43,6 +45,9 @@ func (g *DiskGenerator) Generate() (metrics.Values, error) {
results := make(map[string]float64)
for _, record := range records {
name := record.Name
if g.IgnoreRegexp != nil && g.IgnoreRegexp.MatchString(name) {
continue
}
// Collect metrics for only drives
if len(name) != 2 || name[1] != ':' {
continue
Expand Down
2 changes: 1 addition & 1 deletion metrics/windows/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestDiskGenerator(t *testing.T) {
g, err := NewDiskGenerator(1 * time.Second)
g, err := NewDiskGenerator(nil, 1*time.Second)
if err != nil {
t.Errorf("should not raise error: %v", err)
}
Expand Down