main_windows.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Code generated by "go.opentelemetry.io/collector/cmd/builder". DO NOT EDIT.
  2. //go:build windows
  3. // +build windows
  4. package main
  5. import (
  6. "fmt"
  7. "os"
  8. "go.opentelemetry.io/collector/otelcol"
  9. "golang.org/x/sys/windows/svc"
  10. )
  11. func run(params otelcol.CollectorSettings) error {
  12. if useInteractiveMode, err := checkUseInteractiveMode(); err != nil {
  13. return err
  14. } else if useInteractiveMode {
  15. return runInteractive(params)
  16. } else {
  17. return runService(params)
  18. }
  19. }
  20. func checkUseInteractiveMode() (bool, error) {
  21. // If environment variable NO_WINDOWS_SERVICE is set with any value other
  22. // than 0, use interactive mode instead of running as a service. This should
  23. // be set in case running as a service is not possible or desired even
  24. // though the current session is not detected to be interactive
  25. if value, present := os.LookupEnv("NO_WINDOWS_SERVICE"); present && value != "0" {
  26. return true, nil
  27. }
  28. isInteractiveSession, err := svc.IsAnInteractiveSession()
  29. if err != nil {
  30. return false, fmt.Errorf("failed to determine if we are running in an interactive session: %w", err)
  31. }
  32. return isInteractiveSession, nil
  33. }
  34. func runService(params otelcol.CollectorSettings) error {
  35. // do not need to supply service name when startup is invoked through Service Control Manager directly
  36. if err := svc.Run("", otelcol.NewSvcHandler(params)); err != nil {
  37. return fmt.Errorf("failed to start collector server: %w", err)
  38. }
  39. return nil
  40. }