-
Notifications
You must be signed in to change notification settings - Fork 50
/
process.go
75 lines (64 loc) · 1.42 KB
/
process.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package containers
import (
"context"
"os"
"time"
"github.com/jpillora/backoff"
"github.com/cilium/ebpf/link"
"github.com/coroot/coroot-node-agent/proc"
"github.com/mdlayher/taskstats"
)
type Process struct {
Pid uint32
StartedAt time.Time
NetNsId string
ctx context.Context
cancelFunc context.CancelFunc
dotNetMonitor *DotNetMonitor
uprobes []link.Link
goTlsUprobesChecked bool
openSslUprobesChecked bool
}
func NewProcess(pid uint32, stats *taskstats.Stats) *Process {
ns, err := proc.GetNetNs(pid)
if err != nil {
return nil
}
defer ns.Close()
p := &Process{Pid: pid, StartedAt: stats.BeginTime, NetNsId: ns.UniqueId()}
p.ctx, p.cancelFunc = context.WithCancel(context.Background())
go p.instrument()
return p
}
func (p *Process) isHostNs() bool {
return p.NetNsId == hostNetNsId
}
func (p *Process) instrument() {
b := backoff.Backoff{Factor: 2, Min: time.Second, Max: time.Minute}
for {
select {
case <-p.ctx.Done():
return
default:
dest, err := os.Readlink(proc.Path(p.Pid, "exe"))
if err != nil {
return
}
if dest != "/" {
if dotNetAppName, err := dotNetApp(p.Pid); err == nil {
if dotNetAppName != "" {
p.dotNetMonitor = NewDotNetMonitor(p.ctx, p.Pid, dotNetAppName)
}
return
}
}
time.Sleep(b.Duration())
}
}
}
func (p *Process) Close() {
p.cancelFunc()
for _, u := range p.uprobes {
_ = u.Close()
}
}