Skip to content

Commit

Permalink
process: Use registry collector for V1 data (#1814)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke authored Dec 21, 2024
1 parent 39c929e commit a9f8b3b
Show file tree
Hide file tree
Showing 158 changed files with 7,791 additions and 7,746 deletions.
4 changes: 2 additions & 2 deletions .run/all.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<configuration default="false" name="all" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="run">
<module name="windows_exporter" />
<working_directory value="$PROJECT_DIR$" />
<parameters value="--web.listen-address=127.0.0.1:9182 --log.level=info --collectors.enabled=ad,adcs,adfs,cache,container,cpu,cpu_info,cs,dfsr,dhcp,diskdrive,dns,exchange,filetime,fsrmquota,hyperv,iis,license,logical_disk,logon,memory,mscluster,msmq,mssql,net,netframework,nps,os,pagefile,performancecounter,physical_disk,printer,process,remote_fx,scheduled_task,service,smb,smbclient,smtp,system,tcp,terminal_services,thermalzone,time,udp,update,vmware,performancecounter --debug.enabled --collector.performancecounter.objects='[{ &quot;name&quot;: &quot;memory&quot;, &quot;object&quot;: &quot;Memory&quot;, &quot;counters&quot;: [{ &quot;name&quot;:&quot;Cache Faults/sc&quot;, &quot;type&quot;:&quot;counter&quot; }]}]'" />
<parameters value="--web.listen-address=127.0.0.1:9182 --log.level=info --collectors.enabled=ad,adcs,adfs,cache,container,cpu,cpu_info,cs,dfsr,dhcp,diskdrive,dns,exchange,filetime,fsrmquota,hyperv,iis,license,logical_disk,logon,memory,mscluster,msmq,mssql,net,netframework,nps,os,pagefile,performancecounter,physical_disk,printer,process,remote_fx,scheduled_task,service,smb,smbclient,smtp,system,tcp,terminal_services,thermalzone,time,udp,update,vmware,performancecounter --debug.enabled --collector.performancecounter.objects='[{ &quot;name&quot;: &quot;memory&quot;, &quot;object&quot;: &quot;Memory&quot;, &quot;counters&quot;: [{ &quot;name&quot;:&quot;Cache Faults/sec&quot;, &quot;type&quot;:&quot;counter&quot; }]}]'" />
<sudo value="true" />
<kind value="PACKAGE" />
<package value="github.com/prometheus-community/windows_exporter/cmd/windows_exporter" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$/exporter.go" />
<method v="2" />
</configuration>
</component>
</component>
28 changes: 18 additions & 10 deletions cmd/windows_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"os/signal"
"os/user"
"runtime"
"runtime/debug"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -66,6 +67,7 @@ func main() {

func run() int {
startTime := time.Now()
ctx := context.Background()

app := kingpin.New("windows_exporter", "A metrics collector for Windows.")

Expand Down Expand Up @@ -103,6 +105,10 @@ func run() int {
"process.priority",
"Priority of the exporter process. Higher priorities may improve exporter responsiveness during periods of system load. Can be one of [\"realtime\", \"high\", \"abovenormal\", \"normal\", \"belownormal\", \"low\"]",
).Default("normal").String()
memoryLimit = app.Flag(
"process.memory-limit",
"Limit memory usage in bytes. This is a soft-limit and not guaranteed. 0 means no limit. Read more at https://pkg.go.dev/runtime/debug#SetMemoryLimit .",
).Default("200000000").Int64()
)

logFile := &log.AllowedFile{}
Expand Down Expand Up @@ -132,6 +138,8 @@ func run() int {
return 1
}

debug.SetMemoryLimit(*memoryLimit)

logger, err := log.New(logConfig)
if err != nil {
//nolint:sloglint // we do not have an logger yet
Expand All @@ -143,7 +151,7 @@ func run() int {
}

if *configFile != "" {
resolver, err := config.NewResolver(*configFile, logger, *insecureSkipVerify)
resolver, err := config.NewResolver(ctx, *configFile, logger, *insecureSkipVerify)
if err != nil {
logger.Error("could not load config file",
slog.Any("err", err),
Expand All @@ -153,7 +161,7 @@ func run() int {
}

if err = resolver.Bind(app, os.Args[1:]); err != nil {
logger.Error("Failed to bind configuration",
logger.ErrorContext(ctx, "failed to bind configuration",
slog.Any("err", err),
)

Expand All @@ -167,7 +175,7 @@ func run() int {

// Parse flags once more to include those discovered in configuration file(s).
if _, err = app.Parse(os.Args[1:]); err != nil {
logger.Error("Failed to parse CLI args from YAML file",
logger.ErrorContext(ctx, "failed to parse CLI args from YAML file",
slog.Any("err", err),
)

Expand All @@ -185,7 +193,7 @@ func run() int {
}
}

logger.Debug("Logging has Started")
logger.LogAttrs(ctx, slog.LevelDebug, "logging has Started")

if err = setPriorityWindows(logger, os.Getpid(), *processPriority); err != nil {
logger.Error("failed to set process priority",
Expand Down Expand Up @@ -217,7 +225,7 @@ func run() int {

logCurrentUser(logger)

logger.Info("Enabled collectors: " + strings.Join(enabledCollectorList, ", "))
logger.InfoContext(ctx, "Enabled collectors: "+strings.Join(enabledCollectorList, ", "))

mux := http.NewServeMux()
mux.Handle("GET /health", httphandler.NewHealthHandler())
Expand All @@ -235,7 +243,7 @@ func run() int {
mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace)
}

logger.Info(fmt.Sprintf("starting windows_exporter in %s", time.Since(startTime)),
logger.LogAttrs(ctx, slog.LevelInfo, fmt.Sprintf("starting windows_exporter in %s", time.Since(startTime)),
slog.String("version", version.Version),
slog.String("branch", version.Branch),
slog.String("revision", version.GetRevision()),
Expand All @@ -262,7 +270,7 @@ func run() int {
close(errCh)
}()

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, os.Kill)
defer stop()

select {
Expand All @@ -272,7 +280,7 @@ func run() int {
logger.Info("Shutting down windows_exporter via service control")
case err := <-errCh:
if err != nil {
logger.Error("Failed to start windows_exporter",
logger.ErrorContext(ctx, "Failed to start windows_exporter",
slog.Any("err", err),
)

Expand All @@ -285,7 +293,7 @@ func run() int {

_ = server.Shutdown(ctx)

logger.Info("windows_exporter has shut down")
logger.InfoContext(ctx, "windows_exporter has shut down")

return 0
}
Expand Down Expand Up @@ -326,7 +334,7 @@ func setPriorityWindows(logger *slog.Logger, pid int, priority string) error {
return nil
}

logger.Debug("setting process priority to " + priority)
logger.LogAttrs(context.Background(), slog.LevelDebug, "setting process priority to "+priority)

// https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights
handle, err := windows.OpenProcess(
Expand Down
Loading

0 comments on commit a9f8b3b

Please sign in to comment.