I'm using APM Java client in my Spring Boot 2.3 application. At the beginning I used it to trace performance but right now I see the great advantace to intercept exceptions as well.
Before of APM I was saving exceptions in ElasticSearch as weel doing something manual. Of course with the agent everything is simpler but I'm wondering how to do a thing that right now I was able to accomplish with my implementation: when an exception occur, I generate a random code useful to track the exception itself.
Example: an user in the UI is doing something and an exception server side is raised. When I return the exception I add a random code and I return back the message "Error [123-645-578]: you cannot perform this action right now.". This is very useful for support because the customer can just tell the code of the exception and I'm able to track it without asking anything else (date/time, what he was doing, his tenant's id, etc).
I'm wondering how can I do the same thing with APM adding this new field (that should be searchable, of course).
To begin with, I assume the currentTransaction's ID would serve well for such correlation. Just before creating the exception, get the current transaction's ID and use it for your exception message. Then use the captureException API to report an APM error. You can then use the transaction.id field to search for the relevant trace/transaction/error in the APM UI. Note that since 1.14.0, the captureException API returns the Elastic APM error ID. It is too late if you do it this way to use in the exception message, but be aware and take advantage if you can (it is also searchable through the error.id field).
In addition, take a look at our logging correlation capabilities to see how you can get those transaction ID and error ID written to your logs, which you can ingest as well and search in the same way in Kibana.
Is that call needed in my case? I think there is already some kind of interceptor that automatically intercept Exception in my Spring application and send them.
Do you have any "Spring" hint to give to keep in mind in addition to that already said?
Apparently not. It would be needed if you caught the Exception somewhere outside a traced span/transaction, but it doesn't seem to be the case. Whenever an exception is thrown out of a traced method, the agent sends it automatically (doing pretty much the same thing that captureException is doing).
Not that I can think of. You probably know your Spring dependencies much better than I do
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.