Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newrelic-3822 Bug fix for when Spring Webclient times out and emits a ReadTimeoutException, the transaction fails to link #1109

Merged
merged 6 commits into from
Dec 30, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* * Copyright 2022 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package io.netty.channel;

import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

@Weave(type = MatchType.Interface, originalName = "io.netty.channel.ChannelHandler")
public class ChannelHandler_Instrumentation {

/*
* This is to solve a bug where the transaction is lost when spring webclient times out and throws an error
* using the io.netty.handler.timeout.ReadTimeoutHandler class from netty.
*
* Any extra handlers used by netty will now link a transaction if available.
*
* */
@Trace(async = true, excludeFromTransactionTrace = true)
public void exceptionCaught(ChannelHandlerContext_Instrumentation ctx, Throwable cause) throws Exception {
if (ctx != null &&
ctx.pipeline().token != null && ctx.pipeline().token.isActive()) {
ctx.pipeline().token.link();
}

Weaver.callOriginal();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* * Copyright 2022 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package io.netty.channel;

import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

@Weave(type = MatchType.Interface, originalName = "io.netty.channel.ChannelHandler")
public class ChannelHandler_Instrumentation {

/*
* This is to solve a bug where the transaction is lost when spring webclient times out and throws an error
* using the io.netty.handler.timeout.ReadTimeoutHandler class from netty.
*
* Any extra handlers used by netty will now link a transaction if available.
*
* -----------------------------------
* WARNING
* -----------------------------------
*
* Netty has marked this method as deprecated since 4.1
*
* If instrumentation verification fails for because of this class,
* then in the new instrumentation module try instrumenting the class:
*
* io.netty.channel.AbstractChannelHandlerContext
*
* and its method:
*
* static void invokeExceptionCaught(final AbstractChannelHandlerContext next, final Throwable cause)
*
* */
@Trace(async = true, excludeFromTransactionTrace = true)
public void exceptionCaught(ChannelHandlerContext_Instrumentation ctx, Throwable cause) throws Exception {
if (ctx != null &&
ctx.pipeline().token != null && ctx.pipeline().token.isActive()) {
ctx.pipeline().token.link();
}

Weaver.callOriginal();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* * Copyright 2022 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package reactor.netty.http.client;

import com.newrelic.api.agent.Token;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import reactor.netty.Connection;
import reactor.util.context.Context;

@Weave(originalName = "reactor.netty.http.client.HttpClientConnect")
final class HttpClientConnect_Instrumentation {

@Weave(originalName = "reactor.netty.http.client.HttpClientConnect$HttpObserver")
static final class HttpObserver_Instrumentation {

public Context currentContext() {
return Weaver.callOriginal();
}

@Trace(async = true, excludeFromTransactionTrace = true)
public void onUncaughtException(Connection connection, Throwable error) {
Context ctx = currentContext();
Token token = ctx != null ? ctx.getOrDefault("newrelic-token", null) : null;
if (token != null && token.isActive()) {
token.link();
}
Weaver.callOriginal();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* * Copyright 2022 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package reactor.netty.http.client;

import com.newrelic.api.agent.Token;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import reactor.netty.Connection;
import reactor.util.context.Context;

@Weave(originalName = "reactor.netty.http.client.HttpClientConnect")
final class HttpClientConnect_Instrumentation {

@Weave(originalName = "reactor.netty.http.client.HttpClientConnect$HttpObserver")
static final class HttpObserver_Instrumentation {

public Context currentContext() {
return Weaver.callOriginal();
}

@Trace(async = true, excludeFromTransactionTrace = true)
public void onUncaughtException(Connection connection, Throwable error) {
Context ctx = currentContext();
Token token = ctx != null ? ctx.getOrDefault("newrelic-token", null) : null;
if (token != null && token.isActive()) {
token.link();
}
Weaver.callOriginal();
}
}
}