-
Notifications
You must be signed in to change notification settings - Fork 317
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
improve servlet error capture #812
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9604ac8
merge conflict resolved
410e3ca
Merge remote-tracking branch 'upstream/master'
a50dcf6
added exception handle insturmentation
cd68c3f
fixed test message
bb4ebd1
updated assciidoc
a9b7c3a
added feature for changelog
bb85347
fixed according to comments
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...gin/src/main/java/co/elastic/apm/agent/spring/webmvc/ExceptionHandlerInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.webmvc; | ||
|
||
import co.elastic.apm.agent.bci.ElasticApmInstrumentation; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
public class ExceptionHandlerInstrumentation extends ElasticApmInstrumentation { | ||
|
||
@Override | ||
public Class<?> getAdviceClass() { | ||
return ExceptionHandlerAdviceService.class; | ||
} | ||
|
||
public static class ExceptionHandlerAdviceService { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void captureException(@Advice.Argument(0) HttpServletRequest request, | ||
@Advice.Argument(3) Exception e) { | ||
if (request != null) { | ||
request.setAttribute("co.elastic.apm.exception", e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> getTypeMatcher() { | ||
return named("org.springframework.web.servlet.DispatcherServlet"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<? super MethodDescription> getMethodMatcher() { | ||
return named("processHandlerException") | ||
.and(takesArgument(0, named("javax.servlet.http.HttpServletRequest"))) | ||
.and(takesArgument(1, named("javax.servlet.http.HttpServletResponse"))) | ||
.and(takesArgument(2, named("java.lang.Object"))) | ||
.and(takesArgument(3, named("java.lang.Exception"))) | ||
.and(returns(named("org.springframework.web.servlet.ModelAndView"))); | ||
} | ||
|
||
@Override | ||
public Collection<String> getInstrumentationGroupNames() { | ||
return Arrays.asList("exception-handler"); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...n/src/main/resources/META-INF/services/co.elastic.apm.agent.bci.ElasticApmInstrumentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
co.elastic.apm.agent.spring.webmvc.SpringTransactionNameInstrumentation | ||
co.elastic.apm.agent.spring.webmvc.DispatcherServletRenderInstrumentation | ||
co.elastic.apm.agent.spring.webmvc.SpringServiceNameInstrumentation | ||
co.elastic.apm.agent.spring.webmvc.ExceptionHandlerInstrumentation |
103 changes: 103 additions & 0 deletions
103
...lastic/apm/agent/spring/webmvc/exception/AbstractExceptionHandlerInstrumentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.webmvc.exception; | ||
|
||
import co.elastic.apm.agent.MockReporter; | ||
import co.elastic.apm.agent.bci.ElasticApmAgent; | ||
import co.elastic.apm.agent.configuration.SpyConfiguration; | ||
import co.elastic.apm.agent.impl.ElasticApmTracer; | ||
import co.elastic.apm.agent.impl.ElasticApmTracerBuilder; | ||
import co.elastic.apm.agent.servlet.ServletInstrumentation; | ||
import co.elastic.apm.agent.spring.webmvc.ExceptionHandlerInstrumentation; | ||
import net.bytebuddy.agent.ByteBuddyAgent; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.Arrays; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(value = SpringJUnit4ClassRunner.class) | ||
@WebAppConfiguration | ||
@TestConfiguration | ||
public abstract class AbstractExceptionHandlerInstrumentationTest { | ||
|
||
protected static MockReporter reporter; | ||
protected static ElasticApmTracer tracer; | ||
protected MockMvc mockMvc; | ||
|
||
@Autowired | ||
WebApplicationContext wac; | ||
|
||
@BeforeClass | ||
@BeforeAll | ||
public static void setUpAll() { | ||
reporter = new MockReporter(); | ||
tracer = new ElasticApmTracerBuilder() | ||
.configurationRegistry(SpyConfiguration.createSpyConfig()) | ||
.reporter(reporter) | ||
.build(); | ||
ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install(), | ||
Arrays.asList(new ServletInstrumentation(tracer), new ExceptionHandlerInstrumentation())); | ||
} | ||
|
||
@AfterClass | ||
@AfterAll | ||
public static void afterAll() { | ||
ElasticApmAgent.reset(); | ||
} | ||
|
||
@Before | ||
@BeforeEach | ||
public void setup() { | ||
this.mockMvc = | ||
MockMvcBuilders.webAppContextSetup(wac).build(); | ||
} | ||
|
||
protected void assertExceptionCapture(Class exceptionClazz, MockHttpServletResponse response, int statusCode, String responseContent, String exceptionMessage) throws UnsupportedEncodingException { | ||
assertEquals(1, reporter.getTransactions().size()); | ||
assertEquals(0, reporter.getSpans().size()); | ||
assertEquals(1, reporter.getErrors().size()); | ||
assertEquals(exceptionMessage, reporter.getErrors().get(0).getException().getMessage()); | ||
assertEquals(exceptionClazz, reporter.getErrors().get(0).getException().getClass()); | ||
assertEquals(statusCode, response.getStatus()); | ||
assertEquals(responseContent, response.getContentAsString()); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...gent/spring/webmvc/exception/ExceptionHandlerInstrumentationWithExceptionHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.webmvc.exception; | ||
|
||
import co.elastic.apm.agent.spring.webmvc.exception.testapp.exception_handler.ExceptionHandlerRuntimeException; | ||
import co.elastic.apm.agent.spring.webmvc.exception.testapp.exception_handler.ExceptionHandlerController; | ||
import org.junit.Test; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
|
||
@ContextConfiguration(classes = { | ||
ExceptionHandlerController.class}) | ||
public class ExceptionHandlerInstrumentationWithExceptionHandlerTest extends AbstractExceptionHandlerInstrumentationTest { | ||
|
||
@Test | ||
public void testExceptionCapture() throws Exception { | ||
ResultActions resultActions = this.mockMvc.perform(get("/exception-handler/throw-exception")); | ||
MvcResult result = resultActions.andReturn(); | ||
MockHttpServletResponse response = result.getResponse(); | ||
|
||
assertExceptionCapture(ExceptionHandlerRuntimeException.class, response, 409, "exception-handler runtime exception occured", "runtime exception occured"); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ent/spring/webmvc/exception/ExceptionHandlerInstrumentationWithExceptionResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.webmvc.exception; | ||
|
||
import co.elastic.apm.agent.spring.webmvc.exception.testapp.exception_resolver.ExceptionResolverController; | ||
import co.elastic.apm.agent.spring.webmvc.exception.testapp.exception_resolver.ExceptionResolverRuntimeException; | ||
import co.elastic.apm.agent.spring.webmvc.exception.testapp.exception_resolver.RestResponseStatusExceptionResolver; | ||
import org.junit.Test; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
|
||
@ContextConfiguration(classes = { | ||
ExceptionResolverController.class, | ||
RestResponseStatusExceptionResolver.class}) | ||
public class ExceptionHandlerInstrumentationWithExceptionResolverTest extends AbstractExceptionHandlerInstrumentationTest { | ||
|
||
@Test | ||
public void testCallApiWithExceptionThrown() throws Exception { | ||
ResultActions resultActions = this.mockMvc.perform(get("/exception-resolver/throw-exception")); | ||
MvcResult result = resultActions.andReturn(); | ||
MockHttpServletResponse response = result.getResponse(); | ||
|
||
assertExceptionCapture(ExceptionResolverRuntimeException.class, response, 200, "", "runtime exception occured"); | ||
assertEquals("error-page", response.getForwardedUrl()); | ||
assertEquals("runtime exception occured", result.getModelAndView().getModel().get("message")); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...pm/agent/spring/webmvc/exception/ExceptionHandlerInstrumentationWithGlobalAdviceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.spring.webmvc.exception; | ||
|
||
import co.elastic.apm.agent.spring.webmvc.exception.testapp.controller_advice.ControllerAdviceController; | ||
import co.elastic.apm.agent.spring.webmvc.exception.testapp.controller_advice.ControllerAdviceRuntimeException; | ||
import co.elastic.apm.agent.spring.webmvc.exception.testapp.controller_advice.GlobalExceptionHandler; | ||
import org.junit.Test; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
|
||
@ContextConfiguration(classes = { | ||
ControllerAdviceController.class, | ||
GlobalExceptionHandler.class | ||
}) | ||
public class ExceptionHandlerInstrumentationWithGlobalAdviceTest extends AbstractExceptionHandlerInstrumentationTest { | ||
|
||
@Test | ||
public void testExceptionCaptureWithGlobalControllerAdvice() throws Exception { | ||
|
||
ResultActions resultActions = this.mockMvc.perform(get("/controller-advice/throw-exception")); | ||
MvcResult result = resultActions.andReturn(); | ||
MockHttpServletResponse response = result.getResponse(); | ||
|
||
assertExceptionCapture(ControllerAdviceRuntimeException.class, response, 409, "controller-advice runtime exception occured", "runtime exception occured"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do:
instead, does the relevant test fail now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with instrumentation:
without:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's failing now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, then it is properly tested 🙂
Thanks!