Skip to content

Commit 14413b7

Browse files
authored
Add Windows Services (#474)
1 parent eff68fc commit 14413b7

File tree

7 files changed

+61
-36
lines changed

7 files changed

+61
-36
lines changed

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 = "otelcol-google"
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
}

google-built-opentelemetry-collector/goo/otelcol.goospec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{$GOARCH := .GOARCH -}}
55
{{$INSTALL_DIR := or (.INSTALL_DIR) "<ProgramFiles>/Google/OpenTelemetry Collector" -}}
66
{
7-
"name": "google-built-opentelemetry-collector",
7+
"name": "otelcol-google",
88
"version": "{{$PKG_VERSION}}",
99
"arch": "{{$ARCH}}",
1010
"authors": "Google Cloud Platform",

integration_test/smoke_test/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.0
44

55
toolchain go1.24.3
66

7-
require github.com/GoogleCloudPlatform/opentelemetry-operations-collector/integration_test/gce-testing-internal v0.0.0-20251222203343-1635e8bc87b0
7+
require github.com/GoogleCloudPlatform/opentelemetry-operations-collector/integration_test/gce-testing-internal v0.0.0-20260119145159-eff68fc7b8ef
88

99
require (
1010
cel.dev/expr v0.20.0 // indirect

integration_test/smoke_test/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ cloud.google.com/go/trace v1.11.6 h1:2O2zjPzqPYAHrn3OKl029qlqG6W8ZdYaOWRyr8NgMT4
2222
cloud.google.com/go/trace v1.11.6/go.mod h1:GA855OeDEBiBMzcckLPE2kDunIpC72N+Pq8WFieFjnI=
2323
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
2424
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
25-
github.com/GoogleCloudPlatform/opentelemetry-operations-collector/integration_test/gce-testing-internal v0.0.0-20251222203343-1635e8bc87b0 h1:zb5s1Fj3k9wFXfooSCl+8uGL85Ep2xTiSiwIBI+3/hE=
26-
github.com/GoogleCloudPlatform/opentelemetry-operations-collector/integration_test/gce-testing-internal v0.0.0-20251222203343-1635e8bc87b0/go.mod h1:hmqK+hsg2z+Xo/Yz16sNxb6TZX+q0KxKsrgYzdpV0P8=
25+
github.com/GoogleCloudPlatform/opentelemetry-operations-collector/integration_test/gce-testing-internal v0.0.0-20260119145159-eff68fc7b8ef h1:v2hZ/3wkAB/9TvEqqE3zjlVsqozs4rzZ4zqvW/9YPLQ=
26+
github.com/GoogleCloudPlatform/opentelemetry-operations-collector/integration_test/gce-testing-internal v0.0.0-20260119145159-eff68fc7b8ef/go.mod h1:hmqK+hsg2z+Xo/Yz16sNxb6TZX+q0KxKsrgYzdpV0P8=
2727
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0 h1:ErKg/3iS1AKcTkf3yixlZ54f9U1rljCkQyEXWUnIUxc=
2828
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0/go.mod h1:yAZHSGnqScoU556rBOVkwLze6WP5N+U11RHuWaGVxwY=
2929
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.51.0 h1:fYE9p3esPxA/C0rQ0AHhP0drtPXDRhaWiwg1DPqO7IU=

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 otelcol-google"
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 = "{{ .BinaryName }}"
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
}

templates/google-built-opentelemetry-collector/goo/otelcol.goospec.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{`{{$GOARCH := .GOARCH -}}`}}
55
{{`{{$INSTALL_DIR := or (.INSTALL_DIR) "<ProgramFiles>/Google/OpenTelemetry Collector" -}}`}}
66
{
7-
"name": "{{ .Name }}",
7+
"name": "{{ .BinaryName }}",
88
"version": "{{`{{$PKG_VERSION}}`}}",
99
"arch": "{{`{{$ARCH}}`}}",
1010
"authors": "Google Cloud Platform",

0 commit comments

Comments
 (0)