Path: blob/main/pkg/integrations/windows_exporter/windows_exporter_windows.go
5398 views
package windows_exporter //nolint:golint12import (3"sort"4"strings"5"time"67"github.com/alecthomas/kingpin/v2"8"github.com/go-kit/log"9"github.com/go-kit/log/level"10"github.com/grafana/agent/pkg/integrations"11"github.com/prometheus-community/windows_exporter/collector"12)1314// New creates a new windows_exporter integration.15func New(logger log.Logger, c *Config) (integrations.Integration, error) {16windowsExporter := kingpin.New("", "")17collector.RegisterCollectorsFlags(windowsExporter)18c.toExporterConfig(windowsExporter)1920if _, err := windowsExporter.Parse([]string{}); err != nil {21return nil, err22}2324collector.RegisterCollectors()25enabledCollectorNames := enabledCollectors(c.EnabledCollectors)26collectors, err := buildCollectors(enabledCollectorNames)27if err != nil {28return nil, err29}3031collectorNames := make([]string, 0, len(collectors))32for key := range collectors {33collectorNames = append(collectorNames, key)34}35sort.Strings(collectorNames)36level.Info(logger).Log("msg", "enabled windows_exporter collectors", "collectors", strings.Join(collectorNames, ","))3738return integrations.NewCollectorIntegration(c.Name(), integrations.WithCollectors(39// Hard-coded 4m timeout to represent the time a series goes stale.40// TODO: Make configurable if useful.41collector.NewPrometheus(4*time.Minute, collectors),42)), nil43}4445func enabledCollectors(input string) []string {46separated := strings.Split(input, ",")47unique := map[string]struct{}{}48for _, s := range separated {49s = strings.TrimSpace(s)50if s != "" {51unique[s] = struct{}{}52}53}54result := make([]string, 0, len(unique))55for s := range unique {56result = append(result, s)57}58return result59}6061func buildCollectors(enabled []string) (map[string]collector.Collector, error) {62collectors := map[string]collector.Collector{}6364for _, name := range enabled {65c, err := collector.Build(name)66if err != nil {67return nil, err68}69collectors[name] = c70}7172return collectors, nil73}747576