Преглед на файлове

Merge pull request #73 from coroot/fix_dotnet_app_detection

retry app type detection during container initialization
Nikolay Sivko преди 2 години
родител
ревизия
6b886112eb
променени са 1 файла, в които са добавени 23 реда и са изтрити 5 реда
  1. 23 5
      containers/process.go

+ 23 - 5
containers/process.go

@@ -2,8 +2,11 @@ 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"
@@ -32,7 +35,7 @@ func NewProcess(pid uint32, stats *taskstats.Stats) *Process {
 	defer ns.Close()
 	p := &Process{Pid: pid, StartedAt: stats.BeginTime, NetNsId: ns.UniqueId()}
 	p.ctx, p.cancelFunc = context.WithCancel(context.Background())
-	p.instrument()
+	go p.instrument()
 	return p
 }
 
@@ -41,11 +44,26 @@ func (p *Process) isHostNs() bool {
 }
 
 func (p *Process) instrument() {
-	if dotNetAppName, err := dotNetApp(p.Pid); err == nil {
-		if dotNetAppName != "" {
-			p.dotNetMonitor = NewDotNetMonitor(p.ctx, p.Pid, dotNetAppName)
+	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())
 		}
-		return
 	}
 }