wy471x opened a new pull request, #7527: URL: https://github.com/apache/shenyu/pull/7527
Fixes #6581 Make sure that: - [X] You have read the [contribution guidelines](https://shenyu.apache.org/community/contributor-guide). - [X] You submit test cases (unit or integration tests) that back your changes. - [X] Your local test passed `./mvnw clean install -Dmaven.javadoc.skip=true`. ## Summary Every access-log entry serialized its request header map to JSON on the request/response thread: `AbstractLoggingPlugin.doExecute` called `LogCollectUtils.getHeaders(request.getHeaders())` while the exchange was still on the Netty event loop, and `LoggingServerHttpResponse` did the same for the response headers when the response completed. That work (a stream + `Collectors.toMap` + Gson reflection over every header) blocks the event loop for every sampled request, while the actual log delivery is already asynchronous: the resulting `ShenyuRequestLog` is only handed to the buffered collector and consumed later on the collector's consumer thread. This PR keeps the exact same log payload but defers the serialization to whoever first reads the header value, so in the normal (non-desensitized) path the JSON is built on the log consumer thread instead of the request thread. Requests whose log entries are dropped because the buffer is full no longer pay for serialization at all. ### Changes: 1. `ShenyuRequestLog` (ShenyuRequestLog.java:41, 236, 259) — added two `transient` `Supplier<String>` fields plus `setRequestHeader(Supplier<String>)` / `setResponseHeader(Supplier<String>)` overloads. `getRequestHeader()` / `getResponseHeader()` now resolve the supplier on first read and cache the result; the eager `String` setters clear the deferred source so an explicit value always wins. The fields are `transient` so log clients (Gson/Jackson) never see them. 2. `AbstractLoggingPlugin.doExecute()` (AbstractLoggingPlugin.java:109) — sets the request header with a supplier instead of an eagerly built JSON string. 3. `LoggingServerHttpResponse.appendResponse()` / `logError()` (LoggingServerHttpResponse.java:150, 277) — same for the response headers on both the normal and the error completion path. When desensitization is enabled the value is still materialized on the response thread, because `AbstractLogCollector.desensitize` reads the header through the same getter; that path is unchanged. ### Test Cases: `ShenyuRequestLogTest` (existing suite, +4 tests): - `testRequestHeaderIsSerializedLazily` — the supplier is not invoked when it is set, is invoked exactly once on the first `getRequestHeader()` and is cached afterwards. - `testResponseHeaderIsSerializedLazily` — same contract for the response header. - `testResolvedHeaderIsNotSerializedTwice` — an explicit `String` set afterwards wins and the stale supplier is never invoked. - `testDeferredHeaderSourceIsNotSerialized` — serializing an unresolved log with Gson exposes neither the supplier field nor the header json, so the on-wire log shape is unchanged. ## Verification - `mvn -pl shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common -am clean install -Dmaven.javadoc.skip=true` — passed (12 reactor modules), checkstyle (validate), apache-rat and the module suites included. - Test results: logging-common 60, logging-desensitize-api 13 — 0 failures, 0 errors. close #6581 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
