-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
1/3 Add new perfcounters package that uses perflib #1835
Merged
bogdandrutu
merged 2 commits into
open-telemetry:master
from
james-bebbington:hostmetrics-perflib
Sep 26, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_notwindows.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !windows | ||
|
||
package perfcounters |
202 changes: 202 additions & 0 deletions
202
receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build windows | ||
|
||
package perfcounters | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/leoluk/perflib_exporter/perflib" | ||
|
||
"go.opentelemetry.io/collector/internal/processor/filterset" | ||
) | ||
|
||
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 | ||
} | ||
|
||
func (p *PerfLibScraper) Initialize(objects ...string) error { | ||
// "Counter 009" reads perf counter names in English. | ||
// This is always present regardless of the OS language. | ||
nameTable := perflib.QueryNameTable("Counter 009") | ||
|
||
// lookup object indices from name table | ||
objectIndicesMap := map[uint32]struct{}{} | ||
for _, name := range objects { | ||
index := nameTable.LookupIndex(name) | ||
if index == 0 { | ||
return fmt.Errorf("Failed to retrieve perf counter object %q", name) | ||
} | ||
|
||
objectIndicesMap[index] = struct{}{} | ||
} | ||
|
||
// convert to a space-separated string | ||
objectIndicesSlice := make([]string, 0, len(objectIndicesMap)) | ||
for k := range objectIndicesMap { | ||
objectIndicesSlice = append(objectIndicesSlice, strconv.Itoa(int(k))) | ||
} | ||
p.objectIndices = strings.Join(objectIndicesSlice, " ") | ||
return nil | ||
} | ||
|
||
func (p *PerfLibScraper) Scrape() (PerfDataCollection, error) { | ||
objects, err := perflib.QueryPerformanceData(p.objectIndices) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
indexed := make(map[string]*perflib.PerfObject) | ||
for _, obj := range objects { | ||
indexed[obj.Name] = obj | ||
} | ||
|
||
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) | ||
} | ||
|
||
type perfDataCollection struct { | ||
perfObject map[string]*perflib.PerfObject | ||
} | ||
|
||
func (p perfDataCollection) GetObject(objectName string) (PerfDataObject, error) { | ||
obj, ok := p.perfObject[objectName] | ||
if !ok { | ||
return nil, fmt.Errorf("Unable to find object %q", objectName) | ||
} | ||
|
||
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) | ||
} | ||
|
||
type perfDataObject struct { | ||
*perflib.PerfObject | ||
} | ||
|
||
func (obj perfDataObject) Filter(includeFS, excludeFS filterset.FilterSet, includeTotal bool) { | ||
if includeFS == nil && excludeFS == nil && includeTotal { | ||
return | ||
} | ||
|
||
filteredDevices := make([]*perflib.PerfInstance, 0, len(obj.Instances)) | ||
for _, device := range obj.Instances { | ||
if includeDevice(device.Name, includeFS, excludeFS, includeTotal) { | ||
filteredDevices = append(filteredDevices, device) | ||
} | ||
} | ||
obj.Instances = filteredDevices | ||
} | ||
|
||
func includeDevice(deviceName string, includeFS, excludeFS filterset.FilterSet, includeTotal bool) bool { | ||
if deviceName == totalInstanceName { | ||
return includeTotal | ||
} | ||
|
||
return (includeFS == nil || includeFS.Matches(deviceName)) && | ||
(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 | ||
} | ||
|
||
type counterIndex struct { | ||
index int | ||
name string | ||
} | ||
|
||
func (obj perfDataObject) GetValues(counterNames ...string) ([]*CounterValues, error) { | ||
counterIndices := make([]counterIndex, 0, len(counterNames)) | ||
for idx, counter := range obj.CounterDefs { | ||
// "Base" values give the value of a related counter that pdh.dll uses to compute the derived | ||
// value for this counter. We only care about raw values so ignore base values. See | ||
// https://docs.microsoft.com/en-us/windows/win32/perfctrs/retrieving-counter-data. | ||
if counter.IsBaseValue { | ||
continue | ||
} | ||
|
||
for _, counterName := range counterNames { | ||
if counter.Name == counterName { | ||
counterIndices = append(counterIndices, counterIndex{index: idx, name: counter.Name}) | ||
break | ||
} | ||
} | ||
} | ||
|
||
if len(counterIndices) < len(counterNames) { | ||
return nil, fmt.Errorf("Unable to find counters %q in object %q", missingCounterNames(counterNames, counterIndices), obj.Name) | ||
} | ||
|
||
values := make([]*CounterValues, len(obj.Instances)) | ||
for i, instance := range obj.Instances { | ||
instanceValues := &CounterValues{InstanceName: instance.Name, Values: make(map[string]int64, len(counterIndices))} | ||
for _, counter := range counterIndices { | ||
instanceValues.Values[counter.name] = instance.Counters[counter.index].Value | ||
} | ||
values[i] = instanceValues | ||
} | ||
return values, nil | ||
} | ||
|
||
func missingCounterNames(counterNames []string, counterIndices []counterIndex) []string { | ||
matchedCounters := make(map[string]struct{}, len(counterIndices)) | ||
for _, counter := range counterIndices { | ||
matchedCounters[counter.name] = struct{}{} | ||
} | ||
|
||
counters := make([]string, 0, len(counterNames)-len(matchedCounters)) | ||
for _, counter := range counterNames { | ||
if _, ok := matchedCounters[counter]; !ok { | ||
counters = append(counters, counter) | ||
} | ||
} | ||
return counters | ||
} |
78 changes: 78 additions & 0 deletions
78
receiver/hostmetricsreceiver/internal/perfcounters/perfcounter_scraper_mock.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build windows | ||
|
||
package perfcounters | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/internal/processor/filterset" | ||
) | ||
|
||
// 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 | ||
} | ||
|
||
return mockPerfDataCollectionError{getObjectErr: p.getObjectErr, getValuesErr: p.getValuesErr}, nil | ||
} | ||
|
||
type mockPerfDataCollectionError struct { | ||
getObjectErr error | ||
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 | ||
} | ||
|
||
return mockPerfDataObjectError{getValuesErr: p.getValuesErr}, nil | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is for testing only and no reason to expose it publicly consider to:
Move it to a file that ends with _test.
Use lower case letter to not expose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the intent is to keep it public add struct and funcs comments :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do want to keep it public as the intent is for this Mock to be used by several different Scraper packages. Added a bunch of comments