-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_windows.go
61 lines (52 loc) · 1.42 KB
/
main_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//go:build windows
package main
import (
"errors"
"fmt"
"log"
observeotel "observe/otelcol"
"os"
"go.opentelemetry.io/collector/otelcol"
"golang.org/x/sys/windows"
"observe-agent/cmd"
"observe-agent/cmd/config"
"golang.org/x/sys/windows/svc"
)
func run() error {
inService, err := svc.IsWindowsService()
if err != nil {
log.Fatalf("failed to determine if we are running in service: %v", err)
}
if inService {
if len(os.Args) != 2 {
log.Fatal("Expected to run svc as: observe-agent.exe <path to observe-agent.yaml>")
}
cmd.CfgFile = os.Args[1]
cmd.InitConfig()
// Set Env Vars from config
err := config.SetEnvVars()
if err != nil {
return err
}
//
configFilePaths, overridePath, err := config.GetAllOtelConfigFilePaths()
if err != nil {
return err
}
if overridePath != "" {
defer os.Remove(overridePath)
}
colSettings := observeotel.GenerateCollectorSettings(configFilePaths)
if err := svc.Run("", otelcol.NewSvcHandler(*colSettings)); err != nil {
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()
}
return fmt.Errorf("failed to start collector server: %w", err)
}
} else {
return runInteractive()
}
return nil
}