|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.jaxrsclient; |
| 7 | + |
| 8 | +import com.sun.jersey.api.client.Client; |
| 9 | +import com.sun.jersey.api.client.ClientHandlerException; |
| 10 | +import com.sun.jersey.api.client.ClientResponse; |
| 11 | +import com.sun.jersey.api.client.WebResource; |
| 12 | +import com.sun.jersey.api.client.filter.GZIPContentEncodingFilter; |
| 13 | +import com.sun.jersey.api.client.filter.LoggingFilter; |
| 14 | +import io.opentelemetry.api.common.AttributeKey; |
| 15 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 16 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest; |
| 17 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension; |
| 18 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; |
| 19 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; |
| 20 | +import io.opentelemetry.semconv.NetworkAttributes; |
| 21 | +import java.net.URI; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 27 | + |
| 28 | +class JaxRsClientV1Test extends AbstractHttpClientTest<WebResource.Builder> { |
| 29 | + @RegisterExtension |
| 30 | + static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent(); |
| 31 | + |
| 32 | + private final Client client = buildClient(false); |
| 33 | + private final Client clientWithReadTimeout = buildClient(true); |
| 34 | + |
| 35 | + private static Client buildClient(boolean readTimeout) { |
| 36 | + Client client = Client.create(); |
| 37 | + client.setConnectTimeout((int) CONNECTION_TIMEOUT.toMillis()); |
| 38 | + if (readTimeout) { |
| 39 | + client.setReadTimeout((int) READ_TIMEOUT.toMillis()); |
| 40 | + } |
| 41 | + // Add filters to ensure spans aren't duplicated. |
| 42 | + client.addFilter(new LoggingFilter()); |
| 43 | + client.addFilter(new GZIPContentEncodingFilter()); |
| 44 | + |
| 45 | + return client; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected void configure(HttpClientTestOptions.Builder options) { |
| 50 | + options.setTestCircularRedirects(false); |
| 51 | + options.setTestNonStandardHttpMethod(false); |
| 52 | + options.setTestCallback(false); |
| 53 | + options.setHttpAttributes( |
| 54 | + serverEndpoint -> { |
| 55 | + Set<AttributeKey<?>> attributes = |
| 56 | + new HashSet<>(HttpServerTestOptions.DEFAULT_HTTP_ATTRIBUTES); |
| 57 | + attributes.remove(NetworkAttributes.NETWORK_PROTOCOL_VERSION); |
| 58 | + return attributes; |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterAll |
| 63 | + void tearDown() { |
| 64 | + client.destroy(); |
| 65 | + clientWithReadTimeout.destroy(); |
| 66 | + } |
| 67 | + |
| 68 | + private Client getClient(URI uri) { |
| 69 | + if (uri.toString().contains("/read-timeout")) { |
| 70 | + return clientWithReadTimeout; |
| 71 | + } |
| 72 | + return client; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public WebResource.Builder buildRequest(String method, URI uri, Map<String, String> headers) { |
| 77 | + WebResource.Builder builder = getClient(uri).resource(uri).getRequestBuilder(); |
| 78 | + headers.forEach(builder::header); |
| 79 | + return builder; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int sendRequest( |
| 84 | + WebResource.Builder builder, String method, URI uri, Map<String, String> headers) |
| 85 | + throws Exception { |
| 86 | + String body = "POST".equals(method) || "PUT".equals(method) ? "" : null; |
| 87 | + try { |
| 88 | + return builder.method(method, ClientResponse.class, body).getStatus(); |
| 89 | + } catch (ClientHandlerException exception) { |
| 90 | + Throwable cause = exception.getCause(); |
| 91 | + if (cause instanceof Exception) { |
| 92 | + throw (Exception) cause; |
| 93 | + } |
| 94 | + throw new IllegalStateException(cause); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments