forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_ppid_windows.go
55 lines (47 loc) · 1.4 KB
/
monitor_ppid_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
//go:build windows
package opampextension
import (
"context"
"fmt"
"os"
"time"
"go.opentelemetry.io/collector/component"
)
func monitorPPID(ctx context.Context, ppid int, reportStatus func(*component.StatusEvent)) {
// On Windows systems, we can look up and synchronously wait for the process to exit.
// This is not possible on other systems, since Wait doesn't work on most systems unless the
// process is a child of the current one (see doc on process.Wait).
for {
process, err := os.FindProcess(ppid)
if err != nil {
err := fmt.Errorf("collector was orphaned, error finding process %d: %w", ppid, err)
status := component.NewFatalErrorEvent(err)
reportStatus(status)
return
}
if process == nil {
err := fmt.Errorf("collector was orphaned, process %d does not exist", ppid)
status := component.NewFatalErrorEvent(err)
reportStatus(status)
return
}
processState, err := process.Wait()
if err != nil {
err := fmt.Errorf("collector was orphaned, error while waiting on process %d to exit: %w", ppid, err)
status := component.NewFatalErrorEvent(err)
reportStatus(status)
return
}
if processState.Exited() {
err := fmt.Errorf("collector was orphaned, process %d exited: %w", ppid, err)
status := component.NewFatalErrorEvent(err)
reportStatus(status)
return
}
select {
case <-time.After(orphanPollInterval):
case <-ctx.Done():
return
}
}
}