pdh: release partially initialized collectors in the constructors (#2736)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2026-10-16 09:11:11 +02:00
committed by GitHub
parent 41f8bfb352
commit 126b655afa
8 changed files with 241 additions and 69 deletions
@@ -380,19 +380,15 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric, maxScrapeDuration time.
return g.Wait()
}
// newPerfCollector adds the counters of object to query and retains
// ownership of partially initialized collectors. Missing objects are optional;
// missing individual counters remain errors.
// newPerfCollector adds the counters of object to query. On error,
// pdh.NewQueryCollector has already removed the counters it added. Missing
// objects are optional; missing individual counters remain errors.
func newPerfCollector[T any](logger *slog.Logger, query *pdh.Query, object string) (perfCollector[T], error) {
c, err := pdh.NewQueryCollector[T](logger, query, pdh.CounterTypeRaw, object, pdh.InstancesAll)
if err == nil {
return c, nil
}
if c != nil {
c.Close()
}
if onlyMissingObject(err) {
return unavailablePerfCollector[T]{}, nil
}