From 5a0442c226337f93949eaa9480e844277f5c8c3f Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:37:07 +0100 Subject: [PATCH] draft eager exception capture --- .../agent/awssdk/v1/AbstractAws1ClientIT.java | 18 ++++++++++ .../agent/awssdk/v2/AbstractAws2ClientIT.java | 18 ++++++++++ .../apm/agent/servlet/EagerThrowable.java | 34 +++++++++++++++++++ .../servlet/ServletTransactionHelper.java | 13 ++++++- ...SpringExceptionHandlerInstrumentation.java | 4 ++- 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/EagerThrowable.java diff --git a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-1-plugin/src/test/java/co/elastic/apm/agent/awssdk/v1/AbstractAws1ClientIT.java b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-1-plugin/src/test/java/co/elastic/apm/agent/awssdk/v1/AbstractAws1ClientIT.java index 0d1fba6cf6..d7b30d82d2 100644 --- a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-1-plugin/src/test/java/co/elastic/apm/agent/awssdk/v1/AbstractAws1ClientIT.java +++ b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-1-plugin/src/test/java/co/elastic/apm/agent/awssdk/v1/AbstractAws1ClientIT.java @@ -1,3 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package co.elastic.apm.agent.awssdk.v1; import co.elastic.apm.agent.awssdk.common.AbstractAwsClientIT; diff --git a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-2-plugin/src/test/java/co/elastic/apm/agent/awssdk/v2/AbstractAws2ClientIT.java b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-2-plugin/src/test/java/co/elastic/apm/agent/awssdk/v2/AbstractAws2ClientIT.java index b0162779c1..83f17b6d4c 100644 --- a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-2-plugin/src/test/java/co/elastic/apm/agent/awssdk/v2/AbstractAws2ClientIT.java +++ b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-2-plugin/src/test/java/co/elastic/apm/agent/awssdk/v2/AbstractAws2ClientIT.java @@ -1,3 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package co.elastic.apm.agent.awssdk.v2; import co.elastic.apm.agent.awssdk.common.AbstractAwsClientIT; diff --git a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/EagerThrowable.java b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/EagerThrowable.java new file mode 100644 index 0000000000..f92c3f80f6 --- /dev/null +++ b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/EagerThrowable.java @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.servlet; + +public class EagerThrowable extends Throwable { + + private final Class originalType; + + public EagerThrowable(Throwable t) { + super(t.getMessage(), t.getCause(), false, false); + setStackTrace(t.getStackTrace()); + this.originalType = t.getClass(); + } + + public Class getOriginalType() { + return originalType; + } +} diff --git a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java index 4b2545aa22..79a492e251 100644 --- a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java +++ b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java @@ -232,7 +232,18 @@ public void onAfter(Transaction transaction, @Nullable Throwable exception, b @Nullable String servletPath, @Nullable String pathInfo, @Nullable String contentTypeHeader, boolean deactivate) { try { // thrown the first time a JSP is invoked in order to register it - if (exception != null && "weblogic.servlet.jsp.AddToMapException".equals(exception.getClass().getName())) { + boolean ignoreTransaction = false; + + if(exception != null){ + Class exceptionType; + if (exception instanceof EagerThrowable) { + exceptionType = ((EagerThrowable) exception).getOriginalType(); + } else { + exceptionType = exception.getClass(); + } + ignoreTransaction = "weblogic.servlet.jsp.AddToMapException".equals(exceptionType.getName()); + } + if(ignoreTransaction){ transaction.ignoreTransaction(); } else { doOnAfter(transaction, exception, committed, status, overrideStatusCodeOnThrowable, method, diff --git a/apm-agent-plugins/apm-spring-webmvc/apm-spring-webmvc-spring5/src/main/java/co/elastic/apm/agent/springwebmvc/AbstractSpringExceptionHandlerInstrumentation.java b/apm-agent-plugins/apm-spring-webmvc/apm-spring-webmvc-spring5/src/main/java/co/elastic/apm/agent/springwebmvc/AbstractSpringExceptionHandlerInstrumentation.java index 331f8131a4..6ca43f463b 100644 --- a/apm-agent-plugins/apm-spring-webmvc/apm-spring-webmvc-spring5/src/main/java/co/elastic/apm/agent/springwebmvc/AbstractSpringExceptionHandlerInstrumentation.java +++ b/apm-agent-plugins/apm-spring-webmvc/apm-spring-webmvc-spring5/src/main/java/co/elastic/apm/agent/springwebmvc/AbstractSpringExceptionHandlerInstrumentation.java @@ -20,6 +20,7 @@ import co.elastic.apm.agent.sdk.ElasticApmInstrumentation; import co.elastic.apm.agent.servlet.Constants; +import co.elastic.apm.agent.servlet.EagerThrowable; import co.elastic.apm.agent.servlet.adapter.ServletRequestAdapter; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; @@ -63,7 +64,8 @@ public static void captureRequestError(ServletRequestAdapte @Nullable HttpServletRequest request, @Nullable Exception e) { if (request != null && e != null) { - adapter.setAttribute(request, "co.elastic.apm.exception", e); + + adapter.setAttribute(request, "co.elastic.apm.exception", new EagerThrowable(e)); } } }