-
Notifications
You must be signed in to change notification settings - Fork 17
Add HTTP trace logging to AsyncHttpClient #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| push: | ||
| branches: | ||
| - main | ||
|
|
||
| jobs: | ||
| build-and-test: | ||
| name: Build & Unit Tests (Java 21) | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout source | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Java 21 | ||
| uses: actions/setup-java@v4 | ||
| with: | ||
| distribution: temurin | ||
| java-version: '21' | ||
| cache: maven | ||
|
|
||
| - name: Build and run unit tests | ||
| run: mvn --batch-mode --no-transfer-progress verify | ||
|
|
||
| - name: Upload test reports | ||
| if: always() | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: surefire-reports | ||
| path: '**/target/surefire-reports/*.xml' | ||
| retention-days: 7 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,6 +174,9 @@ private void processRequest(Map<String, String> headers, EventEnvelope input, in | |
| validateUrl(request); | ||
| String uri = request.getFinalizedUrl(); | ||
| po.annotateTrace(DESTINATION, request.getTargetHost() + getRawUrl(uri)); | ||
| if (log.isDebugEnabled()) { | ||
| logHttpRequest(request, uri); | ||
| } | ||
| HttpClient client = HttpClient.create() | ||
| .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout) | ||
| .headers(h -> updateHttpHeaders(po, request, h)); | ||
|
Comment on lines
176
to
182
|
||
|
|
@@ -453,6 +456,35 @@ private void removeExpiredFiles() { | |
| } | ||
| } | ||
|
|
||
| private void logHttpRequest(AsyncHttpRequest request, String uri) { | ||
| var sb = new StringBuilder(); | ||
| sb.append("\n>>> ").append(request.getMethod()).append(' ') | ||
| .append(request.getTargetHost()).append(uri).append('\n'); | ||
| request.getHeaders().forEach((k, v) -> | ||
| sb.append(" ").append(k).append(": ").append(v).append('\n')); | ||
|
Comment on lines
+461
to
+464
|
||
| Object body = request.getBody(); | ||
| if (body instanceof byte[] b) { | ||
| sb.append(" body: [").append(b.length).append(" bytes]\n"); | ||
| } else if (body instanceof Map || body instanceof List) { | ||
| sb.append(" body: ") | ||
| .append(SimpleMapper.getInstance().getMapper().writeValueAsString(body)).append('\n'); | ||
| } else if (body instanceof String text && !text.isEmpty()) { | ||
| sb.append(" body: ").append(text).append('\n'); | ||
| } | ||
| log.debug(sb.toString()); | ||
| } | ||
|
|
||
| private void logHttpResponse(EventEnvelope response, byte[] b) { | ||
| var sb = new StringBuilder(); | ||
| sb.append("\n<<< ").append(response.getStatus()).append('\n'); | ||
| response.getHeaders().forEach((k, v) -> | ||
| sb.append(" ").append(k).append(": ").append(v).append('\n')); | ||
| if (b != null && b.length > 0) { | ||
| sb.append(" body: ").append(Utility.getInstance().getUTF(b)).append('\n'); | ||
| } | ||
|
Comment on lines
+479
to
+484
|
||
| log.debug(sb.toString()); | ||
| } | ||
|
|
||
| private class HttpResponseHandler { | ||
| private final Utility util = Utility.getInstance(); | ||
| private final CustomContentTypeResolver resolver = CustomContentTypeResolver.getInstance(); | ||
|
|
@@ -535,6 +567,9 @@ private void sendStreamResponse(EventEnvelope response, InputStream stream) { | |
| } | ||
|
|
||
| private void sendFixedLengthResponse(String resContentType, EventEnvelope response, byte[] b) { | ||
| if (log.isDebugEnabled()) { | ||
| logHttpResponse(response, b); | ||
| } | ||
| if (resContentType != null) { | ||
| if (resContentType.startsWith(APPLICATION_JSON)) { | ||
| sendJsonResponse(response, b); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation claims changing HTTP_TRACE_LEVEL requires no restart and that only the environment variable controls output. In most deployments (e.g., Docker/Kubernetes/systemd), environment variables are fixed at process start, so changing HTTP_TRACE_LEVEL typically requires restarting the application (or at least reloading the log4j2 configuration). Please reword to avoid implying runtime toggling via env var without restart; alternatively, describe using log4j2 configuration reload/monitorInterval if that’s the intended mechanism.