-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix RestTemplateInterceptor so that it calls endExceptionally() on ex…
…ception (#2516)
- Loading branch information
Mateusz Rzeszutek
authored
Mar 8, 2021
1 parent
fe4d95a
commit 3dff448
Showing
22 changed files
with
290 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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
48 changes: 48 additions & 0 deletions
48
...tion/spring/spring-web-3.1/library/src/test/groovy/RestTemplateInstrumentationTest.groovy
This file contains 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,48 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import io.opentelemetry.instrumentation.spring.httpclients.RestTemplateInterceptor | ||
import io.opentelemetry.instrumentation.test.LibraryTestTrait | ||
import io.opentelemetry.instrumentation.test.base.HttpClientTest | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.web.client.ResourceAccessException | ||
import org.springframework.web.client.RestTemplate | ||
import spock.lang.Shared | ||
|
||
class RestTemplateInstrumentationTest extends HttpClientTest implements LibraryTestTrait { | ||
@Shared | ||
RestTemplate restTemplate | ||
|
||
def setupSpec() { | ||
if (restTemplate == null) { | ||
restTemplate = new RestTemplate() | ||
restTemplate.getInterceptors().add(new RestTemplateInterceptor(getOpenTelemetry())) | ||
} | ||
} | ||
|
||
@Override | ||
int doRequest(String method, URI uri, Map<String, String> headers, Closure callback) { | ||
try { | ||
return restTemplate.execute(uri, HttpMethod.valueOf(method), { request -> | ||
headers.forEach(request.getHeaders().&add) | ||
}, { response -> | ||
callback?.call() | ||
response.statusCode.value() | ||
}) | ||
} catch (ResourceAccessException exception) { | ||
throw exception.getCause() | ||
} | ||
} | ||
|
||
@Override | ||
boolean testCircularRedirects() { | ||
false | ||
} | ||
|
||
@Override | ||
boolean testRemoteConnection() { | ||
false | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateInterceptorTest.java
This file contains 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,54 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.httpclients; | ||
|
||
import static io.opentelemetry.instrumentation.testing.util.TraceUtils.withClientSpan; | ||
import static io.opentelemetry.sdk.testing.assertj.TracesAssert.assertThat; | ||
import static org.mockito.BDDMockito.then; | ||
|
||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RestTemplateInterceptorTest { | ||
@RegisterExtension | ||
static final LibraryInstrumentationExtension instrumentation = | ||
LibraryInstrumentationExtension.create(); | ||
|
||
@Mock HttpRequest httpRequestMock; | ||
@Mock ClientHttpRequestExecution requestExecutionMock; | ||
byte[] requestBody = new byte[0]; | ||
|
||
@Test | ||
void shouldSkipWhenContextHasClientSpan() throws Exception { | ||
// given | ||
RestTemplateInterceptor interceptor = | ||
new RestTemplateInterceptor(instrumentation.getOpenTelemetry()); | ||
|
||
// when | ||
withClientSpan( | ||
"parent", | ||
() -> { | ||
interceptor.intercept(httpRequestMock, requestBody, requestExecutionMock); | ||
}); | ||
|
||
// then | ||
then(requestExecutionMock).should().execute(httpRequestMock, requestBody); | ||
|
||
assertThat(instrumentation.waitForTraces(1)) | ||
.hasTracesSatisfyingExactly( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> span.hasName("parent").hasKind(SpanKind.CLIENT))); | ||
} | ||
} |
This file contains 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 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 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 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.