Add HTTP client error test
MODIFY
instrumentation/http-url-connection/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpurlconnection/HttpUrlConnectionInstrumentationModule.java
instrumentation/http-url-connection/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpurlconnection/HttpUrlConnectionInstrumentationModule.java
@@ -141,7 +141,14 @@ public class HttpUrlConnectionInstrumentationModule extends InstrumentationModul
scope.close();
if (throwable != null) {
+ 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.

MODIFY
instrumentation/http-url-connection/javaagent/src/test/groovy/HttpUrlConnectionTest.groovy
instrumentation/http-url-connection/javaagent/src/test/groovy/HttpUrlConnectionTest.groovy
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"() {

MODIFY
instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-common/javaagent/src/test/groovy/ResteasyProxyClientTest.groovy
instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-common/javaagent/src/test/groovy/ResteasyProxyClientTest.groovy
@@ -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)
}
\ No newline at end of file

MODIFY
testing-common/src/main/groovy/io/opentelemetry/instrumentation/test/base/HttpClientTest.groovy
testing-common/src/main/groovy/io/opentelemetry/instrumentation/test/base/HttpClientTest.groovy
@@ -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

Add HTTP client error test (#2932)