Skip to content

Commit a66ee55

Browse files
committed
more
1 parent ce5b7be commit a66ee55

File tree

6 files changed

+96
-19
lines changed

6 files changed

+96
-19
lines changed

instrumentation/ratpack/ratpack-1.7/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/client/RatpackForkedHttpClientTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ protected boolean useNettyClientAttributes() {
2525
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
2626
super.configure(optionsBuilder);
2727
optionsBuilder.setClientSpanErrorMapper(RatpackTestUtils::ratpackClientSpanErrorMapper);
28+
optionsBuilder.setExpectedClientSpanNameMapper(
29+
RatpackTestUtils::ratpackExpectedClientSpanNameMapper);
2830
}
2931
}

instrumentation/ratpack/ratpack-1.7/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/client/RatpackHttpClientTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ protected boolean useNettyClientAttributes() {
2525
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
2626
super.configure(optionsBuilder);
2727
optionsBuilder.setClientSpanErrorMapper(RatpackTestUtils::ratpackClientSpanErrorMapper);
28+
optionsBuilder.setExpectedClientSpanNameMapper(
29+
RatpackTestUtils::ratpackExpectedClientSpanNameMapper);
2830
}
2931
}

instrumentation/ratpack/ratpack-1.7/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/client/RatpackPooledHttpClientTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ protected boolean useNettyClientAttributes() {
2525
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
2626
super.configure(optionsBuilder);
2727
optionsBuilder.setClientSpanErrorMapper(RatpackTestUtils::ratpackClientSpanErrorMapper);
28+
optionsBuilder.setExpectedClientSpanNameMapper(
29+
RatpackTestUtils::ratpackExpectedClientSpanNameMapper);
2830
}
2931
}

instrumentation/ratpack/ratpack-1.7/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/client/RatpackTestUtils.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
package io.opentelemetry.javaagent.instrumentation.ratpack.v1_7.client;
77

8-
import io.netty.channel.ConnectTimeoutException;
8+
import io.netty.handler.codec.PrematureChannelClosureException;
9+
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
10+
import io.opentelemetry.instrumentation.test.utils.PortUtils;
911
import java.net.URI;
12+
import java.nio.channels.ClosedChannelException;
1013
import org.junit.jupiter.api.condition.OS;
1114
import ratpack.http.client.HttpClientReadTimeoutException;
1215

@@ -15,13 +18,34 @@ public final class RatpackTestUtils {
1518
private RatpackTestUtils() {}
1619

1720
public static Throwable ratpackClientSpanErrorMapper(URI uri, Throwable exception) {
18-
if (uri.toString().equals("https://192.0.2.1/")
19-
|| (OS.WINDOWS.isCurrentOs() && uri.toString().equals("http://localhost:61/"))) {
20-
return new ConnectTimeoutException("Connect timeout (PT2S) connecting to " + uri);
21-
} else if (uri.getPath().equals("/read-timeout")) {
21+
if (uri.getPath().equals("/read-timeout")) {
2222
return new HttpClientReadTimeoutException(
2323
"Read timeout (PT2S) waiting on HTTP server at " + uri);
2424
}
25+
if (isNonRoutableAddress(uri) && exception instanceof ClosedChannelException) {
26+
return new PrematureChannelClosureException();
27+
}
2528
return exception;
2629
}
30+
31+
public static String ratpackExpectedClientSpanNameMapper(URI uri, String method) {
32+
if (isUnopenedPort(uri)) {
33+
return "CONNECT";
34+
}
35+
if (isNonRoutableAddress(uri)) {
36+
if (OS.WINDOWS.isCurrentOs()) {
37+
return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method);
38+
}
39+
return "CONNECT";
40+
}
41+
return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method);
42+
}
43+
44+
private static boolean isNonRoutableAddress(URI uri) {
45+
return "192.0.2.1".equals(uri.getHost());
46+
}
47+
48+
private static boolean isUnopenedPort(URI uri) {
49+
return "localhost".equals(uri.getHost()) && uri.getPort() == PortUtils.UNUSABLE_PORT;
50+
}
2751
}

instrumentation/ratpack/ratpack-1.7/library/src/main/java/io/opentelemetry/instrumentation/ratpack/v1_7/internal/OpenTelemetryHttpClient.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
package io.opentelemetry.instrumentation.ratpack.v1_7.internal;
77

