Skip to content

Commit 4f8933a

Browse files
committed
[Draft] Attempting to add Windows Services
I ran out of time and will pick this up in the new year Things I'm unsure about: * Can we change the package name on Windows? The package and binaries names for everything else is uniformly 'otelcol-google' but the package name on Windows is 'google-built-opentelemetry-collector' * I deleted some code that said it was handling an issue with spaces in the default config path on Windows. I don't know what that was for but I'd like to fix/workaround that if possible since we are now using services which statically point to the default config path in Program Files * I tested maint.ps1 in isolation, and it works, but I haven't done an end-to-end test yet
1 parent d8a9ac2 commit 4f8933a

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

google-built-opentelemetry-collector/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The Google-Built OpenTelemetry Collector is an open-source, production-ready bui
8989
| ack | [docs](https://www.github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/ackextension/README.md) |
9090
| basicauth | [docs](https://www.github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/basicauthextension/README.md) |
9191
| bearertokenauth | [docs](https://www.github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/bearertokenauthextension/README.md) |
92-
| filestorage | [docs](https://www.github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/storage/README.md) |
92+
| filestorage | [docs](https://www.github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/storage/filestorage/README.md) |
9393
| googleclientauth | [docs](https://www.github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/googleclientauthextension/README.md) |
9494
| headerssetter | [docs](https://www.github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/headerssetterextension/README.md) |
9595
| healthagent | [docs](No docs linked for component) |

google-built-opentelemetry-collector/goo/maint.ps1

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ $envFromMatch = {
2727
Where-Object -Property Name -eq $match.Groups[1].Value).Value
2828
}
2929
$InstallDir = [regex]::Replace($InstallDir,'^<([^>]+)>',$envFromMatch)
30-
3130
$configFilePath = "$InstallDir\config.yaml"
31+
$serviceName = "google-built-opentelemetry-collector"
32+
33+
function Set-ServiceConfig {
34+
& sc.exe failure $serviceName reset= 60 actions= restart/1000/restart/2000
35+
& sc.exe config $serviceName depend= "rpcss" start= delayed-auto
36+
}
3237

3338
if ($Action -eq "install") {
3439
if (-not(Test-Path -Path $configFilePath -PathType Leaf)) {
@@ -39,10 +44,25 @@ if ($Action -eq "install") {
3944
catch {
4045
throw $_.Exception.Message
4146
}
42-
}
43-
else {
44-
Write-Host "Keep [$configFilePath] as-is because a file with that name already exists."
45-
}
46-
# Sleep for 5s before installing services to allow previous service deletion to complete.
47-
Start-Sleep -s 5
47+
}
48+
else {
49+
Write-Host "Keep [$configFilePath] as-is because a file with that name already exists."
50+
}
51+
New-EventLog -LogName Application -Source $serviceName
52+
if (-not (Get-Service $serviceName -ErrorAction SilentlyContinue)) {
53+
New-Service -DisplayName "Google-Built OpenTelemetry Collector" `
54+
-Name $serviceName `
55+
-BinaryPathName "`"${InstallDir}\bin\otelcol-google.exe`" --config=`"${configFilePath}`"" `
56+
-StartupType Automatic `
57+
-Description "OpenTelemetry Collector Built By Google"
58+
Set-ServiceConfig
59+
Start-Service $serviceName -Verbose -ErrorAction Stop
60+
}
61+
else {
62+
Set-ServiceConfig
63+
}
64+
}
65+
elseif ($Action -eq "uninstall") {
66+
Stop-Service -Force $serviceName
67+
& sc.exe delete $serviceName
4868
}

integration_test/smoke_test/smoke_test.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func locationFromEnvVars() PackageLocation {
157157

158158
func restartCommandForPlatform(platform string) string {
159159
if gce.IsWindows(platform) {
160-
panic("Unimplemented call to restartCommandForPlatform on Windows.")
160+
return "Restart-Service -Force google-built-opentelemetry-collector"
161161
}
162162
return "sudo systemctl restart otelcol-google"
163163
}
@@ -276,24 +276,9 @@ func setupOtelCollectorFrom(ctx context.Context, logger *log.Logger, vm *gce.VM,
276276
}
277277

278278
if gce.IsWindows(vm.ImageSpec) {
279-
// Sidestep some quoting issues with spaces in the default config location.
280-
uploadedConfigPath := `C:\configUpload\otel-config.yaml`
281-
if err := gce.UploadContent(ctx, logger, vm, strings.NewReader(config), uploadedConfigPath); err != nil {
282-
return fmt.Errorf("setupOtelCollectorFrom() failed to upload config file: %v", err)
283-
}
284-
285-
quotedOtelPath := "`\"C:\\Program Files\\Google\\OpenTelemetry Collector\\bin\\otelcol-google.exe`\""
286-
// The best way I've found to start a process asynchronously
287-
// (Start-Process and Start-Job didn't detach properly).
288-
// One downside is that standard output and standard error are lost.
289-
if _, err := gce.RunRemotely(ctx, logger, vm, fmt.Sprintf(`Invoke-WmiMethod -ComputerName . -Class Win32_Process -Name Create -ArgumentList "%s --config=%s"`, quotedOtelPath, uploadedConfigPath)); err != nil {
290-
return fmt.Errorf("setupOtelCollectorFrom() failed to start otel collector process: %v", err)
291-
}
292279
// Give the collector time to start up.
293280
time.Sleep(10 * time.Second)
294-
295-
return nil
296-
} // End windows handling.
281+
}
297282

298283
defaultConfigPath := collectorConfigPath(vm.ImageSpec)
299284
if err := gce.UploadContent(ctx, logger, vm, strings.NewReader(config), defaultConfigPath); err != nil {

templates/google-built-opentelemetry-collector/goo/maint.ps1.go.tmpl

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ $envFromMatch = {
2727
Where-Object -Property Name -eq $match.Groups[1].Value).Value
2828
}
2929
$InstallDir = [regex]::Replace($InstallDir,'^<([^>]+)>',$envFromMatch)
30-
3130
$configFilePath = "$InstallDir\config.yaml"
31+
$serviceName = "{{ .Name }}"
32+
33+
function Set-ServiceConfig {
34+
& sc.exe failure $serviceName reset= 60 actions= restart/1000/restart/2000
35+
& sc.exe config $serviceName depend= "rpcss" start= delayed-auto
36+
}
3237
3338
if ($Action -eq "install") {
3439
if (-not(Test-Path -Path $configFilePath -PathType Leaf)) {
@@ -39,10 +44,25 @@ if ($Action -eq "install") {
3944
catch {
4045
throw $_.Exception.Message
4146
}
42-
}
43-
else {
44-
Write-Host "Keep [$configFilePath] as-is because a file with that name already exists."
45-
}
46-
# Sleep for 5s before installing services to allow previous service deletion to complete.
47-
Start-Sleep -s 5
47+
}
48+
else {
49+
Write-Host "Keep [$configFilePath] as-is because a file with that name already exists."
50+
}
51+
New-EventLog -LogName Application -Source $serviceName
52+
if (-not (Get-Service $serviceName -ErrorAction SilentlyContinue)) {
53+
New-Service -DisplayName "{{ .DisplayName }}" `
54+
-Name $serviceName `
55+
-BinaryPathName "`"${InstallDir}\bin\{{ .BinaryName }}.exe`" --config=`"${configFilePath}`"" `
56+
-StartupType Automatic `
57+
-Description "{{ .Description }}"
58+
Set-ServiceConfig
59+
Start-Service $serviceName -Verbose -ErrorAction Stop
60+
}
61+
else {
62+
Set-ServiceConfig
63+
}
64+
}
65+
elseif ($Action -eq "uninstall") {
66+
Stop-Service -Force $serviceName
67+
& sc.exe delete $serviceName
4868
}

0 commit comments

Comments
 (0)