Skip to content

Commit

Permalink
chore: optimize registry collector (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke authored Oct 10, 2024
1 parent f46f908 commit 22fdb33
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/perfdata/v1/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Collector) Describe() map[string]string {
}

func (c *Collector) Collect() (map[string]map[string]perftypes.CounterValues, error) {
perfObjects, err := QueryPerformanceData(c.query)
perfObjects, err := QueryPerformanceData(c.query, c.object)
if err != nil {
return nil, fmt.Errorf("QueryPerformanceData: %w", err)
}
Expand Down
17 changes: 14 additions & 3 deletions internal/perfdata/v1/perflib.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ The query can be any of the following:
Many objects have dependencies - if you query one of them, you often get back
more than you asked for.
*/
func QueryPerformanceData(query string) ([]*PerfObject, error) {
func QueryPerformanceData(query string, counterName string) ([]*PerfObject, error) {
buffer, err := queryRawData(query)
if err != nil {
return nil, err
Expand All @@ -297,6 +297,8 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
// Parse the performance data

numObjects := int(header.NumObjectTypes)
numFilteredObjects := 0

objects := make([]*PerfObject, numObjects)

objOffset := int64(header.HeaderLength)
Expand All @@ -314,6 +316,14 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
return nil, err
}

perfCounterName := obj.LookupName()

if counterName != "" && perfCounterName != counterName {
objOffset += int64(obj.TotalByteLength)

continue
}

numCounterDefs := int(obj.NumCounters)
numInstances := int(obj.NumInstances)

Expand All @@ -328,7 +338,7 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
counterDefs := make([]*PerfCounterDef, numCounterDefs)

objects[i] = &PerfObject{
Name: obj.LookupName(),
Name: perfCounterName,
NameIndex: uint(obj.ObjectNameTitleIndex),
Instances: instances,
CounterDefs: counterDefs,
Expand Down Expand Up @@ -410,9 +420,10 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {

// Next perfObjectType
objOffset += int64(obj.TotalByteLength)
numFilteredObjects++
}

return objects, nil
return objects[:numFilteredObjects], nil
}

func parseCounterBlock(b []byte, r io.ReadSeeker, pos int64, defs []*PerfCounterDef) (int64, []*PerfCounter, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/perfdata/v1/perflib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (

func BenchmarkQueryPerformanceData(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = QueryPerformanceData("Global")
_, _ = QueryPerformanceData("Global", "")
}
}
2 changes: 1 addition & 1 deletion internal/perfdata/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func MapCounterToIndex(name string) string {
}

func GetPerflibSnapshot(objNames string) (map[string]*PerfObject, error) {
objects, err := QueryPerformanceData(objNames)
objects, err := QueryPerformanceData(objNames, "")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 22fdb33

Please sign in to comment.