Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.apm.agent.resttemplate;

import org.springframework.http.client.ClientHttpResponse;

public class ClientHttpResponseAdapter {

private static final int UNKNOWN_STATUS = -1;

public static int getStatusCode(ClientHttpResponse response) {
int status = internalGetStatusCode(response);
if (status == UNKNOWN_STATUS) {
status = legacyGetStatusCode(response);
}
return status;
}

private static int internalGetStatusCode(ClientHttpResponse response) {
try {
return response.getStatusCode().value();
} catch (Exception e) {
// using broad exception to handle when method is missing for pre 6.x versions
return UNKNOWN_STATUS;
}
}

private static int legacyGetStatusCode(ClientHttpResponse response) {
// getRawStatusCode has been introduced in 3.1.1
// but deprecated in 6.x, will be removed in 7.x
try {
return response.getRawStatusCode();
} catch (Exception e) {
// using broad exception to handle when method is missing in pre-3.1.1 and post 7.x
return UNKNOWN_STATUS;
}
}

private ClientHttpResponseAdapter() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public static void afterExecute(@Advice.Return @Nullable ClientHttpResponse clie
Span<?> span = (Span<?>) spanObj;
try {
if (clientHttpResponse != null) {
// getRawStatusCode has been introduced in 3.1.1
span.getContext().getHttp().withStatusCode(clientHttpResponse.getRawStatusCode());
span.getContext().getHttp().withStatusCode(ClientHttpResponseAdapter.getStatusCode(clientHttpResponse));
}
if (t != null) {
span.withOutcome(Outcome.FAILURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
.and(returns(
hasSuperType(named("org.springframework.http.client.ClientHttpResponse"))
// getRawStatusCode added in 3.1.1 thus we rely on that to filter unsupported versions
.and(declaresMethod(named("getRawStatusCode")))
// will be removed in 7.x
.and(declaresMethod(named("getRawStatusCode"))
// getStatusCode added in 6.x and replaces getRawStatusCode in 7.x
.or(declaresMethod(named("getStatusCode"))))
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void dispatch404_usePathAsName() {

private Predicate<Throwable> expectClientError(int expectedStatus) {
return error -> (error instanceof WebClientResponseException)
&& ((WebClientResponseException) error).getRawStatusCode() == expectedStatus;
&& ((WebClientResponseException) error).getStatusCode().value() == expectedStatus;
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ private static Predicate<ServerSentEvent<String>> checkSSE(final int index) {

private Predicate<Throwable> expectClientError(int expectedStatus) {
return error -> (error instanceof WebClientResponseException)
&& ((WebClientResponseException) error).getRawStatusCode() == expectedStatus;
&& ((WebClientResponseException) error).getStatusCode().value() == expectedStatus;
}
}
Loading