Skip to content

Commit ff206a3

Browse files
committed
support for auth tokens, atexit fix
1 parent bc7aa2f commit ff206a3

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

pkg/agent/profiler/profiler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type Config struct {
1313
ApplicationName string // e.g backend.purchases
1414
ServerAddress string // e.g http://pyroscope.services.internal:4040
15+
AuthToken string
1516
}
1617

1718
type Profiler struct {
@@ -21,6 +22,7 @@ type Profiler struct {
2122
// Start starts continuously profiling go code
2223
func Start(cfg Config) (*Profiler, error) {
2324
u := remote.New(remote.RemoteConfig{
25+
AuthToken: cfg.AuthToken,
2426
UpstreamAddress: cfg.ServerAddress,
2527
UpstreamThreads: 4,
2628
UpstreamRequestTimeout: 30 * time.Second,

pkg/agent/upstream/remote/remote.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Remote struct {
3131
}
3232

3333
type RemoteConfig struct {
34+
AuthToken string
3435
UpstreamThreads int
3536
UpstreamAddress string
3637
UpstreamRequestTimeout time.Duration
@@ -99,7 +100,18 @@ func (u *Remote) uploadProfile(j *uploadJob) {
99100
urlObj.RawQuery = q.Encode()
100101
buf := j.t.Bytes()
101102
log.Info("uploading at ", urlObj.String())
102-
resp, err := u.client.Post(urlObj.String(), "binary/octet-stream+trie", bytes.NewReader(buf))
103+
104+
req, err := http.NewRequest("POST", urlObj.String(), bytes.NewReader(buf))
105+
if err != nil {
106+
log.Error("Error happened when uploading a profile:", err)
107+
return
108+
}
109+
req.Header.Set("Content-Type", "binary/octet-stream+trie")
110+
if u.cfg.AuthToken != "" {
111+
req.Header.Set("Authorization", "Bearer "+u.cfg.AuthToken)
112+
}
113+
resp, err := u.client.Do(req)
114+
103115
if err != nil {
104116
log.Error("Error happened when uploading a profile:", err)
105117
}

pkg/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Agent struct {
2222
AgentSpyName string `desc:"name of the spy you want to use"` // TODO: add options
2323
AgentPID int `def:"-1" desc:"pid of the process you want to spy on"`
2424
ServerAddress string `def:"http://localhost:4040" desc:"address of the pyroscope server"`
25+
AuthToken string `def:"" desc:"authorization token used to upload profiling data"`
2526
UpstreamThreads int `def:"4"`
2627
UpstreamRequestTimeout time.Duration `def:"10s"`
2728
UNIXSocketPath string `def:"<installPrefix>/var/run/pyroscope-agent.sock" desc:"path to a UNIX socket file"`
@@ -52,7 +53,7 @@ type Server struct {
5253
MaxNodesSerialization int `def:"2048" desc:"max number of nodes used when saving profiles to disk"`
5354
MaxNodesRender int `def:"2048" desc:"max number of nodes used to display data on the frontend"`
5455

55-
// current only used in our demo app
56+
// currently only used in our demo app
5657
HideApplications []string `def:""`
5758

5859
AnalyticsOptOut bool `def:"false" desc:"disables analytics"`
@@ -79,6 +80,7 @@ type Exec struct {
7980
DetectSubprocesses bool `def:"true" desc:"makes pyroscope keep track of and profile subprocesses of the main process"`
8081
LogLevel string `def:"info", desc:"debug|info|warn|error"`
8182
ServerAddress string `def:"http://localhost:4040" desc:"address of the pyroscope server"`
83+
AuthToken string `def:"" desc:"authorization token used to upload profiling data"`
8284
UpstreamThreads int `def:"4" desc:"number of upload threads"`
8385
UpstreamRequestTimeout time.Duration `def:"10s" desc:"profile upload timeout"`
8486
NoLogging bool `def:"false" desc:"disables logging from pyroscope"`

pkg/util/atexit/atexit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package atexit
33
import (
44
"os"
55
"os/signal"
6+
"sync"
67
"syscall"
78
)
89

910
var callbacks []func()
1011

11-
func init() {
12+
var once sync.Once
13+
14+
func initSignalHandler() {
1215
signalChan := make(chan os.Signal, 1)
1316
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
1417
go func() {
@@ -20,5 +23,6 @@ func init() {
2023
}
2124

2225
func Register(cb func()) {
26+
once.Do(initSignalHandler)
2327
callbacks = append(callbacks, cb)
2428
}

0 commit comments

Comments
 (0)