# Does Java APM sample rate affect exceptions?

**URL:** <https://discuss.elastic.co/t/does-java-apm-sample-rate-affect-exceptions/239695>\
**Category:** APM\
**Tags:** java\
**Created:** [July 2, 2020, 6:18pm UTC](https://discuss.elastic.co/t/does-java-apm-sample-rate-affect-exceptions/239695 "2020-07-02T18:18:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikita2206](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@nikita2206](https://discuss.elastic.co/u/nikita2206)\
**Post date:** [July 2, 2020, 6:18pm UTC](https://discuss.elastic.co/t/does-java-apm-sample-rate-affect-exceptions/239695/1 "2020-07-02T18:18:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [July 7, 2020, 3:01pm UTC](https://discuss.elastic.co/t/does-java-apm-sample-rate-affect-exceptions/239695/2 "2020-07-07T15:01:20Z")

</div>

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](https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ExceptionMapper.html) 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](https://docs.oracle.com/javaee/7/api/javax/ws/rs/WebApplicationException.html) for example to indicate something does not exists a [NotFoundException](https://docs.oracle.com/javaee/7/api/javax/ws/rs/NotFoundException.html) 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](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#public-api). in the case where the exception does not match anything you would expect from normal application behavior.

```auto
ElasticApm.currentTransaction().captureException(...);

```

---

<div class="post-metadata">

**Author:** ![nikita2206](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@nikita2206](https://discuss.elastic.co/u/nikita2206)\
**Post date:** [July 7, 2020, 7:47pm UTC](https://discuss.elastic.co/t/does-java-apm-sample-rate-affect-exceptions/239695/3 "2020-07-07T19:47:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [July 8, 2020, 7:38am UTC](https://discuss.elastic.co/t/does-java-apm-sample-rate-affect-exceptions/239695/4 "2020-07-08T07:38:59Z")

</div>

Yes, that's exactly what happens with Spring `@ExceptionHandler`, and we suggest to apply the same approach ([APM dont capture exception from @ExceptionHandler](https://discuss.elastic.co/t/apm-dont-capture-exception-from-exceptionhandler/187530)).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 29, 2020, 3:39am UTC](https://discuss.elastic.co/t/does-java-apm-sample-rate-affect-exceptions/239695/5 "2020-07-29T03:39:02Z")

</div>

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