From 432486d7d6d4f181a1ad828303cc9e91b622df22 Mon Sep 17 00:00:00 2001 From: Jamie Milton Date: Tue, 23 Aug 2022 14:57:16 +0100 Subject: [PATCH 1/4] Move Service Initiate out to seperate package Signed-off-by: Jamie Milton --- exporter.go | 55 ++++++---------------------------------- initiate/initiate.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 initiate/initiate.go diff --git a/exporter.go b/exporter.go index 38d968b2c..0e127944b 100644 --- a/exporter.go +++ b/exporter.go @@ -4,6 +4,10 @@ package main import ( + //We keep this at the top to ensure it happens first + "github.com/prometheus-community/windows_exporter/initiate" + "github.com/prometheus-community/windows_exporter/log" + "encoding/json" "fmt" "net/http" @@ -16,12 +20,10 @@ import ( "sync" "time" - "golang.org/x/sys/windows/svc" - "github.com/StackExchange/wmi" "github.com/prometheus-community/windows_exporter/collector" "github.com/prometheus-community/windows_exporter/config" - "github.com/prometheus-community/windows_exporter/log" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -289,7 +291,6 @@ func main() { "Seconds to subtract from the timeout allowed by the client. Tune to allow for overhead or high loads.", ).Default("0.5").Float64() ) - log.AddFlags(kingpin.CommandLine) kingpin.Version(version.Print("windows_exporter")) kingpin.HelpFlag.Short('h') @@ -297,7 +298,7 @@ func main() { // Load values from configuration file(s). Executable flags must first be parsed, in order // to load the specified file(s). kingpin.Parse() - + log.Debug("Logging has Started") if *configFile != "" { resolver, err := config.NewResolver(*configFile) if err != nil { @@ -327,21 +328,6 @@ func main() { initWbem() - isService, err := svc.IsWindowsService() - if err != nil { - log.Fatal(err) - } - - stopCh := make(chan bool) - if isService { - go func() { - err = svc.Run(serviceName, &windowsExporterService{stopCh: stopCh}) - if err != nil { - log.Errorf("Failed to start service: %v", err) - } - }() - } - collectors, err := loadCollectors(*enabledCollectors) if err != nil { log.Fatalf("Couldn't load collectors: %s", err) @@ -421,7 +407,7 @@ func main() { }() for { - if <-stopCh { + if <-initiate.StopCh { log.Info("Shutting down windows_exporter") break } @@ -463,33 +449,6 @@ func withConcurrencyLimit(n int, next http.HandlerFunc) http.HandlerFunc { } } -type windowsExporterService struct { - stopCh chan<- bool -} - -func (s *windowsExporterService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { - const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown - changes <- svc.Status{State: svc.StartPending} - changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} -loop: - for { - select { - case c := <-r: - switch c.Cmd { - case svc.Interrogate: - changes <- c.CurrentStatus - case svc.Stop, svc.Shutdown: - s.stopCh <- true - break loop - default: - log.Error(fmt.Sprintf("unexpected control request #%d", c)) - } - } - } - changes <- svc.Status{State: svc.StopPending} - return -} - type metricsHandler struct { timeoutMargin float64 collectorFactory func(timeout time.Duration, requestedCollectors []string) (error, *windowsCollector) diff --git a/initiate/initiate.go b/initiate/initiate.go new file mode 100644 index 000000000..071b16d60 --- /dev/null +++ b/initiate/initiate.go @@ -0,0 +1,60 @@ +package initiate + +import ( + "fmt" + + "github.com/prometheus-community/windows_exporter/log" + "golang.org/x/sys/windows/svc" +) + +const ( + serviceName = "windows_exporter" +) + +type windowsExporterService struct { + stopCh chan<- bool +} + +func (s *windowsExporterService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { + const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown + changes <- svc.Status{State: svc.StartPending} + changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} +loop: + for { + select { + case c := <-r: + switch c.Cmd { + case svc.Interrogate: + changes <- c.CurrentStatus + case svc.Stop, svc.Shutdown: + log.Debug("Service Stop Received") + s.stopCh <- true + break loop + default: + log.Error(fmt.Sprintf("unexpected control request #%d", c)) + } + } + } + changes <- svc.Status{State: svc.StopPending} + return +} + +var StopCh chan bool + +func init() { + log.Debug("Checking if We are a service") + isService, err := svc.IsWindowsService() + if err != nil { + log.Fatal(err) + } + StopCh := make(chan bool) + log.Debug("Attempting to start exporter service") + if isService { + go func() { + err = svc.Run(serviceName, &windowsExporterService{stopCh: StopCh}) + if err != nil { + log.Errorf("Failed to start service: %v", err) + } + }() + } +} From b997ecd89c8fe0aa062eb7c40f8146a63c63dc3d Mon Sep 17 00:00:00 2001 From: Jamie Milton Date: Tue, 23 Aug 2022 16:47:31 +0100 Subject: [PATCH 2/4] Correct Channel Definition Signed-off-by: Jamie Milton --- initiate/initiate.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/initiate/initiate.go b/initiate/initiate.go index 071b16d60..7da31c0d2 100644 --- a/initiate/initiate.go +++ b/initiate/initiate.go @@ -39,7 +39,7 @@ loop: return } -var StopCh chan bool +var StopCh = make(chan bool) func init() { log.Debug("Checking if We are a service") @@ -47,7 +47,6 @@ func init() { if err != nil { log.Fatal(err) } - StopCh := make(chan bool) log.Debug("Attempting to start exporter service") if isService { go func() { From 3d94cbf8b1cb03cf44e2269a7e4fd2c6536f8c17 Mon Sep 17 00:00:00 2001 From: Jamie Milton Date: Wed, 24 Aug 2022 09:16:16 +0100 Subject: [PATCH 3/4] Remove unused Const Signed-off-by: Jamie Milton --- exporter.go | 1 - 1 file changed, 1 deletion(-) diff --git a/exporter.go b/exporter.go index 0e127944b..4f514c3a6 100644 --- a/exporter.go +++ b/exporter.go @@ -52,7 +52,6 @@ type prometheusVersion struct { const ( defaultCollectors = "cpu,cs,logical_disk,net,os,service,system,textfile" defaultCollectorsPlaceholder = "[defaults]" - serviceName = "windows_exporter" ) var ( From 135b74675e4140365e0d53a5c7220d34f20501fb Mon Sep 17 00:00:00 2001 From: Jamie Milton Date: Wed, 24 Aug 2022 10:04:23 +0100 Subject: [PATCH 4/4] Additional Comments Signed-off-by: Jamie Milton --- exporter.go | 2 +- initiate/initiate.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/exporter.go b/exporter.go index 4f514c3a6..42e805c63 100644 --- a/exporter.go +++ b/exporter.go @@ -4,7 +4,7 @@ package main import ( - //We keep this at the top to ensure it happens first + //Its important that we do these first so that we can register with the windows service control ASAP to avoid timeouts "github.com/prometheus-community/windows_exporter/initiate" "github.com/prometheus-community/windows_exporter/log" diff --git a/initiate/initiate.go b/initiate/initiate.go index 7da31c0d2..9b4c08e8a 100644 --- a/initiate/initiate.go +++ b/initiate/initiate.go @@ -1,3 +1,4 @@ +//This package allows us to initiate Time Sensitive components (Like registering the windows service) as early as possible in the startup process package initiate import (