-
Notifications
You must be signed in to change notification settings - Fork 57
HTTP-187 HTTP content tracing #189
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
Merged
+544
−22
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
115 changes: 115 additions & 0 deletions
115
src/main/java/com/getindata/connectors/http/internal/HttpLogger.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package com.getindata.connectors.http.internal; | ||
|
|
||
| import java.net.http.HttpHeaders; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
| import java.util.StringJoiner; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import com.getindata.connectors.http.internal.table.lookup.HttpLookupSourceRequestEntry; | ||
| import static com.getindata.connectors.http.internal.config.HttpConnectorConfigConstants.HTTP_LOGGING_LEVEL; | ||
|
|
||
| @Slf4j | ||
| public class HttpLogger { | ||
|
|
||
| private final HttpLoggingLevelType httpLoggingLevelType; | ||
davidradl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static HttpLogger getHttpLogger(Properties properties) { | ||
| return new HttpLogger(properties); | ||
| } | ||
|
|
||
| public void logRequest(HttpRequest httpRequest) { | ||
| log.debug(createStringForRequest(httpRequest)); | ||
| } | ||
|
|
||
| public void logResponse(HttpResponse<String> response) { | ||
| log.debug(createStringForResponse(response)); | ||
| } | ||
|
|
||
| public void logRequestBody(String body) { | ||
| log.debug(createStringForBody(body)); | ||
| } | ||
|
|
||
| public void logExceptionResponse(HttpLookupSourceRequestEntry request, Exception e) { | ||
| log.debug(createStringForExceptionResponse(request, e)); | ||
| } | ||
|
|
||
| private HttpLogger(Properties properties) { | ||
| String code = (String) properties.get(HTTP_LOGGING_LEVEL); | ||
| this.httpLoggingLevelType = HttpLoggingLevelType.valueOfStr(code); | ||
| } | ||
|
|
||
| String createStringForRequest(HttpRequest httpRequest) { | ||
| String headersForLog = getHeadersForLog(httpRequest.headers()); | ||
| return String.format("HTTP %s Request: URL: %s, Headers: %s", | ||
| httpRequest.method(), | ||
| httpRequest.uri().toString(), | ||
| headersForLog | ||
| ); | ||
| } | ||
|
|
||
| private String getHeadersForLog(HttpHeaders httpHeaders) { | ||
| if (httpHeaders == null) return "None"; | ||
| Map<String, List<String>> headersMap = httpHeaders.map(); | ||
| if (headersMap.isEmpty()) return "None"; | ||
| if (this.httpLoggingLevelType == HttpLoggingLevelType.MAX) { | ||
| StringJoiner headers = new StringJoiner(";"); | ||
| for (Map.Entry<String, List<String>> reqHeaders : headersMap.entrySet()) { | ||
| StringJoiner values = new StringJoiner(";"); | ||
| for (String value : reqHeaders.getValue()) { | ||
| values.add(value); | ||
| } | ||
| String header = reqHeaders.getKey() + ":[" + values + "]"; | ||
| headers.add(header); | ||
| } | ||
| return headers.toString(); | ||
| } | ||
| return "***"; | ||
| } | ||
|
|
||
| String createStringForResponse(HttpResponse<String> response) { | ||
| String headersForLog = getHeadersForLog(response.headers()); | ||
|
|
||
| String bodyForLog = "***"; | ||
| if (response.body() == null || response.body().isEmpty()) { | ||
| bodyForLog = "None"; | ||
| } else { | ||
| if (this.httpLoggingLevelType != HttpLoggingLevelType.MIN) { | ||
| bodyForLog = response.body().toString(); | ||
| } | ||
| } | ||
| return String.format("HTTP %s Response: URL: %s," | ||
| + " Response Headers: %s, status code: %s, Response Body: %s", | ||
| response.request().method(), | ||
| response.uri(), | ||
| headersForLog, | ||
| response.statusCode(), | ||
| bodyForLog | ||
| ); | ||
| } | ||
|
|
||
| private String createStringForExceptionResponse(HttpLookupSourceRequestEntry request, Exception e) { | ||
| HttpRequest httpRequest = request.getHttpRequest(); | ||
| return String.format("HTTP %s Exception Response: URL: %s Exception %s", | ||
| httpRequest.method(), | ||
| httpRequest.uri(), | ||
| e | ||
| ); | ||
| } | ||
|
|
||
| String createStringForBody(String body) { | ||
| String bodyForLog = "***"; | ||
| if (body == null || body.isEmpty()) { | ||
| bodyForLog = "None"; | ||
| } else { | ||
| if (this.httpLoggingLevelType != HttpLoggingLevelType.MIN) { | ||
| bodyForLog = body.toString(); | ||
| } | ||
| } | ||
| return bodyForLog; | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
src/main/java/com/getindata/connectors/http/internal/HttpLoggingLevelType.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.getindata.connectors.http.internal; | ||
|
|
||
| public enum HttpLoggingLevelType { | ||
| MIN, | ||
| REQRESPONSE, | ||
| MAX; | ||
|
|
||
| public static HttpLoggingLevelType valueOfStr(String code) { | ||
| if (code == null) { | ||
| return MIN; | ||
| } else { | ||
| return valueOf(code); | ||
| } | ||
| } | ||
| } |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.