Skip to content

Commit 6af5383

Browse files
committed
exec improvements, extra messages for when agent can't start
1 parent 7c1ca7a commit 6af5383

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

pkg/agent/session.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,3 @@ func findAllSubprocesses(pid int) []int {
201201

202202
return res
203203
}
204-
205-
// TODO: add this check back
206-
207-
// func isRoot() bool {
208-
// u, err := user.Current()
209-
// return err == nil && u.Username == "root"
210-
// }
211-
212-
// func printDarwinMessage() {
213-
// if runtime.GOOS == "darwin" {
214-
// if !isRoot() {
215-
// log.Error("on macOS it is required to run the agent with sudo")
216-
// }
217-
// }
218-
// }

pkg/cli/cli_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type FlagsStruct struct {
1818
Foos []string
1919
Bar int
2020
Baz time.Duration
21+
FooBar string
2122
}
2223

2324
var _ = Describe("config package", func() {
@@ -41,13 +42,15 @@ var _ = Describe("config package", func() {
4142
"-foos", "test-val-3",
4243
"-bar", "123",
4344
"-baz", "10h",
45+
"-foo-bar", "test-val-4",
4446
})
4547

4648
Expect(err).ToNot(HaveOccurred())
4749
Expect(cfg.Foo).To(Equal("test-val-1"))
4850
Expect(cfg.Foos).To(Equal([]string{"test-val-2", "test-val-3"}))
4951
Expect(cfg.Bar).To(Equal(123))
5052
Expect(cfg.Baz).To(Equal(10 * time.Hour))
53+
Expect(cfg.FooBar).To(Equal("test-val-4"))
5154
})
5255
})
5356

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

8286
It("arguments take precendence", func() {

pkg/cli/example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ foos:
55
- "test-val-3"
66
bar: 123
77
baz: "10h"
8+
foo-bar: "test-val-4"

pkg/exec/cli.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"os"
77
"os/exec"
88
"os/signal"
9+
"os/user"
910
"path"
11+
"runtime"
1012
"strings"
1113
"syscall"
1214
"time"
@@ -33,20 +35,21 @@ func Cli(cfg *config.Config, args []string) error {
3335
supportedSpies := supportedSpiesWithoutGospy()
3436
suggestedCommand := fmt.Sprintf("pyroscope exec -spy-name %s %s", supportedSpies[0], strings.Join(args, " "))
3537
return fmt.Errorf(
36-
"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",
38+
"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",
3739
baseName,
3840
color.YellowString("-spy-name"),
3941
color.YellowString(suggestedCommand),
4042
strings.Join(supportedSpies, ","),
43+
armMessage(),
4144
color.BlueString("https://github.com/pyroscope-io/pyroscope/issues"),
4245
)
4346
}
4447
}
4548

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

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

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

98101
return supportedSpies
99102
}
103+
104+
func performChecks(spyName string) error {
105+
if spyName == "gospy" {
106+
return fmt.Errorf("gospy can not profile other processes. See our documentation on using gospy: %s", color.BlueString("https://pyroscope.io/docs/"))
107+
}
108+
109+
if runtime.GOOS == "darwin" {
110+
if !isRoot() {
111+
logrus.Error("on macOS you're required to run the agent with sudo")
112+
}
113+
}
114+
115+
if stringsContains(spy.SupportedSpies, spyName) {
116+
supportedSpies := supportedSpiesWithoutGospy()
117+
return fmt.Errorf(
118+
"Spy \"%s\" is not supported. Available spies are: %s\n%s",
119+
color.BlueString("spyName"),
120+
strings.Join(supportedSpies, ","),
121+
armMessage(),
122+
)
123+
}
124+
125+
return nil
126+
}
127+
128+
func stringsContains(arr []string, element string) bool {
129+
for _, v := range arr {
130+
if v == element {
131+
return true
132+
}
133+
}
134+
return false
135+
}
136+
137+
func isRoot() bool {
138+
u, err := user.Current()
139+
return err == nil && u.Username == "root"
140+
}
141+
142+
func armMessage() string {
143+
if runtime.GOARCH == "arm64" {
144+
return "Note that rbspy is not available on arm64 platform"
145+
}
146+
return ""
147+
}

0 commit comments

Comments
 (0)