Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
james-bebbington committed Sep 25, 2020
1 parent 30e1e18 commit d87206b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)))
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit d87206b

Please sign in to comment.