# Accessing CloudFoundry env vars from java agent

**URL:** <https://discuss.elastic.co/t/accessing-cloudfoundry-env-vars-from-java-agent/356129>\
**Category:** Elastic Observability\
**Created:** [March 25, 2024, 9:48pm UTC](https://discuss.elastic.co/t/accessing-cloudfoundry-env-vars-from-java-agent/356129 "2024-03-25T21:48:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [March 25, 2024, 9:48pm UTC](https://discuss.elastic.co/t/accessing-cloudfoundry-env-vars-from-java-agent/356129/1 "2024-03-25T21:48:28Z")

</div>

I'm using java agent 1.45.

How can I tell the java agent to use [CloudFoundry environment variables](https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html) to build the service.node.name? For example, I would like my node name to be something like ${vcap.application.name}-${instance\_index}. In other words, the nodes would be myApp-0, myApp-1, etc.

I understand that I probably need to set the node name in my user-defined service, but I don't know how to refer to environment variables there.

I'm using the buildpack approach to initialization.

thanks

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [March 25, 2024, 11:26pm UTC](https://discuss.elastic.co/t/accessing-cloudfoundry-env-vars-from-java-agent/356129/2 "2024-03-25T23:26:40Z")

</div>

Hi @johngregg

> [@johngregg](#):
>
> In other words, the nodes would be myApp-0, myApp-1, etc.

I'm pretty Sure The instance ID is part of the metadata captured... by the agent.. so you could always filter on that...

I'm not sure what you mean by node. In cloud foundry, there are App Instances. Is that what you mean?

I'm not sure why you want to include the instance id in the service name; that would seem to be a bit of an anti-pattern...

BUT if you wanted to do that, I think you would need to deploy with the manifest and do something like

```auto
applications:
- name: cardatabase-back-end
  buildpacks:
    - https://github.com/cloudfoundry/java-buildpack.git
  memory: 1G
  path: ./target/cardatabase-0.0.1-SNAPSHOT.jar
  services:
    - elastic-apm-service
  env:
    ELASTIC_APM_SERVICE_NAME: ${vcap.application.name}-${instance_index}
    JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{enabled: false}'

```

or something like that... I would try first by overriding the `service.name` with something static, then work on the env vars... unfortunately, I do not have a CF foundation anymore to test on...

---

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [March 26, 2024, 1:30pm UTC](https://discuss.elastic.co/t/accessing-cloudfoundry-env-vars-from-java-agent/356129/3 "2024-03-26T13:30:23Z")

</div>

You're mixing service.name and service.node.name. I'm talking about node name. Service name would be myApp and node names would be myApp-0, myApp-1, etc. I want the node names to show in the APM app as something human readable and not the default host name. In CloudFoundry, the host name is a guid.

I think this is probably more of a CF question about how to use environment variables.

thanks

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [March 26, 2024, 2:11pm UTC](https://discuss.elastic.co/t/accessing-cloudfoundry-env-vars-from-java-agent/356129/4 "2024-03-26T14:11:48Z")

</div>

Yes I think This is more of a CF question.

But to be clear / precise

There is no field

`service.node.name`

As part of the elastic Java APM agent configuration.

Is the elastic agent reporting this field as part of the APM output?

Can you show an actual example not theoretical?

Is that field already set? If so, can you show it?

If not, you could probably set that field as a label in the manifest using something similar to what I showed above

---

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [March 26, 2024, 2:34pm UTC](https://discuss.elastic.co/t/accessing-cloudfoundry-env-vars-from-java-agent/356129/5 "2024-03-26T14:34:14Z")

</div>

> **[Core configuration options | APM Java Agent Reference \[1.x\] | Elastic](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-service-node-name)**

I'm talking about this.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [March 26, 2024, 2:39pm UTC](https://discuss.elastic.co/t/accessing-cloudfoundry-env-vars-from-java-agent/356129/6 "2024-03-26T14:39:20Z")

</div>

Thanks for clarifying...then I think you would need to do something like

```auto
applications:
- name: cardatabase-back-end
  buildpacks:
    - https://github.com/cloudfoundry/java-buildpack.git
  memory: 1G
  path: ./target/cardatabase-0.0.1-SNAPSHOT.jar
  services:
    - elastic-apm-service
  env:
    ELASTIC_APM_SERVICE_NODE_NAME: ${vcap.application.name}-${instance_index}
    JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{enabled: false}'

```

Assuming those envs are available at `cf push` time so back to really a cf question I think...
