Add HTTP client error test (#2932)

This commit is contained in:
Trask Stalnaker 2021-05-13 13:34:45 -07:00 committed by GitHub
parent a72a7838f0
commit 28d814c813
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 25 deletions

View File

@ -141,7 +141,14 @@ public class HttpUrlConnectionInstrumentationModule extends InstrumentationModul
scope.close();
if (throwable != null) {
tracer().endExceptionally(httpUrlState.context, throwable);
if (responseCode >= 400) {
// HttpURLConnection unnecessarily throws exception on error response.
// None of the other http clients do this, so not recording the exception on the span
// to be consistent with the telemetry for other http clients.
tracer().end(httpUrlState.context, new HttpUrlResponse(connection, responseCode));
} else {
tracer().endExceptionally(httpUrlState.context, throwable);
}
httpUrlState.finished = true;
} else if (methodName.equals("getInputStream") && responseCode > 0) {
// responseCode field is sometimes not populated.

View File

@ -5,7 +5,6 @@
import static io.opentelemetry.api.trace.SpanKind.CLIENT
import static io.opentelemetry.api.trace.SpanKind.SERVER
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP
@ -327,29 +326,6 @@ class HttpUrlConnectionTest extends HttpClientTest<HttpURLConnection> implements
}
}
def "error span"() {
def uri = server.address.resolve("/error")
when:
def url = uri.toURL()
runUnderTrace("parent") {
doRequest(method, uri)
}
then:
def expectedException = new IOException("Server returned HTTP response code: 500 for URL: $url")
thrown(IOException)
assertTraces(1) {
trace(0, 3 + extraClientSpans()) {
basicSpan(it, 0, "parent", null, expectedException)
clientSpan(it, 1, span(0), method, uri, 500, expectedException)
serverSpan(it, 2 + extraClientSpans(), span(1 + extraClientSpans()))
}
}
where:
method = "GET"
}
// This test makes no sense on IBM JVM because there is no HttpsURLConnectionImpl class there
@Requires({ !System.getProperty("java.vm.name").contains("IBM J9 VM") })
def "Make sure we can load HttpsURLConnectionImpl"() {

View File

@ -89,4 +89,9 @@ interface ResteasyProxyResource {
@Path("success")
Response put_success(@QueryParam("with") String param,
@HeaderParam("is-test-server") String isTestServer)
@GET
@Path("error")
Response get_error(@QueryParam("with") String param,
@HeaderParam("is-test-server") String isTestServer)
}

View File

@ -493,6 +493,29 @@ abstract class HttpClientTest<REQUEST> extends InstrumentationSpecification {
method = "GET"
}
def "error span"() {
def uri = server.address.resolve("/error")
when:
runUnderTrace("parent") {
try {
doRequest(method, uri)
} catch (Exception ignored) {
}
}
then:
assertTraces(1) {
trace(0, 3 + extraClientSpans()) {
basicSpan(it, 0, "parent", null)
clientSpan(it, 1, span(0), method, uri, 500)
serverSpan(it, 2 + extraClientSpans(), span(1 + extraClientSpans()))
}
}
where:
method = "GET"
}
def "reuse request"() {
given:
assumeTrue(testReusedRequest())
@ -795,6 +818,8 @@ abstract class HttpClientTest<REQUEST> extends InstrumentationSpecification {
if (exception) {
status ERROR
errorEvent(exception.class, exception.message)
} else if (responseCode >= 400) {
status ERROR
}
attributes {
"${SemanticAttributes.NET_TRANSPORT.key}" IP_TCP