Skip to content

WebClientTracerSubscriber.terminateSpan() throws IllegalArgumentException for non-standard status code #1382

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

Closed
blake-bauman opened this issue Jun 25, 2019 · 12 comments
Labels
Milestone

Comments

@blake-bauman
Copy link
Contributor

When using Spring Cloud Sleuth with a WebClient call, and the downstream service responds with a non-standard HTTP code (say 499), then WebClientTracerSubscriber.terminateSpan() throws an IllegalArgumentException because the status code cannot be mapped to one of the HttpStatus enum values.

The offending code is here:

    void terminateSpan(@Nullable ClientResponse clientResponse,
                       @Nullable Throwable throwable) {
        if (clientResponse == null || clientResponse.statusCode() == null) {

From the Javadoc, ClientResponse.statusCode() will never return null, but will instead throw an IllegalArgumentException. As suggested by spring-projects/spring-framework#21289 clientResponse.getRawStatusCode() should be used if there's a possibility of a non-standard response code. Our app will call something like:

        webClient.get()
                 .uri(...)
                 .exchange()
                 .flatMap(response -> {
                     if (response.getRawStatusCode() == ...)
                         ...
                 })

However, this response handler never gets control because of the exception thrown when terminateSpan() calls clientResponse.statusCode(). When we remove spring-cloud-sleuth, it works fine, but since we would like the OpenTracing support, we still want to use sleuth.

@blake-bauman
Copy link
Contributor Author

We are using the Greenwich.SR1 release train.

@marcingrzejszczak
Copy link
Contributor

Hi @blake-bauman ! Are you interested in filing a PR with a failing test and the impl that fixes it?

@blake-bauman
Copy link
Contributor Author

I wish, but at the moment, the Legal department at my company hasn't approved me for public open source code contributions. I'm going through the vetting process, but that can take months. I'm working on a simple project that demonstrates this.

Browsing through the code, however, it does appear that the problem is localized to this null check, the error validation, and the reason phrase in the exception. walking through handleReceive(), that part appears to be ok.

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Jul 3, 2019 via email

@marcingrzejszczak
Copy link
Contributor

Maybe they should be a part of Brave's API? Like statusCodeOrZero()?

@codefromthecrypt
Copy link
Contributor

yeah this current code overrides statusCode, but better to override statusCodeAsInt instead. in any case catch the exception :P

@codefromthecrypt
Copy link
Contributor

actually it can also throw IOE heh https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/http/client/ClientHttpResponse.java#L46-L55

so something like

  @Override public int statusCodeAsInt(ClientHttpResponse response) {
    try {
      return response.getRawStatusCode();
    } catch (IOException | IllegalArgumentException dontCare) {
      return 0;
    }
  }

@blake-bauman
Copy link
Contributor Author

I don't think this is going to solve the reported problem. It may solve some problem, but not the reported one. Unless terminateSpan() stops calling ClientResponse.getStatusCode(), the original problem will persist.

Btw, getRawStatusCode() won't throw an exception. It's getStatusCode() that'll throw it.

@marcingrzejszczak
Copy link
Contributor

Now it should be better with 39d5e00

@blake-bauman
Copy link
Contributor Author

Which snapshot will this be? 2.2.0.BUILD-SNAPSHOT?

@blake-bauman
Copy link
Contributor Author

Using 2.2.0-BUILD-SNAPSHOT, issue seems to have moved to a different location. Should I open a new Issue to continue discussion? I have a sample project that demonstrates the issue.

@marcingrzejszczak
Copy link
Contributor

Please do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants