Skip to content
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

HTTP protocol version on the logging #1543

Merged
merged 17 commits into from Nov 30, 2021
Merged

Conversation

vitalijr2
Copy link
Collaborator

@vitalijr2 vitalijr2 commented Nov 27, 2021

Fixes #1533

Changes:

  • logRequest does not write protocol version as we do not know what a client will use
  • logAndRebufferResponse tries to write a protocol version if a response has it or writes HTTP/unknown otherwise

A Feign client can add a protocol version when it creates a response.

Next Feign clients have hardcoded value of HTTP/1.1 or try to read it from HTTP client:

  1. Default (hardcoded value)
  2. Apache HTTP Client (hardcoded value)
  3. HC5
  4. Google HTTP Client (hardcoded value)
  5. Java11
  6. OkHttpClient

Copy link
Member

@velo velo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. Main concern would be to revert all changes to expected log messages and make sure we would match what was there before.

I expecte with a default HTTP/1.1 on the Response, all should work as before, reducing backward compatibility problems.

Thanks for this work, looks really nice

core/src/main/java/feign/Client.java Outdated Show resolved Hide resolved
@@ -29,6 +29,7 @@
private final Map<String, Collection<String>> headers;
private final Body body;
private final Request request;
private final String protocolVersion;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if want to make this an enum or we wan't to leave it open so people can have FTPES or any other protocols they want to

Copy link
Collaborator Author

@vitalijr2 vitalijr2 Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we can use enum: there is limited list of protocol versions - HTTP/1.0, HTTP/1.1, HTTP/2.0 (HTTP/2). In the future we can add new values if they are needed.

I have added Request.ProtocolVersion enum and Util.enumForName method that helps to find enum value by any of strings: HTTP_1_0, HTTP/1.0, HTTP_2, HTTP/2.0 etc.

core/src/test/java/feign/LoggerTest.java Outdated Show resolved Hide resolved
core/src/test/java/feign/LoggerTest.java Outdated Show resolved Hide resolved
mock/src/main/java/feign/mock/MockClient.java Outdated Show resolved Hide resolved
@@ -124,6 +128,9 @@ protected Response toFeignResponse(Request request, HttpResponse<byte[]> httpRes
final OptionalLong length = httpResponse.headers().firstValueAsLong("Content-Length");

return Response.builder()
.protocolVersion(httpResponse.version().name()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to fragile.

BTW, can you add a comment showing that is the expect value for version()

Also, I don't think it would be too back to have protocol = HTTP_1_1.

If you reallt think this name transformation is important, can we change this to a switch and hardcoded responses, just to make it easier to read.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.
I have replaced the string regexp-building by looking for proper enum value by its name() and toString() values. This solution covers all available in java11 values: HTTP_1_1 and HTTP_2.

@@ -92,6 +96,9 @@ static Request toOkHttpRequest(feign.Request input) {
private static feign.Response toFeignResponse(Response response, feign.Request request)
throws IOException {
return feign.Response.builder()
.protocolVersion(response.protocol().name()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as java11

Copy link
Collaborator Author

@vitalijr2 vitalijr2 Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I write above I have replaced it by looking for enum value. It covers some of available in OkHttp values: HTTP_1_0, HTTP_1_1 and HTTP_2. But there are other values: SPDY_3 (deprecated), H2_PRIOR_KNOWLEDGE, QUIC - they are replaced by null. Then Logger writes UNKNOWN as protocol version, see https://github.com/radio-rogal/feign/blob/366d2d218cb3e7f15504a9f5aa52e95eb70f91c9/core/src/main/java/feign/Logger.java#L108

slf4j/src/test/java/feign/slf4j/Slf4jLoggerTest.java Outdated Show resolved Hide resolved
…ack compatibility, replace string protocol version with enum, replace fragile conversion of alien enums by string case-insensitive comparision
@vitalijr2
Copy link
Collaborator Author

vitalijr2 commented Nov 29, 2021

There is another idea. After changes Request has constant-like field protocolVersion with value HTTP_1_1.

What about to add before logging of a request in SynchronousMethodHandler.executeAndDecode another call client.prepareRequest: the client can upgrade request's version if its wants.

Copy link
Member

@velo velo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked, will leave to simmer for a few days, in case @kdavisk6 has anything to say about it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to merge Will be merged if no other member ask for changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feign logs HTTP/1.1 when OkHttpClient is using HTTP/2
2 participants