Accessing CloudFoundry env vars from java agent

I'm using java agent 1.45.

How can I tell the java agent to use CloudFoundry environment variables 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

Hi @johngregg

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

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

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

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

I'm talking about this.

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

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