Skip to content

Commit

Permalink
Update hostmetrics disk scraper to use new perfcounters package
Browse files Browse the repository at this point in the history
  • Loading branch information
james-bebbington committed Sep 26, 2020
1 parent d87206b commit a645c76
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 402 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,25 @@ package diskscraper
import (
"context"
"errors"
"runtime"
"testing"

"github.com/shirou/gopsutil/disk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/consumer/pdata"
)

func TestScrapeMetrics_Others(t *testing.T) {
type testCase struct {
name string
bootTimeFunc func() (uint64, error)
ioCountersFunc func(names ...string) (map[string]disk.IOCountersStat, error)
expectedStartTime pdata.TimestampUnixNano
initializationErr string
expectedErr string
name string
ioCountersFunc func(names ...string) (map[string]disk.IOCountersStat, error)
expectedErr string
}

testCases := []testCase{
{
name: "Validate Start Time",
bootTimeFunc: func() (uint64, error) { return 100, nil },
expectedStartTime: 100 * 1e9,
},
{
name: "Boot Time Error",
bootTimeFunc: func() (uint64, error) { return 0, errors.New("err1") },
initializationErr: "err1",
},
{
name: "Error",
ioCountersFunc: func(names ...string) (map[string]disk.IOCountersStat, error) { return nil, errors.New("err2") },
expectedErr: "err2",
ioCountersFunc: func(names ...string) (map[string]disk.IOCountersStat, error) { return nil, errors.New("err1") },
expectedErr: "err1",
},
}

Expand All @@ -62,37 +46,16 @@ func TestScrapeMetrics_Others(t *testing.T) {
scraper, err := newDiskScraper(context.Background(), &Config{})
require.NoError(t, err, "Failed to create disk scraper: %v", err)

if test.bootTimeFunc != nil {
scraper.bootTime = test.bootTimeFunc
}
if test.ioCountersFunc != nil {
scraper.ioCounters = test.ioCountersFunc
}

err = scraper.Initialize(context.Background())
if test.initializationErr != "" {
assert.EqualError(t, err, test.initializationErr)
return
}
require.NoError(t, err, "Failed to initialize disk scraper: %v", err)
defer func() { assert.NoError(t, scraper.Close(context.Background())) }()

metrics, err := scraper.ScrapeMetrics(context.Background())
if test.expectedErr != "" {
assert.EqualError(t, err, test.expectedErr)
return
}

require.NoError(t, err, "Failed to scrape metrics: %v", err)

assertInt64DiskMetricValid(t, metrics.At(0), diskIODescriptor, test.expectedStartTime)
assertInt64DiskMetricValid(t, metrics.At(1), diskOpsDescriptor, test.expectedStartTime)
assertDoubleDiskMetricValid(t, metrics.At(2), diskTimeDescriptor, test.expectedStartTime)
assertDiskPendingOperationsMetricValid(t, metrics.At(3))

if runtime.GOOS == "linux" {
assertInt64DiskMetricValid(t, metrics.At(4), diskMergedDescriptor, test.expectedStartTime)
}
_, err = scraper.ScrapeMetrics(context.Background())
assert.EqualError(t, err, test.expectedErr)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package diskscraper

import (
"context"
"errors"
"runtime"
"testing"

Expand All @@ -29,17 +30,31 @@ import (

func TestScrapeMetrics(t *testing.T) {
type testCase struct {
name string
config Config
expectMetrics bool
newErrRegex string
name string
config Config
bootTimeFunc func() (uint64, error)
newErrRegex string
initializationErr string
expectMetrics bool
expectedStartTime pdata.TimestampUnixNano
}

testCases := []testCase{
{
name: "Standard",
expectMetrics: true,
},
{
name: "Validate Start Time",
bootTimeFunc: func() (uint64, error) { return 100, nil },
expectMetrics: true,
expectedStartTime: 100 * 1e9,
},
{
name: "Boot Time Error",
bootTimeFunc: func() (uint64, error) { return 0, errors.New("err1") },
initializationErr: "err1",
},
{
name: "Include Filter that matches nothing",
config: Config{Include: MatchConfig{filterset.Config{MatchType: "strict"}, []string{"@*^#&*$^#)"}}},
Expand Down Expand Up @@ -67,7 +82,15 @@ func TestScrapeMetrics(t *testing.T) {
}
require.NoError(t, err, "Failed to create disk scraper: %v", err)

if test.bootTimeFunc != nil {
scraper.bootTime = test.bootTimeFunc
}

err = scraper.Initialize(context.Background())
if test.initializationErr != "" {
assert.EqualError(t, err, test.initializationErr)
return
}
require.NoError(t, err, "Failed to initialize disk scraper: %v", err)
defer func() { assert.NoError(t, scraper.Close(context.Background())) }()

Expand All @@ -81,13 +104,13 @@ func TestScrapeMetrics(t *testing.T) {

assert.GreaterOrEqual(t, metrics.Len(), 4)

assertInt64DiskMetricValid(t, metrics.At(0), diskIODescriptor, 0)
assertInt64DiskMetricValid(t, metrics.At(1), diskOpsDescriptor, 0)
assertDoubleDiskMetricValid(t, metrics.At(2), diskTimeDescriptor, 0)
assertInt64DiskMetricValid(t, metrics.At(0), diskIODescriptor, test.expectedStartTime)
assertInt64DiskMetricValid(t, metrics.At(1), diskOpsDescriptor, test.expectedStartTime)
assertDoubleDiskMetricValid(t, metrics.At(2), diskTimeDescriptor, test.expectedStartTime)
assertDiskPendingOperationsMetricValid(t, metrics.At(3))

if runtime.GOOS == "linux" {
assertInt64DiskMetricValid(t, metrics.At(4), diskMergedDescriptor, 0)
assertInt64DiskMetricValid(t, metrics.At(4), diskMergedDescriptor, test.expectedStartTime)
}

internal.AssertSameTimeStampForAllMetrics(t, metrics)
Expand Down
Loading

0 comments on commit a645c76

Please sign in to comment.