Skip to content

Commit

Permalink
Better container pid detection on Linux (#53)
Browse files Browse the repository at this point in the history
* Better container pid detection on Linux

* Use match groups
  • Loading branch information
pglombardo authored Jul 24, 2018
1 parent a5e9b88 commit 9dcca59
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion fsm.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package instana

import (
"bufio"
"fmt"
"net"
"os"
"os/exec"
"regexp"
"strconv"
"time"

Expand Down Expand Up @@ -137,7 +139,30 @@ func (r *fsmS) announceSensor(e *f.Event) {
}
}()

d := &discoveryS{PID: os.Getpid()}
pid := 0
schedFile := fmt.Sprintf("/proc/%d/sched", os.Getpid())
if _, err := os.Stat(schedFile); err == nil {
sf, err := os.Open(schedFile)
defer sf.Close()
if err == nil {
fscanner := bufio.NewScanner(sf)
fscanner.Scan()
primaLinea := fscanner.Text()

r := regexp.MustCompile("\\((\\d+),")
match := r.FindStringSubmatch(primaLinea)
i, err := strconv.Atoi(match[1])
if err == nil {
pid = i
}
}
}

if pid == 0 {
pid = os.Getpid()
}

d := &discoveryS{PID: pid}
d.Name, d.Args = getCommandLine()

if _, err := os.Stat("/proc"); err == nil {
Expand Down

0 comments on commit 9dcca59

Please sign in to comment.