-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.go
More file actions
226 lines (192 loc) · 4.76 KB
/
config.go
File metadata and controls
226 lines (192 loc) · 4.76 KB
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
package profiler
import (
"net/http"
"os"
"runtime"
"strconv"
"time"
"github.com/rs/zerolog"
)
type Option func(*config)
type config struct {
httpClient *http.Client
cpuDuration time.Duration
period time.Duration
uploadTimeout time.Duration
cpuProfileRate int
agentSocket string
types []ProfileType
labels map[string]string
serverId string
serverToken string
}
var (
DefaultProfileTypes = []ProfileType{CPUProfile}
)
const (
DefaultCPUDuration = 45 * time.Second
defaultPeriod = 45 * time.Second
DefaultUploadTimeout = 10 * time.Second
)
func initDefaultConfig() (*config, error) {
c := &config{
cpuDuration: DefaultCPUDuration,
period: defaultPeriod,
uploadTimeout: DefaultUploadTimeout,
agentSocket: DefaultAgentSocket,
types: DefaultProfileTypes,
}
logger, err := newLoggerFromEnv()
if err != nil {
logger.Error().Msgf("%v", err)
}
setGlobalLogger(logger)
if v := os.Getenv("BLACKFIRE_AGENT_SOCKET"); v != "" {
c.agentSocket = v
}
if v := os.Getenv("BLACKFIRE_SERVER_ID"); v != "" {
c.serverId = v
}
if v := os.Getenv("BLACKFIRE_SERVER_TOKEN"); v != "" {
c.serverToken = v
}
if v := os.Getenv("BLACKFIRE_CONPROF_CPU_DURATION"); v != "" {
d, err := strconv.Atoi(v)
if err != nil {
log.Error().Msgf("Invalid CPU duration value.(%d)", d)
} else {
c.cpuDuration = time.Duration(d) * time.Second
}
}
// undocumented
if v := os.Getenv("BLACKFIRE_CONPROF_PERIOD"); v != "" {
d, err := strconv.Atoi(v)
if err != nil {
log.Error().Msgf("Invalid period value.(%d)", d)
} else {
c.period = time.Duration(d) * time.Second
}
}
if v := os.Getenv("BLACKFIRE_CONPROF_CPU_PROFILERATE"); v != "" {
d, err := strconv.Atoi(v)
if err != nil {
log.Error().Msgf("Invalid CPU profile rate value.(%d)", d)
} else {
c.cpuProfileRate = d
}
}
if v := os.Getenv("BLACKFIRE_CONPROF_UPLOAD_TIMEOUT"); v != "" {
d, err := strconv.Atoi(v)
if err != nil {
log.Error().Msgf("Invalid upload timeout value.(%d)", d)
} else {
c.uploadTimeout = time.Duration(d) * time.Second
}
}
if c.cpuDuration > c.period {
c.cpuDuration = c.period
}
// Populate default labels.
c.labels = map[string]string{
"language": "go",
"runtime": "go",
"runtime_os": runtime.GOOS,
"runtime_arch": runtime.GOARCH,
"runtime_version": runtime.Version(),
"probe_version": Version.String(),
}
if hostname, err := os.Hostname(); err == nil {
c.labels["host"] = hostname
}
// Collect more labels from environment variables. Priority matters for the same label name.
lookup := []struct {
labelName string
envVar string
}{
{"application_name", "BLACKFIRE_CONPROF_APP_NAME"},
{"application_name", "PLATFORM_APPLICATION_NAME"},
{"project_id", "PLATFORM_PROJECT"},
}
for _, entry := range lookup {
// Don't override
if _, exists := c.labels[entry.labelName]; exists {
continue
}
if v := os.Getenv(entry.envVar); v != "" {
c.labels[entry.labelName] = v
}
}
return c, nil
}
func WithCPUDuration(d time.Duration) Option {
return func(cfg *config) {
cfg.cpuDuration = d
}
}
// undocumented
func period(d time.Duration) Option {
return func(cfg *config) {
cfg.period = d
}
}
func WithCPUProfileRate(hz int) Option {
return func(cfg *config) {
cfg.cpuProfileRate = hz
}
}
func WithProfileTypes(types ...ProfileType) Option {
return func(cfg *config) {
cfg.types = []ProfileType{} // reset
cfg.types = append(cfg.types, types...)
}
}
// Shortcut to set the "application_name" label
func WithAppName(appName string) Option {
return func(cfg *config) {
cfg.labels["application_name"] = appName
}
}
func WithLabels(labels map[string]string) Option {
return func(cfg *config) {
// Merge with pre-populated labels
for name, value := range labels {
cfg.labels[name] = value
}
}
}
func WithAgentSocket(agentSocket string) Option {
return func(cfg *config) {
cfg.agentSocket = agentSocket
}
}
func WithUploadTimeout(d time.Duration) Option {
return func(cfg *config) {
cfg.uploadTimeout = d
}
}
func WithCredentials(serverId string, serverToken string) Option {
return func(cfg *config) {
cfg.serverId = serverId
cfg.serverToken = serverToken
}
}
func withLogLevel(d int) Option {
return func(cfg *config) {
setGlobalLogger(log.Level(logLevel(d)))
}
}
// this is only used for testing internally to mock the internal HTTP client.
func withHTTPClient(h *http.Client) Option {
return func(cfg *config) {
cfg.httpClient = h
}
}
// this is only used for testing internally to record log output
func withLogRecorder() Option {
return func(cfg *config) {
var h = zerolog.HookFunc(func(e *zerolog.Event, level zerolog.Level, msg string) {
logRecorder.Add(msg)
})
setGlobalLogger(log.Hook(h))
}
}