/
profiler.go
87 lines (76 loc) · 2.37 KB
/
profiler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Package profiler is a public API golang apps should use to send data to pyroscope server. It is intentionally separate from the rest of the code.
// The idea is that this API won't change much over time, while all the other code will.
package profiler
import (
"fmt"
"time"
"github.com/pyroscope-io/pyroscope/pkg/agent"
"github.com/pyroscope-io/pyroscope/pkg/agent/spy"
"github.com/pyroscope-io/pyroscope/pkg/agent/types"
"github.com/pyroscope-io/pyroscope/pkg/agent/upstream/remote"
)
type ProfileType = spy.ProfileType
var (
ProfileCPU = spy.ProfileCPU
ProfileAllocObjects = spy.ProfileAllocObjects
ProfileAllocSpace = spy.ProfileAllocSpace
ProfileInuseObjects = spy.ProfileInuseObjects
ProfileInuseSpace = spy.ProfileInuseSpace
)
type Config struct {
ApplicationName string // e.g backend.purchases
ServerAddress string // e.g http://pyroscope.services.internal:4040
AuthToken string // specify this token when using pyroscope cloud
SampleRate uint32
Logger agent.Logger
ProfileTypes []ProfileType
DisableGCRuns bool // this will disable automatic runtime.GC runs
}
type Profiler struct {
session *agent.ProfileSession
}
// Start starts continuously profiling go code
func Start(cfg Config) (*Profiler, error) {
if len(cfg.ProfileTypes) == 0 {
cfg.ProfileTypes = types.DefaultProfileTypes
}
if cfg.SampleRate == 0 {
cfg.SampleRate = types.DefaultSampleRate
}
if cfg.Logger == nil {
cfg.Logger = &agent.NoopLogger{}
}
rc := remote.RemoteConfig{
AuthToken: cfg.AuthToken,
UpstreamAddress: cfg.ServerAddress,
UpstreamThreads: 4,
UpstreamRequestTimeout: 30 * time.Second,
}
upstream, err := remote.New(rc, cfg.Logger)
if err != nil {
return nil, err
}
sc := agent.SessionConfig{
Upstream: upstream,
AppName: cfg.ApplicationName,
ProfilingTypes: types.DefaultProfileTypes,
DisableGCRuns: cfg.DisableGCRuns,
SpyName: types.GoSpy,
SampleRate: cfg.SampleRate,
UploadRate: 10 * time.Second,
Pid: 0,
WithSubprocesses: false,
}
session := agent.NewSession(&sc, cfg.Logger)
if err := session.Start(); err != nil {
return nil, fmt.Errorf("start session: %v", err)
}
return &Profiler{
session: session,
}, nil
}
// Stop stops continious profiling session
func (p *Profiler) Stop() error {
p.session.Stop()
return nil
}