performancecounter: support yaml documents and tolerate collector errors (#1809)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2024-12-09 19:14:26 +01:00
committed by GitHub
parent a9698e27bf
commit eea5a50d5c
14 changed files with 363 additions and 129 deletions

View File

@@ -206,8 +206,9 @@ func (c *Collection) collectCollector(ch chan<- prometheus.Metric, logger *slog.
if err != nil && !errors.Is(err, perfdata.ErrNoData) && !errors.Is(err, types.ErrNoData) {
loggerFn := logger.Warn
if errors.Is(err, perfdata.ErrPerformanceCounterNotInitialized) || errors.Is(err, mi.MI_RESULT_INVALID_NAMESPACE) {
loggerFn = logger.Debug
err = fmt.Errorf("%w. Check application logs from initialization pharse for more information", err)
}
loggerFn(fmt.Sprintf("collector %s failed after %s, resulting in %d metrics", name, duration, numMetrics),

View File

@@ -74,6 +74,7 @@ import (
"github.com/prometheus-community/windows_exporter/internal/collector/update"
"github.com/prometheus-community/windows_exporter/internal/collector/vmware"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/internal/perfdata"
"github.com/prometheus-community/windows_exporter/internal/types"
"github.com/prometheus/client_golang/prometheus"
)
@@ -196,6 +197,8 @@ func (c *Collection) Enable(enabledCollectors []string) error {
}
// Build To be called by the exporter for collector initialization.
// Instead, fail fast, it will try to build all collectors and return all errors.
// errors are joined with errors.Join.
func (c *Collection) Build(logger *slog.Logger) error {
c.startTime = gotime.Now()
@@ -208,7 +211,6 @@ func (c *Collection) Build(logger *slog.Logger) error {
wg.Add(len(c.collectors))
errCh := make(chan error, len(c.collectors))
errs := make([]error, 0, len(c.collectors))
for _, collector := range c.collectors {
go func() {
@@ -224,7 +226,20 @@ func (c *Collection) Build(logger *slog.Logger) error {
close(errCh)
errs := make([]error, 0, len(c.collectors))
for err := range errCh {
if errors.Is(err, perfdata.ErrNoData) ||
errors.Is(err, perfdata.NewPdhError(perfdata.PdhCstatusNoObject)) ||
errors.Is(err, perfdata.NewPdhError(perfdata.PdhCstatusNoCounter)) ||
errors.Is(err, mi.MI_RESULT_INVALID_NAMESPACE) {
logger.Warn("couldn't initialize collector",
slog.Any("err", err),
)
continue
}
errs = append(errs, err)
}

View File

@@ -128,6 +128,9 @@ var BuildersWithFlags = map[string]BuilderWithFlags[Collector]{
vmware.Name: NewBuilderWithFlags(vmware.NewWithFlags),
}
// Available returns a sorted list of available collectors.
//
//goland:noinspection GoUnusedExportedFunction
func Available() []string {
return slices.Sorted(maps.Keys(BuildersWithFlags))
}