Skip to content

Commit 755db86

Browse files
metrics: only report workers ready when actually ready (#2210)
In #2205 it appears that workers could be reported in metrics as "ready" before they are actually ready. This changes the reporting so that workers are only reported ready once they have completed booting. Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent 2bdf858 commit 755db86

3 files changed

Lines changed: 15 additions & 13 deletions

File tree

metrics.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
const (
1212
StopReasonCrash = iota
1313
StopReasonRestart
14-
//StopReasonShutdown
14+
StopReasonBootFailure // worker crashed before reaching frankenphp_handle_request
1515
)
1616

1717
type StopReason int
@@ -125,10 +125,14 @@ func (m *PrometheusMetrics) StopWorker(name string, reason StopReason) {
125125
}
126126

127127
m.totalWorkers.WithLabelValues(name).Dec()
128-
m.readyWorkers.WithLabelValues(name).Dec()
128+
129+
// only decrement readyWorkers if the worker actually reached frankenphp_handle_request
130+
if reason != StopReasonBootFailure {
131+
m.readyWorkers.WithLabelValues(name).Dec()
132+
}
129133

130134
switch reason {
131-
case StopReasonCrash:
135+
case StopReasonCrash, StopReasonBootFailure:
132136
m.workerCrashes.WithLabelValues(name).Inc()
133137
case StopReasonRestart:
134138
m.workerRestarts.WithLabelValues(name).Inc()

threadworker.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ func (handler *workerThread) name() string {
101101
func setupWorkerScript(handler *workerThread, worker *worker) {
102102
metrics.StartWorker(worker.name)
103103

104-
if handler.state.Is(state.Ready) {
105-
metrics.ReadyWorker(handler.worker.name)
106-
}
107-
108104
// Create a dummy request to set up the worker
109105
fc, err := newDummyContext(
110106
filepath.Base(worker.fileName),
@@ -152,7 +148,11 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
152148
}
153149

154150
// worker has thrown a fatal error or has not reached frankenphp_handle_request
155-
metrics.StopWorker(worker.name, StopReasonCrash)
151+
if handler.isBootingScript {
152+
metrics.StopWorker(worker.name, StopReasonBootFailure)
153+
} else {
154+
metrics.StopWorker(worker.name, StopReasonCrash)
155+
}
156156

157157
if !handler.isBootingScript {
158158
// fatal error (could be due to exit(1), timeouts, etc.)
@@ -207,13 +207,12 @@ func (handler *workerThread) waitForWorkerRequest() (bool, any) {
207207
if !C.frankenphp_shutdown_dummy_request() {
208208
panic("Not in CGI context")
209209
}
210+
211+
// worker is truly ready only after reaching frankenphp_handle_request()
212+
metrics.ReadyWorker(handler.worker.name)
210213
}
211214

212-
// worker threads are 'ready' after they first reach frankenphp_handle_request()
213-
// 'state.TransitionComplete' is only true on the first boot of the worker script,
214-
// while 'isBootingScript' is true on every boot of the worker script
215215
if handler.state.Is(state.TransitionComplete) {
216-
metrics.ReadyWorker(handler.worker.name)
217216
handler.state.Set(state.Ready)
218217
}
219218

worker.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func initWorkers(opt []workerOpt) error {
9898
return nil
9999
}
100100

101-
102101
func newWorker(o workerOpt) (*worker, error) {
103102
// Order is important!
104103
// This order ensures that FrankenPHP started from inside a symlinked directory will properly resolve any paths.

0 commit comments

Comments
 (0)