Skip to content

Commit 8583afd

Browse files
authored
chore: add context to logs to make the linter happy (#1533)
1 parent d10a243 commit 8583afd

File tree

8 files changed

+52
-35
lines changed

8 files changed

+52
-35
lines changed

frankenphp.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ package frankenphp
3030
import "C"
3131
import (
3232
"bytes"
33+
"context"
3334
"errors"
3435
"fmt"
3536
"io"
@@ -291,9 +292,10 @@ func Init(options ...Option) error {
291292

292293
initAutoScaling(mainThread)
293294

294-
logger.LogAttrs(nil, slog.LevelInfo, "FrankenPHP started 🐘", slog.String("php_version", Version().Version), slog.Int("num_threads", mainThread.numThreads), slog.Int("max_threads", mainThread.maxThreads))
295+
ctx := context.Background()
296+
logger.LogAttrs(ctx, slog.LevelInfo, "FrankenPHP started 🐘", slog.String("php_version", Version().Version), slog.Int("num_threads", mainThread.numThreads), slog.Int("max_threads", mainThread.maxThreads))
295297
if EmbeddedAppPath != "" {
296-
logger.LogAttrs(nil, slog.LevelInfo, "embedded PHP app 📦", slog.String("path", EmbeddedAppPath))
298+
logger.LogAttrs(ctx, slog.LevelInfo, "embedded PHP app 📦", slog.String("path", EmbeddedAppPath))
297299
}
298300

299301
return nil
@@ -428,7 +430,7 @@ func go_ub_write(threadIndex C.uintptr_t, cBuf *C.char, length C.int) (C.size_t,
428430

429431
i, e := writer.Write(unsafe.Slice((*byte)(unsafe.Pointer(cBuf)), length))
430432
if e != nil {
431-
fc.logger.LogAttrs(nil, slog.LevelError, "write error", slog.Any("error", e))
433+
fc.logger.LogAttrs(context.Background(), slog.LevelError, "write error", slog.Any("error", e))
432434
}
433435

434436
if fc.responseWriter == nil {
@@ -447,7 +449,7 @@ func go_apache_request_headers(threadIndex C.uintptr_t) (*C.go_string, C.size_t)
447449
if fc.responseWriter == nil {
448450
// worker mode, not handling a request
449451

450-
logger.LogAttrs(nil, slog.LevelDebug, "apache_request_headers() called in non-HTTP context", slog.String("worker", fc.scriptFilename))
452+
logger.LogAttrs(context.Background(), slog.LevelDebug, "apache_request_headers() called in non-HTTP context", slog.String("worker", fc.scriptFilename))
451453

452454
return nil, 0
453455
}
@@ -478,7 +480,7 @@ func go_apache_request_headers(threadIndex C.uintptr_t) (*C.go_string, C.size_t)
478480
func addHeader(fc *frankenPHPContext, cString *C.char, length C.int) {
479481
parts := strings.SplitN(C.GoStringN(cString, length), ": ", 2)
480482
if len(parts) != 2 {
481-
fc.logger.LogAttrs(nil, slog.LevelDebug, "invalid header", slog.String("header", parts[0]))
483+
fc.logger.LogAttrs(context.Background(), slog.LevelDebug, "invalid header", slog.String("header", parts[0]))
482484

483485
return
484486
}
@@ -532,7 +534,7 @@ func go_sapi_flush(threadIndex C.uintptr_t) bool {
532534
}
533535

534536
if err := http.NewResponseController(fc.responseWriter).Flush(); err != nil {
535-
logger.LogAttrs(nil, slog.LevelError, "the current responseWriter is not a flusher", slog.Any("error", err))
537+
logger.LogAttrs(context.Background(), slog.LevelError, "the current responseWriter is not a flusher", slog.Any("error", err))
536538
}
537539

538540
return false
@@ -585,15 +587,15 @@ func go_log(message *C.char, level C.int) {
585587

586588
switch le {
587589
case emerg, alert, crit, err:
588-
logger.LogAttrs(nil, slog.LevelError, m, slog.String("syslog_level", syslogLevel(level).String()))
590+
logger.LogAttrs(context.Background(), slog.LevelError, m, slog.String("syslog_level", syslogLevel(level).String()))
589591

590592
case warning:
591-
logger.LogAttrs(nil, slog.LevelWarn, m, slog.String("syslog_level", syslogLevel(level).String()))
593+
logger.LogAttrs(context.Background(), slog.LevelWarn, m, slog.String("syslog_level", syslogLevel(level).String()))
592594
case debug:
593-
logger.LogAttrs(nil, slog.LevelDebug, m, slog.String("syslog_level", syslogLevel(level).String()))
595+
logger.LogAttrs(context.Background(), slog.LevelDebug, m, slog.String("syslog_level", syslogLevel(level).String()))
594596

595597
default:
596-
logger.LogAttrs(nil, slog.LevelInfo, m, slog.String("syslog_level", syslogLevel(level).String()))
598+
logger.LogAttrs(context.Background(), slog.LevelInfo, m, slog.String("syslog_level", syslogLevel(level).String()))
597599
}
598600
}
599601

internal/testserver/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"log/slog"
56
"net/http"
67
"os"
@@ -31,6 +32,6 @@ func main() {
3132
port = "8080"
3233
}
3334

34-
logger.LogAttrs(nil, slog.LevelError, "server error", slog.Any("error", http.ListenAndServe(":"+port, nil)))
35+
logger.LogAttrs(context.Background(), slog.LevelError, "server error", slog.Any("error", http.ListenAndServe(":"+port, nil)))
3536
os.Exit(1)
3637
}

internal/watcher/watch_pattern.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package watcher
44

55
import (
6+
"context"
67
"log/slog"
78
"path/filepath"
89
"strings"
@@ -82,7 +83,7 @@ func isValidEventType(eventType int) bool {
8283
// 0:dir,1:file,2:hard_link,3:sym_link,4:watcher,5:other,
8384
func isValidPathType(pathType int, fileName string) bool {
8485
if pathType == 4 {
85-
logger.LogAttrs(nil, slog.LevelDebug, "special edant/watcher event", slog.String("fileName", fileName))
86+
logger.LogAttrs(context.Background(), slog.LevelDebug, "special edant/watcher event", slog.String("fileName", fileName))
8687
}
8788
return pathType <= 2
8889
}
@@ -165,7 +166,7 @@ func matchPattern(pattern string, fileName string) bool {
165166
}
166167
patternMatches, err := filepath.Match(pattern, fileName)
167168
if err != nil {
168-
logger.LogAttrs(nil, slog.LevelError, "failed to match filename", slog.String("file", fileName), slog.Any("error", err))
169+
logger.LogAttrs(context.Background(), slog.LevelError, "failed to match filename", slog.String("file", fileName), slog.Any("error", err))
169170
return false
170171
}
171172

internal/watcher/watcher.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package watcher
88
// #include "watcher.h"
99
import "C"
1010
import (
11+
"context"
1112
"errors"
1213
"log/slog"
1314
"runtime/cgo"
@@ -79,13 +80,15 @@ func DrainWatcher() {
7980

8081
// TODO: how to test this?
8182
func retryWatching(watchPattern *watchPattern) {
83+
ctx := context.Background()
84+
8285
failureMu.Lock()
8386
defer failureMu.Unlock()
8487
if watchPattern.failureCount >= maxFailureCount {
85-
logger.LogAttrs(nil, slog.LevelWarn, "giving up watching", slog.String("dir", watchPattern.dir))
88+
logger.LogAttrs(ctx, slog.LevelWarn, "giving up watching", slog.String("dir", watchPattern.dir))
8689
return
8790
}
88-
logger.LogAttrs(nil, slog.LevelInfo, "watcher was closed prematurely, retrying...", slog.String("dir", watchPattern.dir))
91+
logger.LogAttrs(ctx, slog.LevelInfo, "watcher was closed prematurely, retrying...", slog.String("dir", watchPattern.dir))
8992

9093
watchPattern.failureCount++
9194
session, err := startSession(watchPattern)
@@ -132,15 +135,18 @@ func (w *watcher) stopWatching() {
132135
}
133136

134137
func startSession(w *watchPattern) (C.uintptr_t, error) {
138+
ctx := context.Background()
139+
135140
handle := cgo.NewHandle(w)
136141
cDir := C.CString(w.dir)
137142
defer C.free(unsafe.Pointer(cDir))
138143
watchSession := C.start_new_watcher(cDir, C.uintptr_t(handle))
139144
if watchSession != 0 {
140-
logger.LogAttrs(nil, slog.LevelDebug, "watching", slog.String("dir", w.dir), slog.Any("patterns", w.patterns))
145+
logger.LogAttrs(ctx, slog.LevelDebug, "watching", slog.String("dir", w.dir), slog.Any("patterns", w.patterns))
146+
141147
return watchSession, nil
142148
}
143-
logger.LogAttrs(nil, slog.LevelError, "couldn't start watching", slog.String("dir", w.dir))
149+
logger.LogAttrs(ctx, slog.LevelError, "couldn't start watching", slog.String("dir", w.dir))
144150

145151
return watchSession, ErrUnableToStartWatching
146152
}
@@ -190,7 +196,7 @@ func listenForFileEvents(triggerWatcher chan string, stopWatcher chan struct{})
190196
timer.Reset(debounceDuration)
191197
case <-timer.C:
192198
timer.Stop()
193-
logger.LogAttrs(nil, slog.LevelInfo, "filesystem change detected", slog.String("file", lastChangedFile))
199+
logger.LogAttrs(context.Background(), slog.LevelInfo, "filesystem change detected", slog.String("file", lastChangedFile))
194200
scheduleReload()
195201
}
196202
}

phpmainthread.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package frankenphp
88
// #include "frankenphp.h"
99
import "C"
1010
import (
11+
"context"
1112
"log/slog"
1213
"strings"
1314
"sync"
@@ -170,7 +171,7 @@ func (mainThread *phpMainThread) setAutomaticMaxThreads() {
170171
maxAllowedThreads := totalSysMemory / uint64(perThreadMemoryLimit)
171172
mainThread.maxThreads = int(maxAllowedThreads)
172173

173-
logger.LogAttrs(nil, slog.LevelDebug, "Automatic thread limit", slog.Int("perThreadMemoryLimitMB", int(perThreadMemoryLimit/1024/1024)), slog.Int("maxThreads", mainThread.maxThreads))
174+
logger.LogAttrs(context.Background(), slog.LevelDebug, "Automatic thread limit", slog.Int("perThreadMemoryLimitMB", int(perThreadMemoryLimit/1024/1024)), slog.Int("maxThreads", mainThread.maxThreads))
174175
}
175176

176177
//export go_frankenphp_shutdown_main_thread

phpthread.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package frankenphp
44
// #include "frankenphp.h"
55
import "C"
66
import (
7+
"context"
78
"log/slog"
89
"runtime"
910
"sync"
@@ -55,7 +56,7 @@ func (thread *phpThread) boot() {
5556

5657
// start the actual posix thread - TODO: try this with go threads instead
5758
if !C.frankenphp_new_php_thread(C.uintptr_t(thread.threadIndex)) {
58-
logger.LogAttrs(nil, slog.LevelError, "unable to create thread", slog.Int("threadIndex", thread.threadIndex))
59+
logger.LogAttrs(context.Background(), slog.LevelError, "unable to create thread", slog.Int("threadIndex", thread.threadIndex))
5960
panic("unable to create thread")
6061
}
6162

scaling.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package frankenphp
44
//#include <sys/resource.h>
55
import "C"
66
import (
7+
"context"
78
"errors"
89
"log/slog"
910
"sync"
@@ -53,7 +54,7 @@ func initAutoScaling(mainThread *phpMainThread) {
5354

5455
func drainAutoScaling() {
5556
scalingMu.Lock()
56-
logger.LogAttrs(nil, slog.LevelDebug, "shutting down autoscaling", slog.Int("autoScaledThreads", len(autoScaledThreads)))
57+
logger.LogAttrs(context.Background(), slog.LevelDebug, "shutting down autoscaling", slog.Int("autoScaledThreads", len(autoScaledThreads)))
5758
scalingMu.Unlock()
5859
}
5960

@@ -93,7 +94,7 @@ func scaleWorkerThread(worker *worker) {
9394

9495
thread, err := addWorkerThread(worker)
9596
if err != nil {
96-
logger.LogAttrs(nil, slog.LevelWarn, "could not increase max_threads, consider raising this limit", slog.String("worker", worker.name), slog.Any("error", err))
97+
logger.LogAttrs(context.Background(), slog.LevelWarn, "could not increase max_threads, consider raising this limit", slog.String("worker", worker.name), slog.Any("error", err))
9798
return
9899
}
99100

@@ -116,7 +117,7 @@ func scaleRegularThread() {
116117

117118
thread, err := addRegularThread()
118119
if err != nil {
119-
logger.LogAttrs(nil, slog.LevelWarn, "could not increase max_threads, consider raising this limit", slog.Any("error", err))
120+
logger.LogAttrs(context.Background(), slog.LevelWarn, "could not increase max_threads, consider raising this limit", slog.Any("error", err))
120121
return
121122
}
122123

@@ -196,7 +197,7 @@ func deactivateThreads() {
196197

197198
// convert threads to inactive if they have been idle for too long
198199
if thread.state.is(stateReady) && waitTime > maxThreadIdleTime.Milliseconds() {
199-
logger.LogAttrs(nil, slog.LevelDebug, "auto-converting thread to inactive", slog.Int("threadIndex", thread.threadIndex))
200+
logger.LogAttrs(context.Background(), slog.LevelDebug, "auto-converting thread to inactive", slog.Int("threadIndex", thread.threadIndex))
200201
convertToInactiveThread(thread)
201202
stoppedThreadCount++
202203
autoScaledThreads = append(autoScaledThreads[:i], autoScaledThreads[i+1:]...)

threadworker.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package frankenphp
33
// #include "frankenphp.h"
44
import "C"
55
import (
6+
"context"
67
"log/slog"
78
"path/filepath"
89
"time"
@@ -93,13 +94,15 @@ func setupWorkerScript(handler *workerThread, worker *worker) {
9394
handler.dummyContext = fc
9495
handler.isBootingScript = true
9596
clearSandboxedEnv(handler.thread)
96-
logger.LogAttrs(nil, slog.LevelDebug, "starting", slog.String("worker", worker.name), slog.Int("thread", handler.thread.threadIndex))
97+
logger.LogAttrs(context.Background(), slog.LevelDebug, "starting", slog.String("worker", worker.name), slog.Int("thread", handler.thread.threadIndex))
9798
}
9899

99100
func tearDownWorkerScript(handler *workerThread, exitStatus int) {
100101
worker := handler.worker
101102
handler.dummyContext = nil
102103

104+
ctx := context.Background()
105+
103106
// if the worker request is not nil, the script might have crashed
104107
// make sure to close the worker request context
105108
if handler.workerContext != nil {
@@ -112,7 +115,7 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
112115
// TODO: make the max restart configurable
113116
metrics.StopWorker(worker.name, StopReasonRestart)
114117
handler.backoff.recordSuccess()
115-
logger.LogAttrs(nil, slog.LevelDebug, "restarting", slog.String("worker", worker.name))
118+
logger.LogAttrs(ctx, slog.LevelDebug, "restarting", slog.String("worker", worker.name))
116119
return
117120
}
118121

@@ -124,15 +127,15 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
124127
return
125128
}
126129

127-
logger.LogAttrs(nil, slog.LevelError, "worker script has not reached frankenphp_handle_request", slog.String("worker", worker.name))
130+
logger.LogAttrs(ctx, slog.LevelError, "worker script has not reached frankenphp_handle_request", slog.String("worker", worker.name))
128131

129132
// panic after exponential backoff if the worker has never reached frankenphp_handle_request
130133
if handler.backoff.recordFailure() {
131134
if !watcherIsEnabled && !handler.state.is(stateReady) {
132-
logger.LogAttrs(nil, slog.LevelError, "too many consecutive worker failures", slog.String("worker", worker.name), slog.Int("failures", handler.backoff.failureCount))
135+
logger.LogAttrs(ctx, slog.LevelError, "too many consecutive worker failures", slog.String("worker", worker.name), slog.Int("failures", handler.backoff.failureCount))
133136
panic("too many consecutive worker failures")
134137
}
135-
logger.LogAttrs(nil, slog.LevelWarn, "many consecutive worker failures", slog.String("worker", worker.name), slog.Int("failures", handler.backoff.failureCount))
138+
logger.LogAttrs(ctx, slog.LevelWarn, "many consecutive worker failures", slog.String("worker", worker.name), slog.Int("failures", handler.backoff.failureCount))
136139
}
137140
}
138141

@@ -141,7 +144,8 @@ func (handler *workerThread) waitForWorkerRequest() bool {
141144
// unpin any memory left over from previous requests
142145
handler.thread.Unpin()
143146

144-
logger.LogAttrs(nil, slog.LevelDebug, "waiting for request", slog.String("worker", handler.worker.name))
147+
ctx := context.Background()
148+
logger.LogAttrs(ctx, slog.LevelDebug, "waiting for request", slog.String("worker", handler.worker.name))
145149

146150
// Clear the first dummy request created to initialize the worker
147151
if handler.isBootingScript {
@@ -164,7 +168,7 @@ func (handler *workerThread) waitForWorkerRequest() bool {
164168
var fc *frankenPHPContext
165169
select {
166170
case <-handler.thread.drainChan:
167-
logger.LogAttrs(nil, slog.LevelDebug, "shutting down", slog.String("worker", handler.worker.name))
171+
logger.LogAttrs(ctx, slog.LevelDebug, "shutting down", slog.String("worker", handler.worker.name))
168172

169173
// flush the opcache when restarting due to watcher or admin api
170174
// note: this is done right before frankenphp_handle_request() returns 'false'
@@ -180,11 +184,11 @@ func (handler *workerThread) waitForWorkerRequest() bool {
180184
handler.workerContext = fc
181185
handler.state.markAsWaiting(false)
182186

183-
logger.LogAttrs(nil, slog.LevelDebug, "request handling started", slog.String("worker", handler.worker.name), slog.String("url", fc.request.RequestURI))
187+
logger.LogAttrs(ctx, slog.LevelDebug, "request handling started", slog.String("worker", handler.worker.name), slog.String("url", fc.request.RequestURI))
184188

185189
if err := updateServerContext(handler.thread, fc, true); err != nil {
186190
// Unexpected error or invalid request
187-
logger.LogAttrs(nil, slog.LevelDebug, "unexpected error", slog.String("worker", handler.worker.name), slog.String("url", fc.request.RequestURI), slog.Any("error", err))
191+
logger.LogAttrs(ctx, slog.LevelDebug, "unexpected error", slog.String("worker", handler.worker.name), slog.String("url", fc.request.RequestURI), slog.Any("error", err))
188192
fc.rejectBadRequest(err.Error())
189193
handler.workerContext = nil
190194

@@ -212,7 +216,7 @@ func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t) {
212216
fc.closeContext()
213217
thread.handler.(*workerThread).workerContext = nil
214218

215-
fc.logger.LogAttrs(nil, slog.LevelDebug, "request handling finished", slog.String("worker", fc.scriptFilename), slog.String("url", fc.request.RequestURI))
219+
fc.logger.LogAttrs(context.Background(), slog.LevelDebug, "request handling finished", slog.String("worker", fc.scriptFilename), slog.String("url", fc.request.RequestURI))
216220
}
217221

218222
// when frankenphp_finish_request() is directly called from PHP
@@ -222,5 +226,5 @@ func go_frankenphp_finish_php_request(threadIndex C.uintptr_t) {
222226
fc := phpThreads[threadIndex].getRequestContext()
223227
fc.closeContext()
224228

225-
fc.logger.LogAttrs(nil, slog.LevelDebug, "request handling finished", slog.String("url", fc.request.RequestURI))
229+
fc.logger.LogAttrs(context.Background(), slog.LevelDebug, "request handling finished", slog.String("url", fc.request.RequestURI))
226230
}

0 commit comments

Comments
 (0)