Skip to content

Commit

Permalink
Environment variable NO_WINDOWS_SERVICE to force interactive mode on …
Browse files Browse the repository at this point in the history
…Windows (#2272)

Adding a feature - adds a check for NO_WINDOWS_SERVICE environment variable on Windows to allow forcing interactive mode instead of running as a service.

This is required for using the collector in Windows Docker containers, as at least some of the Windows base images do not support services (fails with "The service process could not connect to the service controller"). Environment variable is used instead of automatic detection of Docker as it is uncertain if images that support services are possible and/or desired.

Running collector in Windows Docker containers is required to perform containerized integration tests of agents on Windows.
  • Loading branch information
agoallikmaa committed Dec 16, 2020
1 parent 575600b commit 3595b46
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
26 changes: 20 additions & 6 deletions cmd/otelcol/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,39 @@ package main

import (
"fmt"
"os"

"golang.org/x/sys/windows/svc"

"go.opentelemetry.io/collector/service"
)

func run(params service.Parameters) error {
isInteractive, err := svc.IsAnInteractiveSession()
if err != nil {
return fmt.Errorf("failed to determine if we are running in an interactive session %w", err)
}

if isInteractive {
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
if value, present := os.LookupEnv("NO_WINDOWS_SERVICE"); present && value != "0" {
return true, nil
}

if isInteractiveSession, err := svc.IsAnInteractiveSession(); err != nil {
return false, fmt.Errorf("failed to determine if we are running in an interactive session %w", err)
} else {
return isInteractiveSession, nil
}
}

func runService(params service.Parameters) error {
// do not need to supply service name when startup is invoked through Service Control Manager directly
if err := svc.Run("", service.NewWindowsService(params)); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,11 @@ More often than not, exporting data does not work because of a network
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/master/exporter#proxy-support).

### Startup failing in Windows Docker containers

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
this case the `NO_WINDOWS_SERVICE=1` environment variable should be set to force
the collector to be started as if it were running in an interactive terminal,
without attempting to run as a Windows service.

0 comments on commit 3595b46

Please sign in to comment.