Skip to content

Commit b945742

Browse files
authored
Change pprof labels to be prometheus compatible (#32865)
Enables scrapping pprof endpoint for continuous profiling Closes: #32854
1 parent 195fccd commit b945742

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

modules/graceful/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (g *Manager) doShutdown() {
136136
}
137137
g.lock.Lock()
138138
g.shutdownCtxCancel()
139-
atShutdownCtx := pprof.WithLabels(g.hammerCtx, pprof.Labels("graceful-lifecycle", "post-shutdown"))
139+
atShutdownCtx := pprof.WithLabels(g.hammerCtx, pprof.Labels(LifecyclePProfLabel, "post-shutdown"))
140140
pprof.SetGoroutineLabels(atShutdownCtx)
141141
for _, fn := range g.toRunAtShutdown {
142142
go fn()
@@ -167,7 +167,7 @@ func (g *Manager) doHammerTime(d time.Duration) {
167167
default:
168168
log.Warn("Setting Hammer condition")
169169
g.hammerCtxCancel()
170-
atHammerCtx := pprof.WithLabels(g.terminateCtx, pprof.Labels("graceful-lifecycle", "post-hammer"))
170+
atHammerCtx := pprof.WithLabels(g.terminateCtx, pprof.Labels(LifecyclePProfLabel, "post-hammer"))
171171
pprof.SetGoroutineLabels(atHammerCtx)
172172
}
173173
g.lock.Unlock()
@@ -183,7 +183,7 @@ func (g *Manager) doTerminate() {
183183
default:
184184
log.Warn("Terminating")
185185
g.terminateCtxCancel()
186-
atTerminateCtx := pprof.WithLabels(g.managerCtx, pprof.Labels("graceful-lifecycle", "post-terminate"))
186+
atTerminateCtx := pprof.WithLabels(g.managerCtx, pprof.Labels(LifecyclePProfLabel, "post-terminate"))
187187
pprof.SetGoroutineLabels(atTerminateCtx)
188188

189189
for _, fn := range g.toRunAtTerminate {

modules/graceful/manager_common.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ const (
2222
watchdogMsg systemdNotifyMsg = "WATCHDOG=1"
2323
)
2424

25+
// LifecyclePProfLabel is a label marking manager lifecycle phase
26+
// Making it compliant with prometheus key regex https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
27+
// would enable someone interested to be able to to continuously gather profiles into pyroscope.
28+
// Other labels for pprof (in "modules/process" package) should also follow this rule.
29+
const LifecyclePProfLabel = "graceful_lifecycle"
30+
2531
func statusMsg(msg string) systemdNotifyMsg {
2632
return systemdNotifyMsg("STATUS=" + msg)
2733
}
@@ -65,10 +71,10 @@ func (g *Manager) prepare(ctx context.Context) {
6571
g.hammerCtx, g.hammerCtxCancel = context.WithCancel(ctx)
6672
g.managerCtx, g.managerCtxCancel = context.WithCancel(ctx)
6773

68-
g.terminateCtx = pprof.WithLabels(g.terminateCtx, pprof.Labels("graceful-lifecycle", "with-terminate"))
69-
g.shutdownCtx = pprof.WithLabels(g.shutdownCtx, pprof.Labels("graceful-lifecycle", "with-shutdown"))
70-
g.hammerCtx = pprof.WithLabels(g.hammerCtx, pprof.Labels("graceful-lifecycle", "with-hammer"))
71-
g.managerCtx = pprof.WithLabels(g.managerCtx, pprof.Labels("graceful-lifecycle", "with-manager"))
74+
g.terminateCtx = pprof.WithLabels(g.terminateCtx, pprof.Labels(LifecyclePProfLabel, "with-terminate"))
75+
g.shutdownCtx = pprof.WithLabels(g.shutdownCtx, pprof.Labels(LifecyclePProfLabel, "with-shutdown"))
76+
g.hammerCtx = pprof.WithLabels(g.hammerCtx, pprof.Labels(LifecyclePProfLabel, "with-hammer"))
77+
g.managerCtx = pprof.WithLabels(g.managerCtx, pprof.Labels(LifecyclePProfLabel, "with-manager"))
7278

7379
if !g.setStateTransition(stateInit, stateRunning) {
7480
panic("invalid graceful manager state: transition from init to running failed")

modules/process/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *Context) Value(key any) any {
3232
}
3333

3434
// ProcessContextKey is the key under which process contexts are stored
35-
var ProcessContextKey any = "process-context"
35+
var ProcessContextKey any = "process_context"
3636

3737
// GetContext will return a process context if one exists
3838
func GetContext(ctx context.Context) *Context {

modules/process/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var (
2626
)
2727

2828
// DescriptionPProfLabel is a label set on goroutines that have a process attached
29-
const DescriptionPProfLabel = "process-description"
29+
const DescriptionPProfLabel = "process_description"
3030

3131
// PIDPProfLabel is a label set on goroutines that have a process attached
3232
const PIDPProfLabel = "pid"
@@ -35,7 +35,7 @@ const PIDPProfLabel = "pid"
3535
const PPIDPProfLabel = "ppid"
3636

3737
// ProcessTypePProfLabel is a label set on goroutines that have a process attached
38-
const ProcessTypePProfLabel = "process-type"
38+
const ProcessTypePProfLabel = "process_type"
3939

4040
// IDType is a pid type
4141
type IDType string

0 commit comments

Comments
 (0)