From d0f108afbb461a343fa680c2253f49370dfa6783 Mon Sep 17 00:00:00 2001 From: Jonas Kunz Date: Wed, 6 Mar 2024 17:33:24 +0100 Subject: [PATCH] Avoid instanceof if redacted exceptions are active (#3553) * Avoid instanceof if redacted exceptions are active * Added changelog * Fix test --- CHANGELOG.asciidoc | 5 +++++ .../java/co/elastic/apm/agent/impl/ElasticApmTracer.java | 2 +- .../test/java/org/example/stacktrace/ErrorCaptureTest.java | 5 +++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 76b046e922..af2d6513f2 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -31,6 +31,11 @@ Use subheadings with the "=====" level for adding notes for unreleased changes: === Unreleased +[float] +===== Bug fixes +* Avoid another case where we might touch application exceptions for `safe_exceptions` - {pull}3553[#3553] + + [[release-notes-1.x]] === Java Agent version 1.x diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java index 624bfc3f2f..6980d1812e 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java @@ -1002,7 +1002,7 @@ public Service createService(String ephemeralId) { @Override @Nullable public Throwable redactExceptionIfRequired(@Nullable Throwable original) { - if (original != null && coreConfiguration.isRedactExceptions() && !(original instanceof RedactedException)) { + if (original != null && coreConfiguration.isRedactExceptions()) { return new RedactedException(); } return original; diff --git a/apm-agent-core/src/test/java/org/example/stacktrace/ErrorCaptureTest.java b/apm-agent-core/src/test/java/org/example/stacktrace/ErrorCaptureTest.java index 5137144396..580a1eac28 100644 --- a/apm-agent-core/src/test/java/org/example/stacktrace/ErrorCaptureTest.java +++ b/apm-agent-core/src/test/java/org/example/stacktrace/ErrorCaptureTest.java @@ -117,7 +117,8 @@ void testExceptionRedaction() { Throwable redacted = tracer.redactExceptionIfRequired(exception); assertThat(redacted).isInstanceOf(RedactedException.class); - assertThat(tracer.redactExceptionIfRequired(redacted)).isSameAs(redacted); + // double redaction means no instanceof check + assertThat(tracer.redactExceptionIfRequired(redacted)).isNotSameAs(redacted); ErrorCapture errorCapture = tracer.captureException(exception, tracer.currentContext(), null); assertThat(errorCapture).isNotNull(); @@ -125,7 +126,7 @@ void testExceptionRedaction() { ErrorCapture alreadyRedacted = tracer.captureException(redacted, tracer.currentContext(), null); assertThat(alreadyRedacted).isNotNull(); - assertThat(alreadyRedacted.getException()).isSameAs(redacted); + assertThat(alreadyRedacted.getException()).isNotSameAs(redacted); }