Skip to content

Commit ce5b7be

Browse files
committed
better
1 parent e7edf40 commit ce5b7be

File tree

5 files changed

+18
-46
lines changed

5 files changed

+18
-46
lines changed

instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/AbstractChannelHandlerContextInstrumentation.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,6 @@ public static class InvokeExceptionCaughtAdvice {
4949
public static void onEnter(
5050
@Advice.This ChannelHandlerContext ctx, @Advice.Argument(0) Throwable throwable) {
5151

52-
// On Windows, Netty wraps connection exceptions in AbstractChannel.AnnotatedConnectException
53-
// Unwrap to get the actual exception type for proper error reporting
54-
Throwable error = throwable;
55-
if (error != null
56-
&& error.getClass().getName().contains("AnnotatedConnectException")
57-
&& error.getCause() != null) {
58-
error = error.getCause();
59-
}
60-
6152
// we can't rely on exception handling in HttpClientTracingHandler because it can't catch
6253
// exceptions from handlers that run after it, for example ratpack has ReadTimeoutHandler
6354
// (trigger ReadTimeoutException) after HttpClientCodec (or handler is inserted after it)
@@ -67,12 +58,12 @@ public static void onEnter(
6758
ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT).remove();
6859
contextAttr.remove();
6960
HttpRequestAndChannel request = ctx.channel().attr(HTTP_CLIENT_REQUEST).getAndRemove();
70-
instrumenter().end(clientContext, request, null, error);
61+
instrumenter().end(clientContext, request, null, throwable);
7162
return;
7263
}
7364
ServerContext serverContext = ServerContexts.peekFirst(ctx.channel());
7465
if (serverContext != null) {
75-
NettyErrorHolder.set(serverContext.context(), error);
66+
NettyErrorHolder.set(serverContext.context(), throwable);
7667
}
7768
}
7869
}

instrumentation/netty/netty-common-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4/common/NettyScope.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ public static void end(
4646
nettyScope.scope.close();
4747

4848
if (throwable != null) {
49-
// On Windows, Netty wraps connection exceptions in AbstractChannel.AnnotatedConnectException
50-
// Unwrap to get the actual exception type for proper error reporting
51-
if (throwable.getClass().getName().contains("AnnotatedConnectException")
52-
&& throwable.getCause() != null) {
53-
throwable = throwable.getCause();
54-
}
5549
instrumenter.end(nettyScope.context, nettyScope.request, null, throwable);
5650
} else {
5751
channelPromise.addListener(

instrumentation/netty/netty-common-4.0/library/src/main/java/io/opentelemetry/instrumentation/netty/common/v4_0/internal/client/ConnectionCompleteListener.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ public void operationComplete(Future<Void> future) {
3434
if (future instanceof ChannelFuture) {
3535
channel = ((ChannelFuture) future).channel();
3636
}
37-
Throwable error = future.cause();
38-
// On Windows, Netty wraps connection exceptions in AbstractChannel.AnnotatedConnectException
39-
// Unwrap to get the actual exception type for proper error reporting
40-
if (error != null
41-
&& error.getClass().getName().contains("AnnotatedConnectException")
42-
&& error.getCause() != null) {
43-
error = error.getCause();
44-
}
45-
instrumenter.end(context, request, channel, error);
37+
instrumenter.end(context, request, channel, future.cause());
4638
}
4739
}

instrumentation/ratpack/ratpack-1.4/testing/src/main/java/io/opentelemetry/instrumentation/ratpack/client/AbstractRatpackHttpClientTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
1010
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
1111

12+
import io.netty.handler.codec.PrematureChannelClosureException;
1213
import io.netty.handler.timeout.ReadTimeoutException;
1314
import io.opentelemetry.api.common.AttributeKey;
1415
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
1516
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
1617
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
1718
import java.net.URI;
19+
import java.nio.channels.ClosedChannelException;
1820
import java.time.Duration;
1921
import java.util.Collections;
2022
import java.util.HashSet;
23+
import java.util.Locale;
2124
import java.util.Map;
2225
import java.util.Set;
2326
import org.junit.jupiter.api.AfterAll;
@@ -167,19 +170,14 @@ protected boolean useNettyClientAttributes() {
167170
}
168171

169172
private static Throwable nettyClientSpanErrorMapper(URI uri, Throwable exception) {
170-
// Unwrap AnnotatedConnectException (Windows behavior) so test assertions match what instrumentation records
171-
Throwable unwrappedException = exception;
172-
if (exception != null
173-
&& exception.getClass().getName().contains("AnnotatedConnectException")
174-
&& exception.getCause() != null) {
175-
unwrappedException = exception.getCause();
176-
}
177-
178173
// For read timeout, map to ReadTimeoutException
179174
if (uri.getPath().equals("/read-timeout")) {
180175
return ReadTimeoutException.INSTANCE;
181176
}
182-
return unwrappedException;
177+
if (isNonRoutableAddress(uri) && exception instanceof ClosedChannelException) {
178+
return new PrematureChannelClosureException();
179+
}
180+
return exception;
183181
}
184182

185183
private static String nettyExpectedClientSpanNameMapper(URI uri, String method) {
@@ -190,12 +188,16 @@ private static String nettyExpectedClientSpanNameMapper(URI uri, String method)
190188
// On Windows, non-routable addresses don't fail at CONNECT level.
191189
// The connection proceeds far enough to start HTTP processing before
192190
// the channel closes, resulting in an HTTP span instead of CONNECT.
193-
if (System.getProperty("os.name").toLowerCase().contains("win")) {
191+
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) {
194192
return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method);
195193
}
196194
return "CONNECT";
197195
default:
198196
return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method);
199197
}
200198
}
199+
200+
private static boolean isNonRoutableAddress(URI uri) {
201+
return "192.0.2.1".equals(uri.getHost());
202+
}
201203
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.net.URI;
1919
import java.time.Duration;
2020
import java.util.HashSet;
21+
import java.util.Locale;
2122
import java.util.Map;
2223
import java.util.Set;
2324
import org.junit.jupiter.api.AfterAll;
@@ -152,19 +153,11 @@ protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
152153
}
153154

154155
private static Throwable nettyClientSpanErrorMapper(URI uri, Throwable exception) {
155-
// Unwrap AnnotatedConnectException (Windows behavior) so test assertions match what instrumentation records
156-
Throwable unwrappedException = exception;
157-
if (exception != null
158-
&& exception.getClass().getName().contains("AnnotatedConnectException")
159-
&& exception.getCause() != null) {
160-
unwrappedException = exception.getCause();
161-
}
162-
163156
// For read timeout, map to HttpClientReadTimeoutException
164157
if (uri.getPath().equals("/read-timeout")) {
165158
return HttpClientReadTimeoutException.INSTANCE;
166159
}
167-
return unwrappedException;
160+
return exception;
168161
}
169162

170163
private static String nettyExpectedClientSpanNameMapper(URI uri, String method) {
@@ -175,7 +168,7 @@ private static String nettyExpectedClientSpanNameMapper(URI uri, String method)
175168
// On Windows, non-routable addresses don't fail at CONNECT level.
176169
// The connection proceeds far enough to start HTTP processing before
177170
// the channel closes, resulting in an HTTP span instead of CONNECT.
178-
if (System.getProperty("os.name").toLowerCase().contains("win")) {
171+
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) {
179172
return HttpClientTestOptions.DEFAULT_EXPECTED_CLIENT_SPAN_NAME_MAPPER.apply(uri, method);
180173
}
181174
return "CONNECT";

0 commit comments

Comments
 (0)