feat: Tolerate collector failures (#1769)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2024-11-25 21:27:31 +01:00
committed by GitHub
parent fd76be38e0
commit 1a4c6c5ce7
121 changed files with 1726 additions and 1221 deletions

View File

@@ -119,6 +119,7 @@ type Config struct {
// ConfigDefaults Is an interface to be used by the external libraries. It holds all ConfigDefaults form all collectors
//
//nolint:gochecknoglobals
//goland:noinspection GoUnusedGlobalVariable
var ConfigDefaults = Config{
AD: ad.ConfigDefaults,

View File

@@ -76,6 +76,7 @@ func NewBuilderWithFlags[C Collector](fn BuilderWithFlags[C]) BuilderWithFlags[C
}
}
//nolint:gochecknoglobals
var BuildersWithFlags = map[string]BuilderWithFlags[Collector]{
ad.Name: NewBuilderWithFlags(ad.NewWithFlags),
adcs.Name: NewBuilderWithFlags(adcs.NewWithFlags),

View File

@@ -17,6 +17,7 @@ package collector
import (
"context"
"errors"
"fmt"
"log/slog"
"runtime/debug"
@@ -24,7 +25,9 @@ import (
"sync/atomic"
"time"
"github.com/prometheus-community/windows_exporter/internal/types"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/internal/perfdata"
types "github.com/prometheus-community/windows_exporter/internal/types"
"github.com/prometheus/client_golang/prometheus"
)
@@ -258,7 +261,15 @@ func (p *Prometheus) execute(name string, c Collector, ch chan<- prometheus.Metr
}
if err != nil {
p.logger.Error(fmt.Sprintf("collector %s failed after %s, resulting in %d metrics", name, duration, numMetrics),
loggerFn := p.logger.Warn
if errors.Is(err, types.ErrNoData) ||
errors.Is(err, perfdata.ErrNoData) ||
errors.Is(err, perfdata.ErrPerformanceCounterNotInitialized) ||
errors.Is(err, mi.MI_RESULT_INVALID_NAMESPACE) {
loggerFn = p.logger.Debug
}
loggerFn(fmt.Sprintf("collector %s failed after %s, resulting in %d metrics", name, duration, numMetrics),
slog.Any("err", err),
)