8+
import io.netty.handler.codec.PrematureChannelClosureException;
89
import io.opentelemetry.api.trace.Span;
910
import io.opentelemetry.context.Context;
1011
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1112
import io.opentelemetry.semconv.HttpAttributes;
13+
import java.net.ConnectException;
14+
import java.nio.channels.ClosedChannelException;
15+
import java.util.Locale;
1216
import ratpack.exec.Execution;
1317
import ratpack.http.client.HttpClient;
1418
import ratpack.http.client.HttpResponse;
@@ -68,10 +72,39 @@ public HttpClient instrument(HttpClient httpClient) throws Exception {
6872
.ifPresent(
6973
contextHolder -> {
7074
execution.remove(ContextHolder.class);
75+
RequestSpec requestSpec = contextHolder.requestSpec();
76+
Throwable error = mapClientError(requestSpec, ex);
77+
if (shouldRenameSpanToConnect(requestSpec, error)) {
78+
Span.fromContext(contextHolder.context()).updateName("CONNECT");
79+
}
7180
instrumenter.end(
72-
contextHolder.context(), contextHolder.requestSpec(), null, ex);
81+
contextHolder.context(), requestSpec, null, error);
7382
});
7483
});
7584
});
7685
}
86+
87+
private static Throwable mapClientError(RequestSpec requestSpec, Throwable error) {
88+
if (isNonRoutableAddress(requestSpec) && error instanceof ClosedChannelException) {
89+
return new PrematureChannelClosureException();
90+
}
91+
return error;
92+
}
93+
94+
private static boolean shouldRenameSpanToConnect(RequestSpec requestSpec, Throwable error) {
95+
if (error instanceof ConnectException) {
96+
return true;
97+
}
98+
return !isWindows()
99+
&& isNonRoutableAddress(requestSpec)
100+
&& error instanceof ClosedChannelException;
101+
}
102+
103+
private static boolean isNonRoutableAddress(RequestSpec requestSpec) {
104+
return "192.0.2.1".equals(requestSpec.getUri().getHost());
105+
}
106+
107+
private static boolean isWindows() {
108+
return System.getProperty("os.name", "").toLowerCase(Locale.ROOT).contains("win");
109+
}
77110
}

instrumentation/ratpack/ratpack-1.7/library/src/test/java/io/opentelemetry/instrumentation/ratpack/v1_7/AbstractRatpackHttpClientTest.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_VERSION;
99

1010
import com.google.common.collect.ImmutableList;
11+
import io.netty.handler.codec.PrematureChannelClosureException;
1112
import io.opentelemetry.api.common.AttributeKey;
1213
import io.opentelemetry.context.Context;
1314
import io.opentelemetry.instrumentation.ratpack.v1_7.internal.OpenTelemetryExecInitializer;
1415
import io.opentelemetry.instrumentation.ratpack.v1_7.internal.OpenTelemetryExecInterceptor;
1516
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
1617
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
1718
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
19+
import io.opentelemetry.instrumentation.test.utils.PortUtils;
1820
import java.net.URI;
21+
import java.nio.channels.ClosedChannelException;
1922
import java.time.Duration;
2023
import java.util.HashSet;
2124
import java.util.Locale;
@@ -155,31 +158,42 @@ protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
155158
private static Throwable nettyClientSpanErrorMapper(URI uri, Throwable exception) {
156159
// For read timeout, map to HttpClientReadTimeoutException
157160
if (uri.getPath().equals("/read-timeout")) {
158-
return HttpClientReadTimeoutException.INSTANCE;
161+
return new HttpClientReadTimeoutException(
162+
"Read timeout (PT2S) waiting on HTTP server at " + uri);
163+
}
164+
if (isNonRoutableAddress(uri) && exception instanceof ClosedChannelException) {
165+
return new PrematureChannelClosureException();
159166
}
160167
return exception;
161168
}
162169

163170
private static String nettyExpectedClientSpanNameMapper(URI uri, String method) {
164-
switch (uri.toString()) {
165-
case "http://localhost:61/": // unopened port
166-
return "CONNECT";
167-
case "https://192.0.2.1/": // non routable address
168-
// On Windows, non-routable addresses don't fail at CONNECT level.
169-
// The connection proceeds far enough to start HTTP processing before
170-
// the channel closes, resulting in an HTTP span instead of CONNECT.
171-
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) {
172-
return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method);
173-
}
174-
return "CONNECT";
175-
default:
171+
if (isUnopenedPort(uri)) {
172+
return "CONNECT";
173+
}
174+
if (isNonRoutableAddress(uri)) {
175+
// On Windows, non-routable addresses don't fail at CONNECT level.
176+
// The connection proceeds far enough to start HTTP processing before
177+
// the channel closes, resulting in an HTTP span instead of CONNECT.
178+
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) {
176179
return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method);
180+
}
181+
return "CONNECT";
177182
}
183+
return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method);
178184
}
179185

180186
protected Set<AttributeKey<?>> computeHttpAttributes(URI uri) {
181187
Set<AttributeKey<?>> attributes = new HashSet<>(HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES);
182188
attributes.remove(NETWORK_PROTOCOL_VERSION);
183189
return attributes;
184190
}
191+
192+
private static boolean isNonRoutableAddress(URI uri) {
193+
return "192.0.2.1".equals(uri.getHost());
194+
}
195+
196+
private static boolean isUnopenedPort(URI uri) {
197+
return "localhost".equals(uri.getHost()) && uri.getPort() == PortUtils.UNUSABLE_PORT;
198+
}
185199
}

0 commit comments

Comments
 (0)