forked from DataDog/dd-trace-go
/
profiler.go
370 lines (343 loc) · 11.1 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package profiler
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"sync"
"time"
"github.com/gihyunk/dd-trace-go/internal"
"github.com/gihyunk/dd-trace-go/internal/log"
pprofile "github.com/google/pprof/profile"
)
// outChannelSize specifies the size of the profile output channel.
const outChannelSize = 5
var (
mu sync.Mutex
activeProfiler *profiler
containerID = internal.ContainerID() // replaced in tests
)
// Start starts the profiler. It may return an error if an API key is not provided by means of
// the WithAPIKey option, or if a hostname is not found.
func Start(opts ...Option) error {
mu.Lock()
defer mu.Unlock()
if activeProfiler != nil {
activeProfiler.stop()
}
p, err := newProfiler(opts...)
if err != nil {
return err
}
activeProfiler = p
activeProfiler.run()
return nil
}
// Stop cancels any ongoing profiling or upload operations and returns after
// everything has been stopped.
func Stop() {
mu.Lock()
if activeProfiler != nil {
activeProfiler.stop()
activeProfiler = nil
}
mu.Unlock()
}
// profiler collects and sends preset profiles to the Datadog API at a given frequency
// using a given configuration.
type profiler struct {
mu sync.Mutex
cfg *config // profile configuration
out chan batch // upload queue
uploadFunc func(batch) error // defaults to (*profiler).upload; replaced in tests
exit chan struct{} // exit signals the profiler to stop; it is closed after stopping
stopOnce sync.Once // stopOnce ensures the profiler is stopped exactly once.
wg sync.WaitGroup // wg waits for all goroutines to exit when stopping.
met *metrics // metric collector state
prev map[string]*pprofile.Profile // previous collection results for delta profiling
testHooks testHooks
}
// testHooks are functions that are replaced during testing which would normally
// depend on accessing runtime state that is not needed/available for the test
type testHooks struct {
startCPUProfile func(w io.Writer) error
stopCPUProfile func()
lookupProfile func(name string, w io.Writer, debug int) error
}
func (p *profiler) startCPUProfile(w io.Writer) error {
if p.testHooks.startCPUProfile != nil {
return p.testHooks.startCPUProfile(w)
}
return pprof.StartCPUProfile(w)
}
func (p *profiler) stopCPUProfile() {
if p.testHooks.startCPUProfile != nil {
p.testHooks.stopCPUProfile()
return
}
pprof.StopCPUProfile()
}
func (p *profiler) lookupProfile(name string, w io.Writer, debug int) error {
if p.testHooks.lookupProfile != nil {
return p.testHooks.lookupProfile(name, w, debug)
}
prof := pprof.Lookup(name)
if prof == nil {
return errors.New("profile not found")
}
return prof.WriteTo(w, debug)
}
// newProfiler creates a new, unstarted profiler.
func newProfiler(opts ...Option) (*profiler, error) {
cfg, err := defaultConfig()
if err != nil {
return nil, err
}
for _, opt := range opts {
opt(cfg)
}
// TODO(fg) remove this after making expGoroutineWaitProfile public.
if os.Getenv("DD_PROFILING_WAIT_PROFILE") != "" {
cfg.addProfileType(expGoroutineWaitProfile)
}
// Agentless upload is disabled by default as of v1.30.0, but
// WithAgentlessUpload can be used to enable it for testing and debugging.
if cfg.agentless {
if !isAPIKeyValid(cfg.apiKey) {
return nil, errors.New("profiler.WithAgentlessUpload requires a valid API key. Use profiler.WithAPIKey or the DD_API_KEY env variable to set it")
}
// Always warn people against using this mode for now. All customers should
// use agent based uploading at this point.
log.Warn("profiler.WithAgentlessUpload is currently for internal usage only and not officially supported.")
cfg.targetURL = cfg.apiURL
} else {
// Historically people could use an API Key to enable agentless uploading.
// As of v1.30.0 customers the default behavior is to use agent based
// uploading regardless of the presence of an API key. So if we see an API
// key configured, we warn the customers that this is probably a
// misconfiguration.
if cfg.apiKey != "" {
log.Warn("You are currently setting profiler.WithAPIKey or the DD_API_KEY env variable, but as of dd-trace-go v1.30.0 this value is getting ignored by the profiler. Please see the profiler.WithAPIKey go docs and verify that your integration is still working. If you can't remove DD_API_KEY from your environment, you can use WithAPIKey(\"\") to silence this warning.")
}
cfg.targetURL = cfg.agentURL
}
if cfg.hostname == "" {
hostname, err := os.Hostname()
if err != nil {
if cfg.targetURL == cfg.apiURL {
return nil, fmt.Errorf("could not obtain hostname: %v", err)
}
log.Warn("unable to look up hostname: %v", err)
}
cfg.hostname = hostname
}
// uploadTimeout defaults to DefaultUploadTimeout, but in theory a user might
// set it to 0 or a negative value. However, it's not clear what this should
// mean, and most meanings we could assign seem to be bad: Not having a
// timeout is dangerous, having a timeout that fires immediately breaks
// uploading, and silently defaulting to the default timeout is confusing.
// So let's just stay clear of all of this by not allowing such values.
//
// see similar discussion: https://github.com/golang/go/issues/39177
if cfg.uploadTimeout <= 0 {
return nil, fmt.Errorf("invalid upload timeout, must be > 0: %s", cfg.uploadTimeout)
}
for pt := range cfg.types {
if _, ok := profileTypes[pt]; !ok {
return nil, fmt.Errorf("unknown profile type: %d", pt)
}
}
if cfg.logStartup {
logStartup(cfg)
}
p := profiler{
cfg: cfg,
out: make(chan batch, outChannelSize),
exit: make(chan struct{}),
met: newMetrics(),
prev: make(map[string]*pprofile.Profile),
}
p.uploadFunc = p.upload
return &p, nil
}
// run runs the profiler.
func (p *profiler) run() {
if _, ok := p.cfg.types[MutexProfile]; ok {
runtime.SetMutexProfileFraction(p.cfg.mutexFraction)
}
if _, ok := p.cfg.types[BlockProfile]; ok {
runtime.SetBlockProfileRate(p.cfg.blockRate)
}
p.wg.Add(1)
go func() {
defer p.wg.Done()
tick := time.NewTicker(p.cfg.period)
defer tick.Stop()
p.met.reset(now()) // collect baseline metrics at profiler start
p.collect(tick.C)
}()
p.wg.Add(1)
go func() {
defer p.wg.Done()
p.send()
}()
}
// collect runs the profile types found in the configuration whenever the ticker receives
// an item.
func (p *profiler) collect(ticker <-chan time.Time) {
defer close(p.out)
var (
// mu guards completed
mu sync.Mutex
completed []*profile
wg sync.WaitGroup
)
for {
select {
case <-ticker:
now := now()
bat := batch{
host: p.cfg.hostname,
start: now,
// NB: while this is technically wrong in that it does not
// record the actual start and end timestamps for the batch,
// it is how the backend understands the client-side
// configured CPU profile duration: (start-end).
end: now.Add(p.cfg.cpuDuration),
}
completed = completed[:0]
for _, t := range p.enabledProfileTypes() {
wg.Add(1)
go func(t ProfileType) {
defer wg.Done()
profs, err := p.runProfile(t)
if err != nil {
log.Error("Error getting %s profile: %v; skipping.", t, err)
p.cfg.statsd.Count("datadog.profiler.go.collect_error", 1, append(p.cfg.tags, t.Tag()), 1)
}
mu.Lock()
defer mu.Unlock()
completed = append(completed, profs...)
}(t)
}
wg.Wait()
for _, prof := range completed {
bat.addProfile(prof)
}
p.enqueueUpload(bat)
case <-p.exit:
return
}
}
}
// enabledProfileTypes returns the enabled profile types in a deterministic
// order. The CPU profile always comes first because people might spot
// interesting events in there and then try to look for the counter-part event
// in the mutex/heap/block profile. Deterministic ordering is also important
// for delta profiles, otherwise they'd cover varying profiling periods.
func (p *profiler) enabledProfileTypes() []ProfileType {
order := []ProfileType{
CPUProfile,
HeapProfile,
BlockProfile,
MutexProfile,
GoroutineProfile,
expGoroutineWaitProfile,
MetricsProfile,
}
enabled := []ProfileType{}
for _, t := range order {
if _, ok := p.cfg.types[t]; ok {
enabled = append(enabled, t)
}
}
return enabled
}
// enqueueUpload pushes a batch of profiles onto the queue to be uploaded. If there is no room, it will
// evict the oldest profile to make some. Typically a batch would be one of each enabled profile.
func (p *profiler) enqueueUpload(bat batch) {
for {
select {
case p.out <- bat:
return // 👍
default:
// queue is full; evict oldest
select {
case <-p.out:
p.cfg.statsd.Count("datadog.profiler.go.queue_full", 1, p.cfg.tags, 1)
log.Warn("Evicting one profile batch from the upload queue to make room.")
default:
// this case should be almost impossible to trigger, it would require a
// full p.out to completely drain within nanoseconds or extreme
// scheduling decisions by the runtime.
}
}
}
}
// send takes profiles from the output queue and uploads them.
func (p *profiler) send() {
for {
select {
case <-p.exit:
return
case bat := <-p.out:
if err := p.outputDir(bat); err != nil {
log.Error("Failed to output profile to dir: %v", err)
}
if err := p.uploadFunc(bat); err != nil {
log.Error("Failed to upload profile: %v", err)
}
}
}
}
func (p *profiler) outputDir(bat batch) error {
if p.cfg.outputDir == "" {
return nil
}
// Basic ISO 8601 Format in UTC as the name for the directories.
dir := bat.end.UTC().Format("20060102T150405Z")
dirPath := filepath.Join(p.cfg.outputDir, dir)
// 0755 is what mkdir does, should be reasonable for the use cases here.
if err := os.MkdirAll(dirPath, 0755); err != nil {
return err
}
for _, prof := range bat.profiles {
filePath := filepath.Join(dirPath, prof.name)
// 0644 is what touch does, should be reasonable for the use cases here.
if err := ioutil.WriteFile(filePath, prof.data, 0644); err != nil {
return err
}
}
return nil
}
// interruptibleSleep sleeps for the given duration or until interrupted by the
// p.exit channel being closed.
func (p *profiler) interruptibleSleep(d time.Duration) {
select {
case <-p.exit:
case <-time.After(d):
}
}
// stop stops the profiler.
func (p *profiler) stop() {
p.stopOnce.Do(func() {
close(p.exit)
})
p.wg.Wait()
}
// StatsdClient implementations can count and time certain event occurrences that happen
// in the profiler.
type StatsdClient interface {
// Count counts how many times an event happened, at the given rate using the given tags.
Count(event string, times int64, tags []string, rate float64) error
// Timing creates a distribution of the values registered as the duration of a certain event.
Timing(event string, duration time.Duration, tags []string, rate float64) error
}