Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/windows_exporter/windows_exporter_windows.go
5398 views
1
package windows_exporter //nolint:golint
2
3
import (
4
"sort"
5
"strings"
6
"time"
7
8
"github.com/alecthomas/kingpin/v2"
9
"github.com/go-kit/log"
10
"github.com/go-kit/log/level"
11
"github.com/grafana/agent/pkg/integrations"
12
"github.com/prometheus-community/windows_exporter/collector"
13
)
14
15
// New creates a new windows_exporter integration.
16
func New(logger log.Logger, c *Config) (integrations.Integration, error) {
17
windowsExporter := kingpin.New("", "")
18
collector.RegisterCollectorsFlags(windowsExporter)
19
c.toExporterConfig(windowsExporter)
20
21
if _, err := windowsExporter.Parse([]string{}); err != nil {
22
return nil, err
23
}
24
25
collector.RegisterCollectors()
26
enabledCollectorNames := enabledCollectors(c.EnabledCollectors)
27
collectors, err := buildCollectors(enabledCollectorNames)
28
if err != nil {
29
return nil, err
30
}
31
32
collectorNames := make([]string, 0, len(collectors))
33
for key := range collectors {
34
collectorNames = append(collectorNames, key)
35
}
36
sort.Strings(collectorNames)
37
level.Info(logger).Log("msg", "enabled windows_exporter collectors", "collectors", strings.Join(collectorNames, ","))
38
39
return integrations.NewCollectorIntegration(c.Name(), integrations.WithCollectors(
40
// Hard-coded 4m timeout to represent the time a series goes stale.
41
// TODO: Make configurable if useful.
42
collector.NewPrometheus(4*time.Minute, collectors),
43
)), nil
44
}
45
46
func enabledCollectors(input string) []string {
47
separated := strings.Split(input, ",")
48
unique := map[string]struct{}{}
49
for _, s := range separated {
50
s = strings.TrimSpace(s)
51
if s != "" {
52
unique[s] = struct{}{}
53
}
54
}
55
result := make([]string, 0, len(unique))
56
for s := range unique {
57
result = append(result, s)
58
}
59
return result
60
}
61
62
func buildCollectors(enabled []string) (map[string]collector.Collector, error) {
63
collectors := map[string]collector.Collector{}
64
65
for _, name := range enabled {
66
c, err := collector.Build(name)
67
if err != nil {
68
return nil, err
69
}
70
collectors[name] = c
71
}
72
73
return collectors, nil
74
}
75
76