# Java agent set environment programatically

**URL:** <https://discuss.elastic.co/t/java-agent-set-environment-programatically/226575>\
**Category:** APM\
**Tags:** java\
**Created:** [April 5, 2020, 4:13pm UTC](https://discuss.elastic.co/t/java-agent-set-environment-programatically/226575 "2020-04-05T16:13:22Z")\
**Posts on this page:** 5\
**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:** [April 5, 2020, 4:13pm UTC](https://discuss.elastic.co/t/java-agent-set-environment-programatically/226575/1 "2020-04-05T16:13:23Z")

</div>

I'm attaching APM agent in my Spring Boot application like this:

```auto
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        ElasticApmAttacher.attach();
        SpringApplication.run(MyApplication.class, args);
    }
}
        }

```

I created elasticapm.properties file in my resource folder and everything works fine.  
However I'd like to se the environment property in the file programatically because I run the application with different Spring profile and I want APM add the right environment to logs.

I tried to pass a Map\<String,String\> to attach() method but it seems in this way values set in my property file is not read.

Do you have any suggestion? Thanks

---

<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:** [April 6, 2020, 7:55am UTC](https://discuss.elastic.co/t/java-agent-set-environment-programatically/226575/2 "2020-04-06T07:55:44Z")

</div>

Hi @Daniele_Renda,

You are initializing the agent using the **Programatic API** , which has some limitations when combined with other ways to provide configuration.

As explained in our [configuration page in documentation](https://www.elastic.co/guide/en/apm/agent/java/current/configuration.html#configuration), configuration sources for (5) are mutually exclusive, and in your case you are trying to mix items in (b) and (c).

 ![Screenshot from 2020-04-06 09-53-43](https://us1.discourse-cdn.com/elastic/original/3X/6/d/6da40d26446557aab6b4f549a0887506ec43af31.png)

---

<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:** [April 6, 2020, 10:39am UTC](https://discuss.elastic.co/t/java-agent-set-environment-programatically/226575/3 "2020-04-06T10:39:22Z")

</div>

Hi,  
thanks for your reply.

I ended up to do this with Spring, what do you think about?

```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<>();
            try {
                final Properties props = new Properties();
                try (InputStream resourceStream = ElasticApmAttacher.class.getClassLoader().getResourceAsStream("elasticapm.properties")) {
                    if (resourceStream != null) {
                        props.load(resourceStream);
                        for (String propertyName : props.stringPropertyNames()) {
                            propertyMap.put(propertyName, props.getProperty(propertyName));
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //Add environment value
                propertyMap.put("environment", System.getProperty("spring.profiles.active"));
            } catch (Exception e) {
                log.error("", e);
            }
            ElasticApmAttacher.attach(propertyMap);
        }
    }

```

---

<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:** [April 6, 2020, 1:10pm UTC](https://discuss.elastic.co/t/java-agent-set-environment-programatically/226575/4 "2020-04-06T13:10:55Z")

</div>

That should work, you could even use the fact that `Properties` is in fact a map and avoid copy everything (will require a cast).

---

<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:** [April 27, 2020, 9:11am UTC](https://discuss.elastic.co/t/java-agent-set-environment-programatically/226575/5 "2020-04-27T09:11:05Z")

</div>

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