Skip to content

Commit

Permalink
chore: optimize registry collector
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
  • Loading branch information
jkroepke committed Oct 10, 2024
1 parent f46f908 commit 4c47c9a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 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
19 changes: 15 additions & 4 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 All @@ -345,7 +355,7 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
}

counterDefs[i] = &PerfCounterDef{
Name: def.LookupName(),
Name: perfCounterName,
NameIndex: uint(def.CounterNameTitleIndex),
rawData: def,

Expand Down Expand Up @@ -410,9 +420,10 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {

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

Check failure on line 423 in internal/perfdata/v1/perflib.go

View workflow job for this annotation

GitHub Actions / lint

increment-decrement: should replace numFilteredObjects += 1 with numFilteredObjects++ (revive)
}

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 4c47c9a

Please sign in to comment.