# Add custom serchable fields in logs

**URL:** <https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000>\
**Category:** APM\
**Tags:** java\
**Created:** [May 23, 2020, 1:54pm UTC](https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000 "2020-05-23T13:54:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniele\_Renda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniele_renda/32/39939_2.png) [@Daniele\_Renda](https://discuss.elastic.co/u/Daniele_Renda)\
**Post date:** [May 23, 2020, 1:54pm UTC](https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000/1 "2020-05-23T13:54:20Z")

</div>

I'm using Java APM client in my Spring Boot 2.2.7 application. It's a multi-tenant application, so I'd like to set (dynamically) tenant's id to the current transaction.

So far I configured the agent like this:

```auto
        # ********************************************************************?
        # Configuration file for APM ElasticSearch
        # https://www.elastic.co/guide/en/apm/agent/java/current /configuration.html#_option_reference
        # ********************************************************************

       recording = true
    service_name = my-server
    application_packages = my.package

    server_urls = https://myurl.com
    secret_token = secretToken

    # PERFORMANCE VALUES
    transaction_sample_rate = 0.50
    transaction_max_spans = 500
    capture_headers = false
    metrics_interval = 60s
    enable_jaxrs_annotation_inheritance = false
    profiling_inferred_spans_enabled = false
    ignore_exceptions = javax.validation.ConstraintViolationException

```

and this is the Spring config class:

```auto
    @Profile({"dev", "stage", "prod"})
    @Component
    @Order(Ordered.HIGHEST_PRECEDENCE)
    @Log4j2
    public class ApmConfig {

        @Autowired
        private Environment environment;

        @PostConstruct
        public void init() {
            log.info("Starting APM Java agent...");
            Map<String, String> propertyMap = new HashMap<>();
            String activeProfile = System.getProperty("spring.profiles.active");
            try {
                final Properties props = new Properties();
                try (InputStream resourceStream = ElasticApmAttacher.class.getClassLoader().getResourceAsStream("elasticapm-" + activeProfile + ".properties")) {
                    if (resourceStream != null) {
                        props.load(resourceStream);
                        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);
            }
            ElasticApmAttacher.attach(propertyMap);
        }
    }

```

I didn't touch anything else in my application and I'd like to continue to do so. Am I wondering if is there a way to set additional information (like context, but searchable) and what's the best place where I can set them in the Spring flow.

I guess I should use labels ([https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-span-add-tag](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-span-add-tag)) but I'd like to have some hint about it, expeciallly how to integrate them in a multi-tenant monolithic application where every request could be from a different tenant.

Thanks

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [May 24, 2020, 1:57pm UTC](https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000/2 "2020-05-24T13:57:22Z")

</div>

> [@Daniele\_Renda](#):
>
> I guess I should use labels but I'd like to have some hint about it

This contradicts that:

> [@Daniele\_Renda](#):
>
> I didn't touch anything else in my application and I'd like to continue to do so.

You would need to modify you app code in order to add labels. If this is an option, find a place in your code that:

1. is being executed during traced transactions
2. your tenant ID is accessible at

Than add the agent's [public API](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html) as a dependency of your app, and in your code, get the [current transaction](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-current-transaction) and add a label to it.

---

<div class="post-metadata">

**Author:** ![Daniele\_Renda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniele_renda/32/39939_2.png) [@Daniele\_Renda](https://discuss.elastic.co/u/Daniele_Renda)\
**Post date:** [May 24, 2020, 2:33pm UTC](https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000/3 "2020-05-24T14:33:42Z")

</div>

Hi, I ended up to add this code in my Filter (OncePerRequestFilter):

```auto
         private void decorateApmInformation(String tenantId, String userSid, String username) {
                // ******************************************************************** 
                // ADDITIONAL INFORMATION FOR APM METRICS
                // https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html
                // ********************************************************************
                try {
                    Span currentSpan = ElasticApm.currentSpan();
                    if (currentSpan != null) {
                        currentSpan.addLabel("tenantId", tenantId);
                    }

                    Transaction currentTransaction = ElasticApm.currentTransaction();
                    if (currentTransaction != null) {
                        currentTransaction.addLabel("tenantId", tenantId);
                        currentTransaction.setUser(userSid, null, username);
                    }
                } catch (Exception e) {
                    logger.error("", e);
                }
            }

```

It works, I hope it doesn't hit against any best practice 😉

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [May 24, 2020, 2:36pm UTC](https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000/4 "2020-05-24T14:36:40Z")

</div>

Looks good, only make sure the `currentSpan` and `currentTransaction` are not the same if you want to avoid the possibility to adding twice to transactions.

---

<div class="post-metadata">

**Author:** ![Daniele\_Renda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniele_renda/32/39939_2.png) [@Daniele\_Renda](https://discuss.elastic.co/u/Daniele_Renda)\
**Post date:** [June 1, 2020, 1:00pm UTC](https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000/5 "2020-06-01T13:00:36Z")

</div>

Thanks for your hint. How can I be sure there are not the same? Should I check their ID?

```
 try {
        Span currentSpan = ElasticApm.currentSpan();
        Transaction currentTransaction = ElasticApm.currentTransaction();
        if (currentTransaction != null) {
            currentTransaction.addLabel("tenantId", tenantId);
            currentTransaction.setUser(userSid, null, username);
        }

        if (currentSpan != null && !currentSpan.getId().equalsIgnoreCase(currentTransaction.getId())) {
            currentSpan.addLabel("tenantId", tenantId);
        }

    } catch (Exception e) {
        logger.error("", e);
    }

```

Thanks

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [June 1, 2020, 3:21pm UTC](https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000/6 "2020-06-01T15:21:30Z")

</div>

Yes, ID equality is the way to go 👍

---

<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:** [June 22, 2020, 11:21am UTC](https://discuss.elastic.co/t/add-custom-serchable-fields-in-logs/234000/7 "2020-06-22T11:21:30Z")

</div>

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