FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stackrox/pkg/continuousprofiling/profiler.go at master · stackrox/stackrox · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
stackrox
/
stackrox
Public
Notifications
You must be signed in to change notification settings
Fork
191
Star
1.3k
Code
Issues
54
Pull requests
604
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
stackrox
/
pkg
/
continuousprofiling
/
profiler.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
190 lines (165 loc) · 5.07 KB
Breadcrumbs
stackrox
/
pkg
/
continuousprofiling
/
profiler.go
Copy path
File metadata and controls
190 lines (165 loc) · 5.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
continuousprofiling
import
(
"net/url"
"runtime"
"slices"
"strings"
"github.com/grafana/pyroscope-go"
"github.com/pkg/errors"
"github.com/stackrox/rox/pkg/env"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/pkg/urlfmt"
)
const
(
mutexProfileFraction
=
5
blockProfileRate
=
5
)
// StartClientWrapper wraps the Start function to enable mocking in test
//
//go:generate mockgen-wrapper
type
StartClientWrapper
interface
{
Start
(pyroscope.
Config
) (
*
pyroscope.
Profiler
,
error
)
}
type
startClient
struct
{
}
// Start wrapper for pyroscope.Start
func
(
c
*
startClient
)
Start
(
cfg
pyroscope.
Config
) (
*
pyroscope.
Profiler
,
error
) {
return
pyroscope
.
Start
(
cfg
)
}
var
(
ErrApplicationName
=
errors
.
New
(
"the ApplicationName must be defined"
)
ErrServerAddress
=
errors
.
New
(
"the ServerAddress must be defined"
)
ErrAtLeastOneProfileIsNeeded
=
errors
.
New
(
"at least one profile is needed"
)
log
=
logging
.
LoggerForModule
()
DefaultProfiles
=
[]pyroscope.
ProfileType
{
pyroscope
.
ProfileCPU
,
pyroscope
.
ProfileAllocObjects
,
pyroscope
.
ProfileAllocSpace
,
pyroscope
.
ProfileInuseObjects
,
pyroscope
.
ProfileInuseSpace
,
pyroscope
.
ProfileGoroutines
,
pyroscope
.
ProfileMutexCount
,
pyroscope
.
ProfileMutexDuration
,
pyroscope
.
ProfileBlockCount
,
pyroscope
.
ProfileBlockDuration
,
}
startClientFuncWrapper
StartClientWrapper
=
&
startClient
{}
)
// DefaultConfig creates a new configuration with default properties.
func
DefaultConfig
()
*
pyroscope.
Config
{
labels
,
err
:=
parseLabels
(
env
.
ContinuousProfilingLabels
.
Setting
())
if
err
!=
nil
{
log
.
Errorf
(
"Unable to parse Labels in %q: %v"
,
env
.
ContinuousProfilingLabels
.
EnvVar
(),
err
)
}
return
&
pyroscope.
Config
{
ApplicationName
:
env
.
ContinuousProfilingAppName
.
Setting
(),
ServerAddress
:
env
.
ContinuousProfilingServerAddress
.
Setting
(),
BasicAuthUser
:
env
.
ContinuousProfilingBasicAuthUser
.
Setting
(),
BasicAuthPassword
:
env
.
ContinuousProfilingBasicAuthPassword
.
Setting
(),
ProfileTypes
:
DefaultProfiles
,
Logger
:
nil
,
Tags
:
labels
,
}
}
type
OptionFunc
func
(
*
pyroscope.
Config
)
// WithDefaultAppName sets the ApplicationName
// Default: AppName
func
WithDefaultAppName
(
appName
string
)
OptionFunc
{
return
func
(
cfg
*
pyroscope.
Config
) {
// Never override with the default AppName
if
cfg
.
ApplicationName
==
""
{
cfg
.
ApplicationName
=
appName
}
}
}
// WithProfiles sets the ProfilerTypes
// Default: ProfileCPU, ProfileAllocObjects, ProfileAllocSpace, ProfileInuseObjects, ProfileInuseSpace
func
WithProfiles
(
profiles
...
pyroscope.
ProfileType
)
OptionFunc
{
return
func
(
cfg
*
pyroscope.
Config
) {
cfg
.
ProfileTypes
=
profiles
}
}
// WithLogging enables logging
// Default: nil
func
WithLogging
()
OptionFunc
{
return
func
(
cfg
*
pyroscope.
Config
) {
cfg
.
Logger
=
log
}
}
func
validateServerAddress
(
address
string
) (
string
,
error
) {
// We default to https unless http is specified
sanitizedAddress
:=
urlfmt
.
FormatURL
(
address
,
urlfmt
.
HTTPS
,
urlfmt
.
NoTrailingSlash
)
if
_
,
err
:=
url
.
Parse
(
sanitizedAddress
);
err
!=
nil
{
return
""
,
errox
.
InvalidArgs
.
Newf
(
"unable to parse server address %q"
,
address
).
CausedBy
(
err
)
}
return
sanitizedAddress
,
nil
}
func
parseLabels
(
labels
string
) (
map
[
string
]
string
,
error
) {
parsedLabels
:=
make
(
map
[
string
]
string
)
entries
:=
strings
.
SplitSeq
(
labels
,
","
)
for
entry
:=
range
entries
{
entry
=
strings
.
TrimSpace
(
entry
)
if
entry
==
""
{
continue
}
parts
:=
strings
.
SplitN
(
entry
,
"="
,
2
)
if
len
(
parts
)
!=
2
{
return
nil
,
errors
.
Errorf
(
"invalid label format: %q (expected key=value)"
,
entry
)
}
key
:=
strings
.
TrimSpace
(
parts
[
0
])
value
:=
strings
.
TrimSpace
(
parts
[
1
])
if
key
==
""
{
return
nil
,
errors
.
Errorf
(
"empty label key in %q"
,
entry
)
}
if
value
==
""
{
return
nil
,
errors
.
Errorf
(
"empty label value in %q"
,
entry
)
}
parsedLabels
[
key
]
=
value
}
return
parsedLabels
,
nil
}
func
validateConfig
(
cfg
*
pyroscope.
Config
)
error
{
if
cfg
.
ApplicationName
==
""
{
return
ErrApplicationName
}
if
cfg
.
ServerAddress
==
""
{
return
ErrServerAddress
}
var
err
error
cfg
.
ServerAddress
,
err
=
validateServerAddress
(
cfg
.
ServerAddress
)
if
err
!=
nil
{
return
err
}
if
len
(
cfg
.
ProfileTypes
)
==
0
{
return
ErrAtLeastOneProfileIsNeeded
}
return
nil
}
// SetupClient setups the pyroscope client to start the continuous profiling.
func
SetupClient
(
cfg
*
pyroscope.
Config
,
opts
...
OptionFunc
)
error
{
if
!
env
.
ContinuousProfiling
.
BooleanSetting
() {
return
nil
}
for
_
,
o
:=
range
opts
{
o
(
cfg
)
}
if
err
:=
validateConfig
(
cfg
);
err
!=
nil
{
return
err
}
if
profileTypeEnabled
(
pyroscope
.
ProfileMutexCount
,
cfg
.
ProfileTypes
...
) {
runtime
.
SetMutexProfileFraction
(
mutexProfileFraction
)
}
if
profileTypeEnabled
(
pyroscope
.
ProfileBlockCount
,
cfg
.
ProfileTypes
...
) {
runtime
.
SetBlockProfileRate
(
blockProfileRate
)
}
_
,
err
:=
startClientFuncWrapper
.
Start
(
*
cfg
)
if
err
!=
nil
{
return
err
}
log
.
Info
(
"Continuous Profiling enabled"
)
return
nil
}
func
profileTypeEnabled
(
profile
pyroscope.
ProfileType
,
profiles
...
pyroscope.
ProfileType
)
bool
{
return
slices
.
Contains
(
profiles
,
profile
)
}
Back
|
FazBrowse Home
|
New Git URL