@@ -73,78 +73,23 @@ func NewReader(config Config) (*Reader, error) {
73
73
config : config ,
74
74
}
75
75
r .mapCounters (config )
76
- for i , counter := range r .counters {
77
- r .counters [i ].ChildQueries = []string {}
78
- childQueries , err := query .GetCounterPaths (counter .QueryName )
79
- if err != nil {
80
- if config .IgnoreNECounters {
81
- switch err {
82
- case pdh .PDH_CSTATUS_NO_COUNTER , pdh .PDH_CSTATUS_NO_COUNTERNAME ,
83
- pdh .PDH_CSTATUS_NO_INSTANCE , pdh .PDH_CSTATUS_NO_OBJECT :
84
- r .log .Infow ("Ignoring non existent counter" , "error" , err ,
85
- logp .Namespace ("perfmon" ), "query" , counter .QueryName )
86
- continue
87
- }
88
- } else {
89
- query .Close ()
90
- return nil , errors .Wrapf (err , `failed to expand counter (query="%v")` , counter .QueryName )
91
- }
92
- }
93
- // check if the pdhexpandcounterpath/pdhexpandwildcardpath functions have expanded the counter successfully.
94
- if len (childQueries ) == 0 || (len (childQueries ) == 1 && strings .Contains (childQueries [0 ], "*" )) {
95
- // covering cases when PdhExpandWildCardPathW returns no counter paths or is unable to expand and the ignore_non_existent_counters flag is set
96
- if config .IgnoreNECounters {
97
- r .log .Infow ("Ignoring non existent counter" , "initial query" , counter .QueryName ,
98
- logp .Namespace ("perfmon" ), "expanded query" , childQueries )
99
- continue
100
- }
101
- return nil , errors .Errorf (`failed to expand counter (query="%v"), no error returned` , counter .QueryName )
102
- }
103
- for _ , v := range childQueries {
104
- if err := query .AddCounter (v , counter .InstanceName , counter .Format , len (childQueries ) > 1 ); err != nil {
105
- return nil , errors .Wrapf (err , `failed to add counter (query="%v")` , counter .QueryName )
106
- }
107
- r .counters [i ].ChildQueries = append (r .counters [i ].ChildQueries , v )
108
- }
76
+ _ , err := r .getCounterPaths ()
77
+ if err != nil {
78
+ return nil , err
109
79
}
110
80
return r , nil
111
81
}
112
82
113
83
// RefreshCounterPaths will recheck for any new instances and add them to the counter list
114
84
func (re * Reader ) RefreshCounterPaths () error {
115
- var newCounters []string
116
- for i , counter := range re .counters {
117
- re .counters [i ].ChildQueries = []string {}
118
- childQueries , err := re .query .GetCounterPaths (counter .QueryName )
119
- if err != nil {
120
- if re .config .IgnoreNECounters {
121
- switch err {
122
- case pdh .PDH_CSTATUS_NO_COUNTER , pdh .PDH_CSTATUS_NO_COUNTERNAME ,
123
- pdh .PDH_CSTATUS_NO_INSTANCE , pdh .PDH_CSTATUS_NO_OBJECT :
124
- re .log .Infow ("Ignoring non existent counter" , "error" , err ,
125
- logp .Namespace ("perfmon" ), "query" , counter .QueryName )
126
- continue
127
- }
128
- } else {
129
- return errors .Wrapf (err , `failed to expand counter (query="%v")` , counter .QueryName )
130
- }
131
- }
132
- newCounters = append (newCounters , childQueries ... )
133
- // there are cases when the ExpandWildCardPath will retrieve a successful status but not an expanded query so we need to check for the size of the list
134
- if err == nil && len (childQueries ) >= 1 && ! strings .Contains (childQueries [0 ], "*" ) {
135
- for _ , v := range childQueries {
136
- if err := re .query .AddCounter (v , counter .InstanceName , counter .Format , len (childQueries ) > 1 ); err != nil {
137
- return errors .Wrapf (err , "failed to add counter (query='%v')" , counter .QueryName )
138
- }
139
- re .counters [i ].ChildQueries = append (re .counters [i ].ChildQueries , v )
140
- }
141
- }
85
+ newCounters , err := re .getCounterPaths ()
86
+ if err != nil {
87
+ return errors .Wrap (err , "failed retrieving counter paths" )
142
88
}
143
- err : = re .query .RemoveUnusedCounters (newCounters )
89
+ err = re .query .RemoveUnusedCounters (newCounters )
144
90
if err != nil {
145
91
return errors .Wrap (err , "failed removing unused counter values" )
146
92
}
147
-
148
93
return nil
149
94
}
150
95
@@ -184,6 +129,39 @@ func (re *Reader) Close() error {
184
129
return re .query .Close ()
185
130
}
186
131
132
+ // getCounterPaths func will process the counter paths based on the configuration options entered
133
+ func (re * Reader ) getCounterPaths () ([]string , error ) {
134
+ var newCounters []string
135
+ for i , counter := range re .counters {
136
+ re .counters [i ].ChildQueries = []string {}
137
+ childQueries , err := re .query .GetCounterPaths (counter .QueryName )
138
+ if err != nil {
139
+ if re .config .IgnoreNECounters {
140
+ switch err {
141
+ case pdh .PDH_CSTATUS_NO_COUNTER , pdh .PDH_CSTATUS_NO_COUNTERNAME ,
142
+ pdh .PDH_CSTATUS_NO_INSTANCE , pdh .PDH_CSTATUS_NO_OBJECT :
143
+ re .log .Infow ("Ignoring non existent counter" , "error" , err ,
144
+ logp .Namespace ("perfmon" ), "query" , counter .QueryName )
145
+ continue
146
+ }
147
+ } else {
148
+ return newCounters , errors .Wrapf (err , `failed to expand counter (query="%v")` , counter .QueryName )
149
+ }
150
+ }
151
+ newCounters = append (newCounters , childQueries ... )
152
+ // there are cases when the ExpandWildCardPath will retrieve a successful status but not an expanded query so we need to check for the size of the list
153
+ if err == nil && len (childQueries ) >= 1 && ! strings .Contains (childQueries [0 ], "*" ) {
154
+ for _ , v := range childQueries {
155
+ if err := re .query .AddCounter (v , counter .InstanceName , counter .Format , isWildcard (childQueries , counter .InstanceName )); err != nil {
156
+ return newCounters , errors .Wrapf (err , "failed to add counter (query='%v')" , counter .QueryName )
157
+ }
158
+ re .counters [i ].ChildQueries = append (re .counters [i ].ChildQueries , v )
159
+ }
160
+ }
161
+ }
162
+ return newCounters , nil
163
+ }
164
+
187
165
func (re * Reader ) getCounter (query string ) (bool , PerfCounter ) {
188
166
for _ , counter := range re .counters {
189
167
for _ , childQuery := range counter .ChildQueries {
@@ -310,3 +288,14 @@ func replaceUpperCase(src string) string {
310
288
return newStr
311
289
})
312
290
}
291
+
292
+ // isWildcard function checks if users has configured a wildcard inside the instance configuration option and if the wildcard has been resulted in a valid number of queries
293
+ func isWildcard (queries []string , instance string ) bool {
294
+ if len (queries ) > 1 {
295
+ return true
296
+ }
297
+ if len (queries ) == 1 && strings .Contains (instance , "*" ) {
298
+ return true
299
+ }
300
+ return false
301
+ }
0 commit comments