process.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package containers
  2. import (
  3. "context"
  4. "time"
  5. "github.com/cilium/ebpf/link"
  6. "github.com/coroot/coroot-node-agent/proc"
  7. "github.com/mdlayher/taskstats"
  8. )
  9. type Process struct {
  10. Pid uint32
  11. StartedAt time.Time
  12. NetNsId string
  13. ctx context.Context
  14. cancelFunc context.CancelFunc
  15. dotNetMonitor *DotNetMonitor
  16. uprobes []link.Link
  17. goTlsUprobesChecked bool
  18. openSslUprobesChecked bool
  19. }
  20. func NewProcess(pid uint32, stats *taskstats.Stats) *Process {
  21. ns, err := proc.GetNetNs(pid)
  22. if err != nil {
  23. return nil
  24. }
  25. defer ns.Close()
  26. p := &Process{Pid: pid, StartedAt: stats.BeginTime, NetNsId: ns.UniqueId()}
  27. p.ctx, p.cancelFunc = context.WithCancel(context.Background())
  28. p.instrument()
  29. return p
  30. }
  31. func (p *Process) isHostNs() bool {
  32. return p.NetNsId == hostNetNsId
  33. }
  34. func (p *Process) instrument() {
  35. if dotNetAppName, err := dotNetApp(p.Pid); err == nil {
  36. if dotNetAppName != "" {
  37. p.dotNetMonitor = NewDotNetMonitor(p.ctx, p.Pid, dotNetAppName)
  38. }
  39. return
  40. }
  41. }
  42. func (p *Process) Close() {
  43. p.cancelFunc()
  44. for _, u := range p.uprobes {
  45. _ = u.Close()
  46. }
  47. }