Does Java APM sample rate affect exceptions?

The documentation doesn't state it and I'd by default assume that APM's sample rate would only affect spans but not errors. But what I've recently observed was that a NullPointerException was thrown by JVM when trying to invoke a method on null reference in our code (i.e. it's in a package configured in elastic.apm.application_packages), that exception bubbled up all the way to the controller and was not caught by our code. That exception didn't end up in the APM's Errors section. What might cause that and how can I make sure that all unhandled exceptions end up in the APM?

Something to note: we're using JAX-RS, we have an ExceptionMapper configured which renders exceptions mostly as a generic "Internal server error" response

Sample rate should not impact capturing exceptions.

JAX-RS plugin currently only sets the transaction name to match the resource method.
Transaction is created with Servlet instrumentation, name is just set by jaxrs instrumentation.

Thus in this case, because the ExceptionMapper gets the thrown exception and transforms in a Response object, there is no "thrown exception" at Servlet level, where those are automatically captured and attached to the current transaction.

The issue with JAX-RS is that throwing exceptions is used in the "regular workflow" with subclasses of WebApplicationException for example to indicate something does not exists a NotFoundException is thrown, without being a relevant error to capture, thus capturing all exceptions that are sent to the ExceptionMapper isn't always relevant.

However, you could modify your ExceptionMapper to report those exceptions into the current transaction by using the agent API. in the case where the exception does not match anything you would expect from normal application behavior.

ElasticApm.currentTransaction().captureException(...);
2 Likes

That explains it, thanks Sylvain! Would the same apply to Spring Web MVC with its @ExceptionHandler?

Yes, that's exactly what happens with Spring @ExceptionHandler, and we suggest to apply the same approach (APM dont capture exception from @ExceptionHandler).

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.