From eb920ab1201f12742fe6650a8b089bc404c24519 Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Mon, 4 Dec 2023 19:19:31 -0800 Subject: [PATCH 1/4] Fix run as service detection --- .../fix-running-as-service-detection.yaml | 25 ++++++++++++ .../builder/templates/main_windows.go.tmpl | 39 +++++++------------ docs/troubleshooting.md | 2 +- 3 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 .chloggen/fix-running-as-service-detection.yaml diff --git a/.chloggen/fix-running-as-service-detection.yaml b/.chloggen/fix-running-as-service-detection.yaml new file mode 100644 index 00000000000..f6d8b01c1d0 --- /dev/null +++ b/.chloggen/fix-running-as-service-detection.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: cmd/otelcorecol + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix the code detecting if the collector is running as a service on Windows. + +# One or more tracking issues or pull requests related to the change +issues: [7350] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: This fix should make setting the `NO_WINDOWS_SERVICE` environment variable unnecessary. + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] \ No newline at end of file diff --git a/cmd/builder/internal/builder/templates/main_windows.go.tmpl b/cmd/builder/internal/builder/templates/main_windows.go.tmpl index ba327180578..043f6ba39ba 100644 --- a/cmd/builder/internal/builder/templates/main_windows.go.tmpl +++ b/cmd/builder/internal/builder/templates/main_windows.go.tmpl @@ -8,39 +8,30 @@ package main import ( "fmt" "os" + "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc" "go.opentelemetry.io/collector/otelcol" ) func run(params otelcol.CollectorSettings) error { - if useInteractiveMode, err := checkUseInteractiveMode(); err != nil { - return err - } else if useInteractiveMode { - return runInteractive(params) - } else { - return runService(params) - } -} - -func checkUseInteractiveMode() (bool, error) { - // If environment variable NO_WINDOWS_SERVICE is set with any value other - // than 0, use interactive mode instead of running as a service. This should - // be set in case running as a service is not possible or desired even - // though the current session is not detected to be interactive + // There shouldn't be any reason to use NO_WINDOWS_SERVICE anymore, but, + // keeping it as a forcing mechanism or if someone is concerned about + // the cost of attempting to run as a service before falling back to + // interactive mode. if value, present := os.LookupEnv("NO_WINDOWS_SERVICE"); present && value != "0" { - return true, nil - } - - isInteractiveSession, err := svc.IsAnInteractiveSession() - if err != nil { - return false, fmt.Errorf("failed to determine if we are running in an interactive session: %w", err) + return runInteractive(params) } - return isInteractiveSession, nil -} -func runService(params otelcol.CollectorSettings) error { - // do not need to supply service name when startup is invoked through Service Control Manager directly + // No need to supply service name when startup is invoked through + // the Service Control Manager directly. if err := svc.Run("", otelcol.NewSvcHandler(params)); err != nil { + errno, ok := err.(syscall.Errno) + if ok && errno == windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT { + // Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value + // this means that the process is not running as a service, so run interactively. + return runInteractive(params) + } + return fmt.Errorf("failed to start collector server: %w", err) } diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 24e20bdfe8b..21caec042fc 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -283,7 +283,7 @@ configuration issue. This could be due to a firewall, DNS, or proxy issue. Note that the Collector does have [proxy support](https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter#proxy-support). -### Startup failing in Windows Docker containers +### Startup failing in Windows Docker containers (v0.90.1 and earlier) The process may fail to start in a Windows Docker container with the following error: `The service process could not connect to the service controller`. In From 5d8cbdf78085c381f219465a6099f7ce8064b5ba Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Mon, 4 Dec 2023 19:36:04 -0800 Subject: [PATCH 2/4] make genotelcorecol --- cmd/otelcorecol/main_windows.go | 40 +++++++++++++-------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/cmd/otelcorecol/main_windows.go b/cmd/otelcorecol/main_windows.go index ad96fd1e801..7898bf00531 100644 --- a/cmd/otelcorecol/main_windows.go +++ b/cmd/otelcorecol/main_windows.go @@ -8,41 +8,33 @@ package main import ( "fmt" "os" + "syscall" + "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc" "go.opentelemetry.io/collector/otelcol" ) func run(params otelcol.CollectorSettings) error { - if useInteractiveMode, err := checkUseInteractiveMode(); err != nil { - return err - } else if useInteractiveMode { - return runInteractive(params) - } else { - return runService(params) - } -} - -func checkUseInteractiveMode() (bool, error) { - // If environment variable NO_WINDOWS_SERVICE is set with any value other - // than 0, use interactive mode instead of running as a service. This should - // be set in case running as a service is not possible or desired even - // though the current session is not detected to be interactive + // There shouldn't be any reason to use NO_WINDOWS_SERVICE anymore, but, + // keeping it as a forcing mechanism or if someone is concerned about + // the cost of attempting to run as a service before falling back to + // interactive mode. if value, present := os.LookupEnv("NO_WINDOWS_SERVICE"); present && value != "0" { - return true, nil - } - - isInteractiveSession, err := svc.IsAnInteractiveSession() - if err != nil { - return false, fmt.Errorf("failed to determine if we are running in an interactive session: %w", err) + return runInteractive(params) } - return isInteractiveSession, nil -} -func runService(params otelcol.CollectorSettings) error { - // do not need to supply service name when startup is invoked through Service Control Manager directly + // No need to supply service name when startup is invoked through + // the Service Control Manager directly. if err := svc.Run("", otelcol.NewSvcHandler(params)); err != nil { + errno, ok := err.(syscall.Errno) + if ok && errno == windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT { + // Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value + // this means that the process is not running as a service, so run interactively. + return runInteractive(params) + } + return fmt.Errorf("failed to start collector server: %w", err) } From 9434655b95bb54531929dd5a12024dc42e302a2e Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Tue, 5 Dec 2023 08:23:47 -0800 Subject: [PATCH 3/4] Remove NO_WINDOWS_SERVICE env var --- .chloggen/fix-running-as-service-detection.yaml | 2 +- .../internal/builder/templates/main_windows.go.tmpl | 9 --------- cmd/otelcorecol/main_windows.go | 9 --------- 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/.chloggen/fix-running-as-service-detection.yaml b/.chloggen/fix-running-as-service-detection.yaml index f6d8b01c1d0..d5f3909e6c2 100644 --- a/.chloggen/fix-running-as-service-detection.yaml +++ b/.chloggen/fix-running-as-service-detection.yaml @@ -15,7 +15,7 @@ issues: [7350] # (Optional) One or more lines of additional information to render under the primary note. # These lines will be padded with 2 spaces and then inserted directly into the document. # Use pipe (|) for multiline entries. -subtext: This fix should make setting the `NO_WINDOWS_SERVICE` environment variable unnecessary. +subtext: Removed the `NO_WINDOWS_SERVICE` environment variable given it is not needed anymore. # Optional: The change log or logs in which this entry should be included. # e.g. '[user]' or '[user, api]' diff --git a/cmd/builder/internal/builder/templates/main_windows.go.tmpl b/cmd/builder/internal/builder/templates/main_windows.go.tmpl index 043f6ba39ba..9166599f83b 100644 --- a/cmd/builder/internal/builder/templates/main_windows.go.tmpl +++ b/cmd/builder/internal/builder/templates/main_windows.go.tmpl @@ -7,21 +7,12 @@ package main import ( "fmt" - "os" "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc" "go.opentelemetry.io/collector/otelcol" ) func run(params otelcol.CollectorSettings) error { - // There shouldn't be any reason to use NO_WINDOWS_SERVICE anymore, but, - // keeping it as a forcing mechanism or if someone is concerned about - // the cost of attempting to run as a service before falling back to - // interactive mode. - if value, present := os.LookupEnv("NO_WINDOWS_SERVICE"); present && value != "0" { - return runInteractive(params) - } - // No need to supply service name when startup is invoked through // the Service Control Manager directly. if err := svc.Run("", otelcol.NewSvcHandler(params)); err != nil { diff --git a/cmd/otelcorecol/main_windows.go b/cmd/otelcorecol/main_windows.go index 7898bf00531..6237937b50e 100644 --- a/cmd/otelcorecol/main_windows.go +++ b/cmd/otelcorecol/main_windows.go @@ -7,7 +7,6 @@ package main import ( "fmt" - "os" "syscall" "golang.org/x/sys/windows" @@ -17,14 +16,6 @@ import ( ) func run(params otelcol.CollectorSettings) error { - // There shouldn't be any reason to use NO_WINDOWS_SERVICE anymore, but, - // keeping it as a forcing mechanism or if someone is concerned about - // the cost of attempting to run as a service before falling back to - // interactive mode. - if value, present := os.LookupEnv("NO_WINDOWS_SERVICE"); present && value != "0" { - return runInteractive(params) - } - // No need to supply service name when startup is invoked through // the Service Control Manager directly. if err := svc.Run("", otelcol.NewSvcHandler(params)); err != nil { From d77a32989a5a5d26e673b66d845cc1ef2e200b84 Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Tue, 5 Dec 2023 22:31:26 -0800 Subject: [PATCH 4/4] Use errors.Is instead of casting --- cmd/builder/internal/builder/templates/main_windows.go.tmpl | 4 ++-- cmd/otelcorecol/main_windows.go | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/builder/internal/builder/templates/main_windows.go.tmpl b/cmd/builder/internal/builder/templates/main_windows.go.tmpl index 9166599f83b..3e001b250bf 100644 --- a/cmd/builder/internal/builder/templates/main_windows.go.tmpl +++ b/cmd/builder/internal/builder/templates/main_windows.go.tmpl @@ -6,6 +6,7 @@ package main import ( + "errors" "fmt" "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc" @@ -16,8 +17,7 @@ func run(params otelcol.CollectorSettings) error { // No need to supply service name when startup is invoked through // the Service Control Manager directly. if err := svc.Run("", otelcol.NewSvcHandler(params)); err != nil { - errno, ok := err.(syscall.Errno) - if ok && errno == windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT { + if errors.Is(err, windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { // Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value // this means that the process is not running as a service, so run interactively. return runInteractive(params) diff --git a/cmd/otelcorecol/main_windows.go b/cmd/otelcorecol/main_windows.go index 6237937b50e..285aab81289 100644 --- a/cmd/otelcorecol/main_windows.go +++ b/cmd/otelcorecol/main_windows.go @@ -6,8 +6,8 @@ package main import ( + "errors" "fmt" - "syscall" "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc" @@ -19,8 +19,7 @@ func run(params otelcol.CollectorSettings) error { // No need to supply service name when startup is invoked through // the Service Control Manager directly. if err := svc.Run("", otelcol.NewSvcHandler(params)); err != nil { - errno, ok := err.(syscall.Errno) - if ok && errno == windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT { + if errors.Is(err, windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { // Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value // this means that the process is not running as a service, so run interactively. return runInteractive(params)