Skip to content

Commit 33bf365

Browse files
Fixed some locations to not touch exceptions when safe_exception is configured (#3543)
--------- Co-authored-by: Jonas Kunz <[email protected]>
1 parent e37654f commit 33bf365

File tree

19 files changed

+74
-29
lines changed

19 files changed

+74
-29
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:
3939
[float]
4040
===== Bug fixes
4141
* Added missing support for TracerBuilder in OpenTelemetry bridge - {pull}3535[#3535]
42+
* Fixed some locations to not touch exceptions when `safe_exception` is configured - {pull}3543[#3543]
4243
4344
[float]
4445
===== Potentially breaking changes

apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,11 @@ public boolean isRedactExceptions() {
11751175
return (safeExceptions.get() & 1) != 0;
11761176
}
11771177

1178+
@Override
1179+
public boolean isAvoidTouchingExceptions() {
1180+
return isRedactExceptions() || !captureExceptionDetails();
1181+
}
1182+
11781183
@Override
11791184
public boolean isUseServletAttributesForExceptionPropagation() {
11801185
return (safeExceptions.get() & 2) == 0;

apm-agent-core/src/test/java/co/elastic/apm/agent/bci/InstrumentationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public static String onMethodEnter() {
603603

604604
@Advice.AssignReturned.ToReturned
605605
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
606-
public static String onMethodExit(@Advice.Thrown Throwable throwable) {
606+
public static String onMethodExit() {
607607
throw new RuntimeException("This exception should be suppressed");
608608
}
609609
}

apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient3-plugin/src/main/java/co/elastic/apm/agent/httpclient/v3/HttpClient3Instrumentation.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import co.elastic.apm.agent.tracer.Outcome;
2929
import co.elastic.apm.agent.tracer.Span;
3030
import co.elastic.apm.agent.tracer.Tracer;
31+
import co.elastic.apm.agent.tracer.configuration.CoreConfiguration;
3132
import net.bytebuddy.asm.Advice;
3233
import net.bytebuddy.description.method.MethodDescription;
3334
import net.bytebuddy.description.type.TypeDescription;
@@ -146,8 +147,10 @@ public static void onExit(@Advice.Thrown @Nullable Throwable thrown,
146147
span.getContext().getHttp().withStatusCode(statusLine.getStatusCode());
147148
}
148149

149-
if (thrown instanceof CircularRedirectException) {
150-
span.withOutcome(Outcome.FAILURE);
150+
if(thrown != null && !tracer.getConfig(CoreConfiguration.class).isAvoidTouchingExceptions()) {
151+
if (thrown instanceof CircularRedirectException) {
152+
span.withOutcome(Outcome.FAILURE);
153+
}
151154
}
152155

153156
span.captureException(thrown)

apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/LegacyApacheHttpClientInstrumentation.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import co.elastic.apm.agent.tracer.ElasticContext;
2424
import co.elastic.apm.agent.tracer.Outcome;
2525
import co.elastic.apm.agent.tracer.Span;
26+
import co.elastic.apm.agent.tracer.configuration.CoreConfiguration;
2627
import net.bytebuddy.asm.Advice;
2728
import net.bytebuddy.description.NamedElement;
2829
import net.bytebuddy.description.method.MethodDescription;
@@ -123,10 +124,13 @@ public static void onAfterExecute(@Advice.Return @Nullable HttpResponse response
123124
}
124125
span.captureException(t);
125126
} finally {
126-
// in case of circular redirect, we get an exception but status code won't be available without response
127-
// thus we have to deal with span outcome directly
128-
if (t instanceof CircularRedirectException) {
129-
span.withOutcome(Outcome.FAILURE);
127+
128+
if(t != null && !tracer.getConfig(CoreConfiguration.class).isAvoidTouchingExceptions()) {
129+
// in case of circular redirect, we get an exception but status code won't be available without response
130+
// thus we have to deal with span outcome directly
131+
if (t instanceof CircularRedirectException) {
132+
span.withOutcome(Outcome.FAILURE);
133+
}
130134
}
131135

132136
span.deactivate().end();

apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/ApacheHttpClient4ApiAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121

2222
import co.elastic.apm.agent.httpclient.common.ApacheHttpClientApiAdapter;
23+
import co.elastic.apm.agent.tracer.GlobalTracer;
24+
import co.elastic.apm.agent.tracer.Tracer;
25+
import co.elastic.apm.agent.tracer.configuration.CoreConfiguration;
2326
import org.apache.http.HttpHost;
2427
import org.apache.http.HttpRequest;
2528
import org.apache.http.StatusLine;
@@ -32,6 +35,8 @@
3235
public class ApacheHttpClient4ApiAdapter implements ApacheHttpClientApiAdapter<HttpRequest, HttpRequestWrapper, HttpHost, CloseableHttpResponse> {
3336
private static final ApacheHttpClient4ApiAdapter INSTANCE = new ApacheHttpClient4ApiAdapter();
3437

38+
private final Tracer tracer = GlobalTracer.get();
39+
3540
private ApacheHttpClient4ApiAdapter() {
3641
}
3742

@@ -65,6 +70,9 @@ public int getResponseCode(CloseableHttpResponse closeableHttpResponse) {
6570

6671
@Override
6772
public boolean isCircularRedirectException(Throwable t) {
73+
if (t == null || tracer.getConfig(CoreConfiguration.class).isAvoidTouchingExceptions()) {
74+
return false;
75+
}
6876
return t instanceof CircularRedirectException;
6977
}
7078

apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient5-plugin/src/main/java/co/elastic/apm/agent/httpclient/v5/helper/ApacheHttpClient5ApiAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import co.elastic.apm.agent.httpclient.common.ApacheHttpClientApiAdapter;
2222
import co.elastic.apm.agent.sdk.logging.Logger;
2323
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
24+
import co.elastic.apm.agent.tracer.GlobalTracer;
25+
import co.elastic.apm.agent.tracer.Tracer;
26+
import co.elastic.apm.agent.tracer.configuration.CoreConfiguration;
2427
import org.apache.hc.client5.http.CircularRedirectException;
2528
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
2629
import org.apache.hc.client5.http.routing.RoutingSupport;
@@ -38,6 +41,8 @@ public class ApacheHttpClient5ApiAdapter implements ApacheHttpClientApiAdapter<H
3841

3942
private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClient5ApiAdapter.class);
4043

44+
private final Tracer tracer = GlobalTracer.get();
45+
4146
private ApacheHttpClient5ApiAdapter() {
4247
}
4348

@@ -78,6 +83,9 @@ public int getResponseCode(CloseableHttpResponse closeableHttpResponse) {
7883

7984
@Override
8085
public boolean isCircularRedirectException(Throwable t) {
86+
if (t == null || tracer.getConfig(CoreConfiguration.class).isAvoidTouchingExceptions()) {
87+
return false;
88+
}
8189
return t instanceof CircularRedirectException;
8290
}
8391

apm-agent-plugins/apm-cassandra/apm-cassandra3-plugin/src/main/java/co/elastic/apm/agent/cassandra3/Cassandra3Instrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static void onEnter() {
106106
}
107107

108108
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
109-
public static void onExit(@Advice.Thrown @Nullable Throwable thrown, @Advice.Return @Nullable Object returnValue) {
109+
public static void onExit() {
110110
CassandraHelper.inSyncExecute(false);
111111
}
112112
}

apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-5_6/src/main/java/co/elastic/apm/agent/esrestclient/v5_6/ElasticsearchClientAsyncInstrumentation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public static Object[] onBeforeExecute(@Advice.Argument(0) String method,
8686
}
8787

8888
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
89-
public static void onAfterExecute(@Advice.Thrown @Nullable Throwable t,
90-
@Advice.Enter @Nullable Object[] entryArgs) {
89+
public static void onAfterExecute(@Advice.Enter @Nullable Object[] entryArgs) {
9190
if (entryArgs != null) {
9291
final Span<?> span = (Span<?>) entryArgs[0];
9392
if (span != null) {

apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-6_4/src/main/java/co/elastic/apm/agent/esrestclient/v6_4/ElasticsearchClientAsyncInstrumentation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public static Object[] onBeforeExecute(@Advice.Argument(0) Request request,
7777
}
7878

7979
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
80-
public static void onAfterExecute(@Advice.Thrown @Nullable Throwable t,
81-
@Advice.Enter @Nullable Object[] entryArgs) {
80+
public static void onAfterExecute(@Advice.Enter @Nullable Object[] entryArgs) {
8281
if (entryArgs != null) {
8382
final Span<?> span = (Span<?>) entryArgs[0];
8483
if (span != null) {

0 commit comments

Comments
 (0)