Skip to content

Commit 95fc652

Browse files
committed
feat: run filters earlier to prevent unnecessary processing
1 parent a91653e commit 95fc652

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

example/example.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"log/slog"
99

10+
"log"
11+
1012
slogformatter "github.com/samber/slog-formatter"
1113
sloghttp "github.com/samber/slog-http"
1214
)
@@ -52,7 +54,10 @@ func main() {
5254
handler = sloghttp.New(logger.WithGroup("http"))(handler)
5355

5456
// Start server
55-
http.ListenAndServe(":4242", handler)
57+
err := http.ListenAndServe(":4242", handler)
58+
if err != nil {
59+
log.Fatal(err)
60+
}
5661

5762
// output:
5863
// time=2023-04-10T14:00:00Z level=INFO msg="Success" env=production http.status=200 http.method=GET http.path=/ http.ip=::1 http.latency=25.958µs http.user-agent=curl/7.77.0 http.time=2023-04-10T14:00:00Z http.request-id=229c7fc8-64f5-4467-bc4a-940700503b0d

example/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ require (
1111

1212
require (
1313
github.com/google/uuid v1.6.0 // indirect
14-
github.com/samber/lo v1.38.1 // indirect
14+
github.com/samber/lo v1.47.0 // indirect
1515
github.com/samber/slog-multi v1.0.0 // indirect
1616
go.opentelemetry.io/otel v1.19.0 // indirect
1717
go.opentelemetry.io/otel/trace v1.19.0 // indirect
18-
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
18+
golang.org/x/text v0.16.0 // indirect
1919
)

example/go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
66
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
77
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
88
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9-
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
10-
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
9+
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
10+
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
1111
github.com/samber/slog-formatter v1.0.0 h1:ULxHV+jNqi6aFP8xtzGHl2ejFRMl2+jI2UhCpgoXTDA=
1212
github.com/samber/slog-formatter v1.0.0/go.mod h1:c7pRfwhCfZQNzJz+XirmTveElxXln7M0Y8Pq781uxlo=
1313
github.com/samber/slog-multi v1.0.0 h1:snvP/P5GLQ8TQh5WSqdRaxDANW8AAA3egwEoytLsqvc=
@@ -18,7 +18,7 @@ go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
1818
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
1919
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
2020
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
21-
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
22-
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
21+
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
22+
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
2323
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2424
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

middleware.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ func NewWithConfig(logger *slog.Logger, config Config) func(http.Handler) http.H
131131
bw := newBodyWriter(w, ResponseBodyMaxSize, config.WithResponseBody)
132132

133133
defer func() {
134+
// Pass thru filters and skip early the code below, to prevent unnecessary processing.
135+
for _, filter := range config.Filters {
136+
if !filter(bw, r) {
137+
return
138+
}
139+
}
140+
134141
status := bw.Status()
135142
method := r.Method
136143
host := r.Host
@@ -231,12 +238,6 @@ func NewWithConfig(logger *slog.Logger, config Config) func(http.Handler) http.H
231238
}
232239
}
233240

234-
for _, filter := range config.Filters {
235-
if !filter(bw, r) {
236-
return
237-
}
238-
}
239-
240241
level := config.DefaultLevel
241242
if status >= http.StatusInternalServerError {
242243
level = config.ServerErrorLevel

0 commit comments

Comments
 (0)