mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-25 14:16:36 +00:00
chore: optimize registry collector (#1683)
This commit is contained in:
@@ -39,7 +39,7 @@ func (c *Collector) Describe() map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collector) Collect() (map[string]map[string]perftypes.CounterValues, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("QueryPerformanceData: %w", err)
|
return nil, fmt.Errorf("QueryPerformanceData: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
Many objects have dependencies - if you query one of them, you often get back
|
||||||
more than you asked for.
|
more than you asked for.
|
||||||
*/
|
*/
|
||||||
func QueryPerformanceData(query string) ([]*PerfObject, error) {
|
func QueryPerformanceData(query string, counterName string) ([]*PerfObject, error) {
|
||||||
buffer, err := queryRawData(query)
|
buffer, err := queryRawData(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -297,6 +297,8 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
|
|||||||
// Parse the performance data
|
// Parse the performance data
|
||||||
|
|
||||||
numObjects := int(header.NumObjectTypes)
|
numObjects := int(header.NumObjectTypes)
|
||||||
|
numFilteredObjects := 0
|
||||||
|
|
||||||
objects := make([]*PerfObject, numObjects)
|
objects := make([]*PerfObject, numObjects)
|
||||||
|
|
||||||
objOffset := int64(header.HeaderLength)
|
objOffset := int64(header.HeaderLength)
|
||||||
@@ -314,6 +316,14 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
perfCounterName := obj.LookupName()
|
||||||
|
|
||||||
|
if counterName != "" && perfCounterName != counterName {
|
||||||
|
objOffset += int64(obj.TotalByteLength)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
numCounterDefs := int(obj.NumCounters)
|
numCounterDefs := int(obj.NumCounters)
|
||||||
numInstances := int(obj.NumInstances)
|
numInstances := int(obj.NumInstances)
|
||||||
|
|
||||||
@@ -328,7 +338,7 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
|
|||||||
counterDefs := make([]*PerfCounterDef, numCounterDefs)
|
counterDefs := make([]*PerfCounterDef, numCounterDefs)
|
||||||
|
|
||||||
objects[i] = &PerfObject{
|
objects[i] = &PerfObject{
|
||||||
Name: obj.LookupName(),
|
Name: perfCounterName,
|
||||||
NameIndex: uint(obj.ObjectNameTitleIndex),
|
NameIndex: uint(obj.ObjectNameTitleIndex),
|
||||||
Instances: instances,
|
Instances: instances,
|
||||||
CounterDefs: counterDefs,
|
CounterDefs: counterDefs,
|
||||||
@@ -410,9 +420,10 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
|
|||||||
|
|
||||||
// Next perfObjectType
|
// Next perfObjectType
|
||||||
objOffset += int64(obj.TotalByteLength)
|
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) {
|
func parseCounterBlock(b []byte, r io.ReadSeeker, pos int64, defs []*PerfCounterDef) (int64, []*PerfCounter, error) {
|
||||||
|
|||||||
@@ -6,6 +6,6 @@ import (
|
|||||||
|
|
||||||
func BenchmarkQueryPerformanceData(b *testing.B) {
|
func BenchmarkQueryPerformanceData(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
_, _ = QueryPerformanceData("Global")
|
_, _ = QueryPerformanceData("Global", "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ func MapCounterToIndex(name string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetPerflibSnapshot(objNames string) (map[string]*PerfObject, error) {
|
func GetPerflibSnapshot(objNames string) (map[string]*PerfObject, error) {
|
||||||
objects, err := QueryPerformanceData(objNames)
|
objects, err := QueryPerformanceData(objNames, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user