Repository navigation
perf: cut request ingress and library logging overhead - #140
Merged
Merged
Conversation
- Check library log levels against the source's base core before deriving the correlated logger, and cache the "SRouter" child per logger version. Disabled records such as the per-request Debug "Authentication successful" no longer encode correlation fields. - Install runtime identities, the log source, and client info with a single http.Request copy instead of two. - Skip net.SplitHostPort for inputs it always rejects (portless IPv4/IPv6, bracketed IPv6 without a port), avoiding its error allocation, and slice bracketed IPv6 hosts instead of concatenating. - Leave cloned flags nil when the source has none. BenchmarkProductionAuthRoute: 1145ns -> 661ns, 2766B -> 824B, 18 -> 10 allocs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #140 +/- ##
=======================================
Coverage 99.92% 99.92%
=======================================
Files 26 26
Lines 2604 2629 +25
=======================================
+ Hits 2602 2627 +25
Misses 2 2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four localized hot-path optimizations. No public API changes.
internal/requestlog,pkg/scontext/logger.go, newinternal/loghookrequestlog.Checkrejected disabled levels only afterGetLoggerderived the correlated logger (JSON core clone + field encoding). It now checks the source's base core first and caches theNamed("SRouter").WithOptions(AddCallerSkip(1))child against the logger version. The per-request Debug "Authentication successful" record no longer derives anything when Debug is off.pkg/router/router.gowithRuntimeIdentitiesandwithRequestLoggingeach calledreq.WithContext; they now thread one context and copy the request once.pkg/scontext/client_ip.gonet.SplitHostPortfor inputs it always rejects (bare IPv4/IPv6, bracketed IPv6 without port), avoiding its error allocation, and slices[host]instead of concatenating. All five common shapes are now 0 allocs.pkg/scontext/context.gocloneSRouterContextno longer allocates an empty flags map;WithFlagallocates lazily as for new contexts.Why
internal/loghookrequestlogimportsscontext, so it can't reach the unexported cache. Exporting library-only helpers would widen the scontext API, soscontextsets a hook variable duringinitinstead.Semantics
Withdoes not enable levels its base core disables. This holds for Zap's cores and wrappers (sampler,IncreaseLevel, tee), and is documented indocs/logging.md. The derived core's ownCheckstill runs, so narrowing cores behave as before.cleanClientIPis fuzz-tested against the original implementation (FuzzCleanClientIPMatchesReference, 8.3M execs locally, no mismatches).limitersis async.Map, so a struct key would be boxed per lookup and likely not beat the string concat.Benchmark
New
BenchmarkProductionAuthRoute: build/config IDs, X-Real-IP client IP, trace IDs, auth-required route, Info-level JSON logger.Test plan
go test -race ./...go vet ./...,golangci-lint run(0 issues),go build ./examples/...,go test ./internal/doccheck-race, wheresync.Pooldrops items), library logger follows correlation writes andClearRequestLogger,cleanClientIPmakes 0 allocs, nil/empty flags stay nil on clone and still accept later writes🤖 Generated with Claude Code