forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
profiling.go
93 lines (77 loc) · 2.5 KB
/
profiling.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
/// 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-present Datadog, Inc.
package profiling
import (
"sync"
"time"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
"github.com/StackVista/stackstate-agent/pkg/util/log"
)
var (
mu sync.RWMutex
running bool
)
const (
// ProfileURLTemplate constant template for expected profiling endpoint URL.
ProfileURLTemplate = "https://intake.profile.%s/v1/input"
// ProfilingLocalURLTemplate is the constant used to compute the URL of the local trace agent
ProfilingLocalURLTemplate = "http://%v/profiling/v1/input"
// DefaultProfilingPeriod defines the default profiling period
DefaultProfilingPeriod = 5 * time.Minute
)
// Start initiates profiling with the supplied parameters;
// this function is thread-safe.
func Start(settings Settings) error {
mu.Lock()
defer mu.Unlock()
if running {
return nil
}
settings.applyDefaults()
types := []profiler.ProfileType{profiler.CPUProfile, profiler.HeapProfile}
if settings.WithGoroutineProfile {
types = append(types, profiler.GoroutineProfile)
}
options := []profiler.Option{
profiler.WithEnv(settings.Env),
profiler.WithService(settings.Service),
profiler.WithURL(settings.Site),
profiler.WithPeriod(settings.Period),
profiler.WithProfileTypes(types...),
profiler.CPUDuration(settings.CPUDuration),
profiler.WithTags(settings.Tags...),
}
// If block or mutex profiling was configured via runtime configuration, pass current
// values to profiler. This prevents profiler from resetting mutex profile rate to the
// default value; and enables collection of blocking profile data if it is enabled.
if settings.MutexProfileFraction > 0 {
options = append(options, profiler.MutexProfileFraction(settings.MutexProfileFraction))
}
if settings.BlockProfileRate > 0 {
options = append(options, profiler.BlockProfileRate(settings.BlockProfileRate))
}
err := profiler.Start(options...)
if err == nil {
running = true
log.Debugf("Profiling started! Submitting to: %s", settings.Site)
}
return err
}
// Stop stops the profiler if running - idempotent; this function is thread-safe.
func Stop() {
mu.Lock()
defer mu.Unlock()
if running {
profiler.Stop()
running = false
}
}
// IsRunning returns true if the profiler is running; this function is thread-safe.
func IsRunning() bool {
mu.RLock()
v := running
mu.RUnlock()
return v
}