Skip to content

Commit

Permalink
exec improvements, extra messages for when agent can't start
Browse files Browse the repository at this point in the history
  • Loading branch information
petethepig committed Jan 18, 2021
1 parent 7c1ca7a commit 6af5383
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
15 changes: 0 additions & 15 deletions pkg/agent/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,3 @@ func findAllSubprocesses(pid int) []int {

return res
}

// TODO: add this check back

// func isRoot() bool {
// u, err := user.Current()
// return err == nil && u.Username == "root"
// }

// func printDarwinMessage() {
// if runtime.GOOS == "darwin" {
// if !isRoot() {
// log.Error("on macOS it is required to run the agent with sudo")
// }
// }
// }
4 changes: 4 additions & 0 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type FlagsStruct struct {
Foos []string
Bar int
Baz time.Duration
FooBar string
}

var _ = Describe("config package", func() {
Expand All @@ -41,13 +42,15 @@ var _ = Describe("config package", func() {
"-foos", "test-val-3",
"-bar", "123",
"-baz", "10h",
"-foo-bar", "test-val-4",
})

Expect(err).ToNot(HaveOccurred())
Expect(cfg.Foo).To(Equal("test-val-1"))
Expect(cfg.Foos).To(Equal([]string{"test-val-2", "test-val-3"}))
Expect(cfg.Bar).To(Equal(123))
Expect(cfg.Baz).To(Equal(10 * time.Hour))
Expect(cfg.FooBar).To(Equal("test-val-4"))
})
})

Expand Down Expand Up @@ -77,6 +80,7 @@ var _ = Describe("config package", func() {
Expect(cfg.Foos).To(Equal([]string{"test-val-2", "test-val-3"}))
Expect(cfg.Bar).To(Equal(123))
Expect(cfg.Baz).To(Equal(10 * time.Hour))
Expect(cfg.FooBar).To(Equal("test-val-4"))
})

It("arguments take precendence", func() {
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ foos:
- "test-val-3"
bar: 123
baz: "10h"
foo-bar: "test-val-4"
54 changes: 51 additions & 3 deletions pkg/exec/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"os"
"os/exec"
"os/signal"
"os/user"
"path"
"runtime"
"strings"
"syscall"
"time"
Expand All @@ -33,20 +35,21 @@ func Cli(cfg *config.Config, args []string) error {
supportedSpies := supportedSpiesWithoutGospy()
suggestedCommand := fmt.Sprintf("pyroscope exec -spy-name %s %s", supportedSpies[0], strings.Join(args, " "))
return fmt.Errorf(
"could not automatically find a spy for program \"%s\". Pass spy name via %s argument, for example: \n %s\n\nAvailable spies are: %s\n\nIf you believe this is a mistake, please submit an issue at %s",
"could not automatically find a spy for program \"%s\". Pass spy name via %s argument, for example: \n %s\n\nAvailable spies are: %s\n%s\nIf you believe this is a mistake, please submit an issue at %s",
baseName,
color.YellowString("-spy-name"),
color.YellowString(suggestedCommand),
strings.Join(supportedSpies, ","),
armMessage(),
color.BlueString("https://github.com/pyroscope-io/pyroscope/issues"),
)
}
}

logrus.Info("to disable logging from pyroscope, pass " + color.YellowString("-no-logging") + " argument to pyroscope exec")

if spyName == "gospy" {
return fmt.Errorf("gospy can not profile other processes. See our documentation on using gospy: %s", color.BlueString("https://pyroscope.io/docs/"))
if err := performChecks(spyName); err != nil {
return err
}

signal.Ignore(syscall.SIGCHLD)
Expand Down Expand Up @@ -97,3 +100,48 @@ func supportedSpiesWithoutGospy() []string {

return supportedSpies
}

func performChecks(spyName string) error {
if spyName == "gospy" {
return fmt.Errorf("gospy can not profile other processes. See our documentation on using gospy: %s", color.BlueString("https://pyroscope.io/docs/"))
}

if runtime.GOOS == "darwin" {
if !isRoot() {
logrus.Error("on macOS you're required to run the agent with sudo")
}
}

if stringsContains(spy.SupportedSpies, spyName) {
supportedSpies := supportedSpiesWithoutGospy()
return fmt.Errorf(
"Spy \"%s\" is not supported. Available spies are: %s\n%s",
color.BlueString("spyName"),
strings.Join(supportedSpies, ","),
armMessage(),
)
}

return nil
}

func stringsContains(arr []string, element string) bool {
for _, v := range arr {
if v == element {
return true
}
}
return false
}

func isRoot() bool {
u, err := user.Current()
return err == nil && u.Username == "root"
}

func armMessage() string {
if runtime.GOARCH == "arm64" {
return "Note that rbspy is not available on arm64 platform"
}
return ""
}

0 comments on commit 6af5383

Please sign in to comment.