@@ -19,17 +19,23 @@ $ProgressPreference = 'SilentlyContinue'
1919$COLLECTOR_VERSION = " 0.157.0"
2020$INSTALL_DIR = " $env: LOCALAPPDATA \parseable-otelcol"
2121$COLLECTOR_EXE = " $INSTALL_DIR \otelcol.exe"
22- $CONFIG_FILE = " $PSScriptRoot \otelcol.yaml"
23- $PID_FILE = " $PSScriptRoot \otelcol.pid"
24- $LOG_FILE = " $PSScriptRoot \otelcol.log"
25- $ERROR_LOG_FILE = " $PSScriptRoot \otelcol.err.log"
22+ $SCRIPT_DIR = if ([string ]::IsNullOrWhiteSpace($PSScriptRoot )) {
23+ (Get-Location ).Path
24+ }
25+ else {
26+ $PSScriptRoot
27+ }
28+ $CONFIG_FILE = Join-Path $SCRIPT_DIR " otelcol.yaml"
29+ $PID_FILE = Join-Path $SCRIPT_DIR " otelcol.pid"
30+ $LOG_FILE = Join-Path $SCRIPT_DIR " otelcol.log"
31+ $ERROR_LOG_FILE = Join-Path $SCRIPT_DIR " otelcol.err.log"
2632
2733$SCRIPT_PATH = $PSCommandPath
2834if ([string ]::IsNullOrWhiteSpace($SCRIPT_PATH )) {
2935 $SCRIPT_PATH = $MyInvocation.MyCommand.Path
3036}
3137if ([string ]::IsNullOrWhiteSpace($SCRIPT_PATH )) {
32- $SCRIPT_PATH = Join-Path $PSScriptRoot " ingest.ps1"
38+ $SCRIPT_PATH = Join-Path $SCRIPT_DIR " ingest.ps1"
3339}
3440$SCRIPT_CMD = " powershell -NoProfile -ExecutionPolicy Bypass -File '$SCRIPT_PATH '"
3541
@@ -48,6 +54,11 @@ function Write-ErrorMsg {
4854 Write-Host " [ERROR] $Message " - ForegroundColor Red
4955}
5056
57+ function ConvertTo-PowerShellSingleQuoted {
58+ param ([string ]$Value )
59+ return " '" + $Value.Replace (" '" , " ''" ) + " '"
60+ }
61+
5162function Write-ParseableBanner {
5263 $accent = " $ ( [char ]27 ) [38;2;158;158;240m"
5364 $reset = " $ ( [char ]27 ) [0m"
@@ -135,9 +146,19 @@ function Stop-Collector {
135146
136147 try {
137148 Stop-Process - Id $processId - Force - ErrorAction Stop
138- Start-Sleep - Seconds 2
149+ for ($attempt = 0 ; $attempt -lt 10 ; $attempt ++ ) {
150+ if ($null -eq (Get-Process - Id $processId - ErrorAction SilentlyContinue)) {
151+ break
152+ }
153+ Start-Sleep - Seconds 1
154+ }
155+
156+ if ($null -ne (Get-Process - Id $processId - ErrorAction SilentlyContinue)) {
157+ throw " Process $processId did not stop within 10 seconds; PID file was preserved."
158+ }
159+
160+ Remove-Item $PID_FILE - Force - ErrorAction Stop
139161 Write-Info " OpenTelemetry Collector stopped successfully"
140- Remove-Item $PID_FILE - ErrorAction SilentlyContinue
141162 }
142163 catch {
143164 Write-ErrorMsg " Failed to stop OpenTelemetry Collector: $_ "
@@ -158,8 +179,10 @@ function Show-Status {
158179 Write-Info " Config file: $CONFIG_FILE "
159180 Write-Info " Log file: $LOG_FILE "
160181 Write-Info " Error log file: $ERROR_LOG_FILE "
161- Write-Info " To see logs: $SCRIPT_CMD logs"
162- Write-Info " To stop: $SCRIPT_CMD stop"
182+ $pidFileCommand = ConvertTo-PowerShellSingleQuoted $PID_FILE
183+ $errorLogFileCommand = ConvertTo-PowerShellSingleQuoted $ERROR_LOG_FILE
184+ Write-Info " To see logs: Get-Content $errorLogFileCommand -Tail 80 -Wait"
185+ Write-Info " To stop: Stop-Process -Id (Get-Content $pidFileCommand ); Remove-Item $pidFileCommand "
163186 }
164187 else {
165188 Write-Warning " OpenTelemetry Collector is not running"
@@ -292,15 +315,18 @@ function Start-Collector {
292315 $stillRunning = Get-Process - Id $process.Id - ErrorAction SilentlyContinue
293316 if (-not $stillRunning ) {
294317 Write-ErrorMsg " OpenTelemetry Collector exited immediately"
295- Write-ErrorMsg " Run '$SCRIPT_CMD logs' to see error details"
318+ $errorLogFileCommand = ConvertTo-PowerShellSingleQuoted $ERROR_LOG_FILE
319+ Write-ErrorMsg " Check logs: Get-Content $errorLogFileCommand -Tail 80"
296320 Remove-Item $PID_FILE - ErrorAction SilentlyContinue
297321 exit 1
298322 }
299323
324+ $pidFileCommand = ConvertTo-PowerShellSingleQuoted $PID_FILE
325+ $errorLogFileCommand = ConvertTo-PowerShellSingleQuoted $ERROR_LOG_FILE
300326 Write-Info " OpenTelemetry Collector started successfully (PID: $ ( $process.Id ) )"
301- Write-Info " To check status: $SCRIPT_CMD status "
302- Write-Info " To see logs: $SCRIPT_CMD logs "
303- Write-Info " To stop: $SCRIPT_CMD stop "
327+ Write-Info " To check status: Get-Process -Id (Get-Content $pidFileCommand ) "
328+ Write-Info " To see logs: Get-Content $errorLogFileCommand -Tail 80 -Wait "
329+ Write-Info " To stop: Stop-Process -Id (Get-Content $pidFileCommand ); Remove-Item $pidFileCommand "
304330 return $true
305331}
306332
@@ -432,7 +458,9 @@ function Setup-Collector {
432458
433459 $configContent = ($configLines -join [Environment ]::NewLine) + [Environment ]::NewLine
434460 $utf8NoBom = New-Object System.Text.UTF8Encoding $false
435- $tempConfigFile = " $CONFIG_FILE .$ ( [guid ]::NewGuid().ToString(' N' )) .tmp"
461+ $configUpdateId = [guid ]::NewGuid().ToString(' N' )
462+ $tempConfigFile = " $CONFIG_FILE .$configUpdateId .tmp"
463+ $backupConfigFile = " $CONFIG_FILE .$configUpdateId .bak"
436464
437465 try {
438466 $tempConfigStream = [System.IO.File ]::Create($tempConfigFile )
@@ -462,15 +490,20 @@ function Setup-Collector {
462490
463491 if (Test-Path $CONFIG_FILE ) {
464492 Set-Acl - Path $CONFIG_FILE - AclObject $acl - ErrorAction Stop
465- [System.IO.File ]::Replace($tempConfigFile , $CONFIG_FILE , $null )
493+ [System.IO.File ]::Replace($tempConfigFile , $CONFIG_FILE , $backupConfigFile )
466494 }
467495 else {
468496 [System.IO.File ]::Move($tempConfigFile , $CONFIG_FILE )
469497 }
470498 Set-Acl - Path $CONFIG_FILE - AclObject $acl - ErrorAction Stop
471499 }
500+ catch {
501+ Write-ErrorMsg " Failed to update OpenTelemetry Collector configuration: $_ "
502+ exit 1
503+ }
472504 finally {
473505 Remove-Item $tempConfigFile - Force - ErrorAction SilentlyContinue
506+ Remove-Item $backupConfigFile - Force - ErrorAction SilentlyContinue
474507 }
475508
476509 if (Test-CollectorRunning ) {
0 commit comments