diff --git a/receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper.go b/receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper.go index fa394f70d93..bfd18610d13 100644 --- a/receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper.go +++ b/receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper.go @@ -28,11 +28,18 @@ import ( const totalInstanceName = "_Total" +// PerfCounterScraper scrapes performance counter data. type PerfCounterScraper interface { + // Initialize initializes the PerfCounterScraper so that subsequent calls + // to Scrape will return performance counter data for the specified set. + // of objects Initialize(objects ...string) error + // Scrape returns performance data for the initialized objects. Scrape() (PerfDataCollection, error) } +// PerfLibScraper is an implementation of PerfCounterScraper that uses +// perflib to scrape performance counter data. type PerfLibScraper struct { objectIndices string } @@ -53,7 +60,7 @@ func (p *PerfLibScraper) Initialize(objects ...string) error { objectIndicesMap[index] = struct{}{} } - // convert to space-separated string + // convert to a space-separated string objectIndicesSlice := make([]string, 0, len(objectIndicesMap)) for k := range objectIndicesMap { objectIndicesSlice = append(objectIndicesSlice, strconv.Itoa(int(k))) @@ -76,7 +83,10 @@ func (p *PerfLibScraper) Scrape() (PerfDataCollection, error) { return perfDataCollection{perfObject: indexed}, nil } +// PerfDataCollection represents a collection of perf counter data. type PerfDataCollection interface { + // GetObject returns the perf counter data associated with the specified object, + // or returns an error if no data exists for this object name. GetObject(objectName string) (PerfDataObject, error) } @@ -93,8 +103,15 @@ func (p perfDataCollection) GetObject(objectName string) (PerfDataObject, error) return perfDataObject{obj}, nil } +// PerfDataCollection represents a collection of perf counter values +// and associated instances. type PerfDataObject interface { + // Filter filters the perf counter data to only retain data related to + // relevant instances based on the supplied parameters. Filter(includeFS, excludeFS filterset.FilterSet, includeTotal bool) + // GetValues returns the performance counter data associated with the specified + // counters, or returns an error if any of the specified counter names do not + // exist. GetValues(counterNames ...string) ([]*CounterValues, error) } @@ -125,6 +142,7 @@ func includeDevice(deviceName string, includeFS, excludeFS filterset.FilterSet, (excludeFS == nil || !excludeFS.Matches(deviceName)) } +// CounterValues represents a set of perf counter values for a given instance. type CounterValues struct { InstanceName string Values map[string]int64 diff --git a/receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper_mock.go b/receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper_mock.go index b9ea898d8df..4b47acf8706 100644 --- a/receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper_mock.go +++ b/receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper_mock.go @@ -20,23 +20,27 @@ import ( "go.opentelemetry.io/collector/internal/processor/filterset" ) -// MockPerfCounterScraperError returns the supplied errors when Scrape, GetObject, -// or GetValues are called. - +// MockPerfCounterScraperError is an implementation of PerfCounterScraper that returns +// the supplied errors when Scrape, GetObject, or GetValues are called. type MockPerfCounterScraperError struct { scrapeErr error getObjectErr error getValuesErr error } +// NewMockPerfCounterScraperError returns a MockPerfCounterScraperError that will return +// the specified errors on subsequent function calls. func NewMockPerfCounterScraperError(scrapeErr, getObjectErr, getValuesErr error) *MockPerfCounterScraperError { return &MockPerfCounterScraperError{scrapeErr: scrapeErr, getObjectErr: getObjectErr, getValuesErr: getValuesErr} } +// Initialize is a no-op func (p *MockPerfCounterScraperError) Initialize(objects ...string) error { return nil } +// Scrape returns the specified scrapeErr or an object that will return a subsequent error +// if scrapeErr is nil func (p *MockPerfCounterScraperError) Scrape() (PerfDataCollection, error) { if p.scrapeErr != nil { return nil, p.scrapeErr @@ -50,6 +54,8 @@ type mockPerfDataCollectionError struct { getValuesErr error } +// GetObject returns the specified getObjectErr or an object that will return a subsequent +// error if getObjectErr is nil func (p mockPerfDataCollectionError) GetObject(objectName string) (PerfDataObject, error) { if p.getObjectErr != nil { return nil, p.getObjectErr @@ -62,9 +68,11 @@ type mockPerfDataObjectError struct { getValuesErr error } +// Filter is a no-op func (obj mockPerfDataObjectError) Filter(includeFS, excludeFS filterset.FilterSet, includeTotal bool) { } +// GetValues returns the specified getValuesErr func (obj mockPerfDataObjectError) GetValues(counterNames ...string) ([]*CounterValues, error) { return nil, obj.getValuesErr }