Java client, add random code to Exception

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).


Well, there are lots of options in this regard.

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 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 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.

I hope this helps.

Thanks, really helpful hints. Just a clarification: I do use Spring Boot and this is the way I attach APM:

@Profile({"dev", "stage", "prod"})
public class ApmConfig {

private Environment environment;

public void init() {"Starting APM Java agent...");
    Map<String, String> propertyMap = new HashMap<>();
    String activeProfile = System.getProperty("");
    try {
        final Properties props = new Properties();
        try (InputStream resourceStream = ElasticApmAttacher.class.getClassLoader().getResourceAsStream("elasticapm-" + activeProfile + ".properties")) {
            if (resourceStream != null) {
                for (String propertyName : props.stringPropertyNames()) {
                    propertyMap.put(propertyName, props.getProperty(propertyName));
        } catch (IOException e) {
            log.error("", e);
        //Add environment value
        propertyMap.put("environment", activeProfile);
    } catch (Exception e) {
        log.error("", e);


Exceptions are sent to APM without any other settings by my side. When you say

Then use the captureException API to report an APM error.

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 :slight_smile:

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