Skip to content

Add Elastic Otel Java agent - #1360

Merged
stokpop merged 8 commits into
cloudfoundry:mainfrom
jackshirazi:main
Oct 22, 2026
Merged

stokpop merged 8 commits into
cloudfoundry:mainfrom
jackshirazi:main

Conversation

@jackshirazi

Copy link
Copy Markdown
Contributor

We have customers requesting our agent to be added to cloudfoundry. I've used the existing Elastic APM agent as a template

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@jackshirazi

Copy link
Copy Markdown
Contributor Author

@kiril-keranov @stokpop you seem to be active maintainers, could you please tell me what I need to do to progress this PR

@kiril-keranov kiril-keranov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. Left some comments on findings

Comment thread src/integration/frameworks_test.go Resolved

@stokpop stokpop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes on this PR @jackshirazi!

After another review round: please check cf cups example that might trigger two otel agents.

Also, the uri in the manifest should point to cloud foundry repo that pulls in via cloudfoundry repo. For that we need to add the elastic otel agent also to buildpacks-ci repo, which will replace the uri with its own automated "version update PR" after this is merged. (So no need to update the uri in this PR, the maven central uri is fine.) See: cloudfoundry/buildpacks-ci#682

stokpop added a commit to cloudfoundry/buildpacks-ci that referenced this pull request Oct 7, 2026
Add elastic-otel-javaagent to dependency-builds config, mirroring
elastic-apm-agent setup, to track new releases of the Elastic
Distribution of OpenTelemetry Java agent added in
cloudfoundry/java-buildpack#1360.
@stokpop

stokpop commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

FYI: the automatic PR for updates already works: see #1452

We will merge that one after this PR is merged: it will the also use the latest-greatest 1.13 version it found of the agent (so no need to update the 1.11 in this PR).

@stokpop

stokpop commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Thanks @jackshirazi, ab78843 fixes the documented cf cups my-elastic-otel-service ... case: only the Elastic OTel agent is installed now, and the registry test covers it. 🙏

While testing, I found a few less common setups where two agents (or a different one) can still start:

  • An Elastic service binding plus OTEL_EXPORTER_OTLP_ENDPOINT set as an env var during staging starts both the Splunk and Elastic agents.
  • An Elastic service without credentials starts the upstream OpenTelemetry agent instead of Elastic, because the name matches otel.
  • ELASTIC_OTEL_AGENT set plus an otel-collector binding starts both the OpenTelemetry and Elastic agents.

These aren't specific to this PR. Similar overlaps already exist between the Splunk and upstream OpenTelemetry agents. Rather than adding more skip checks to each framework, we'd prefer a general solution: the framework registry picks one OTel-based agent. I've opened an issue for that: #1469.

One behaviour change for existing apps, for the release notes: an app already bound to a service named or tagged elastic-otel, edot-java or elastic-edot that has an endpoint and credentials will get the Elastic OTel agent after restaging. Today such an app gets the upstream OpenTelemetry agent if the name contains otel, otherwise none.

I'll discuss with the other maintainers whether we can merge this with these cases documented as known limitations. To help with that, could you add a short troubleshooting section to docs/framework-elastic_otel_java_agent.md? Here's a suggestion you could paste:

Troubleshooting: multiple agents or wrong agent

  • Elastic and Splunk agents both start: OTEL_EXPORTER_OTLP_ENDPOINT is set as an env var during staging. Also set ELASTIC_OTEL_AGENT=true, or put the endpoint in the service credentials instead.
  • Upstream OpenTelemetry agent starts instead of Elastic: the Elastic service lacks credentials. Add api_key or otel.exporter.otlp.headers.
  • Elastic and OpenTelemetry agents both start: an otel-collector service or another otel-named or otel-tagged service is also bound. Unbind it.
  • Telemetry not reaching Elastic: OTEL_EXPORTER_OTLP_ENDPOINT, set as an env var during staging, overrides the endpoint from the service binding. Remove it, or make sure it points to Elastic.

Also, findElasticOtelService matches edot-java and elastic-edot too, but the docs only mention elastic-otel. Could you list all three under "Detection Criterion" and "User-Provided Service"?

Thanks again for all the work and patience on this one!

@jackshirazi

Copy link
Copy Markdown
Contributor Author

docs updated per suggestion, thank you @stokpop

@stokpop

stokpop commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Hi @jackshirazi, I ran the integration tests for this PR merged into current main. Both Elastic OTel tests fail: the app never starts.

Root cause: the header Authorization=ApiKey <key> contains a space. It is written single-quoted as -Dotel.exporter.otlp.headers='...' into JAVA_OPTS, but the spring_boot_staged fixture starts with exec java $JAVA_OPTS (unquoted). The shell splits the value and the JVM gets explicit-key' as the main class (ClassNotFoundException). Apps with similar start scripts would fail the same way.

The Elastic APM framework uses the same quoting, but its tokens (and test values) contain no spaces, so it does not hit this. Here the ApiKey prefix always adds one.

Suggested fix: pass the headers as the OTEL_EXPORTER_OTLP_HEADERS env var (e.g. via a profile.d script) instead of a -D system property. This matches the EDOT Java setup docs, which configure OTEL_EXPORTER_OTLP_HEADERS="Authorization=ApiKey <key>". Benefits:

  • no shell quoting/splitting issues at all
  • the API key does not show up on the JVM command line (ps, crash logs, dumps)
  • it is the standard way to configure authentication for EDOT/OTel; there is no dedicated API key setting
  • if the value is resolved at app start (from VCAP_SERVICES) instead of at staging, it is not baked into the droplet, so rotating the API key only needs cf restart instead of cf restage

Note: a -D for the same setting would override the env var, so only one of the two should be written.

I verified this locally against a small OTLP listener: both integration tests pass and the agent sends Authorization: ApiKey <key> correctly, on both the javaexec and the start-script path.

Alternative, if -D is really needed: percent-encode the header values and write them without shell quoting, e.g. -Dotel.exporter.otlp.headers=Authorization=ApiKey%20ab%2Bc%2Fd%3D%3D. OTLP headers use the W3C Baggage format, and OTel Java decodes values with URLDecoder (so + must be %2B). Also verified locally. Only encoding the space is not enough: with an unquoted $JAVA_OPTS the single quotes stay literal and the agent sends a header named 'Authorization.

Suggestion: one test could use apps/integration_valid (javaexec launcher, the common Spring Boot path) while the other stays on spring_boot_staged (start-script path), so both start paths are covered. I can rerun the integration tests locally after your changes.

Thanks!

@jackshirazi

Copy link
Copy Markdown
Contributor Author

@stokpop thank you for the detailed testing and catching that. Hopefully addressed with the latest commit

@stokpop

stokpop commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Thanks @jackshirazi for the update! 👍 4045ce0 fixes the startup failures I saw.

Test results

  • Unit tests: green, all 18 Elastic OTel specs run (none skipped).
  • Integration tests on cflinuxfs5 (PR merged with current main): 116 passed, 0 failed, 11 skipped, including Elastic OTel, Elastic APM, OpenTelemetry and Splunk.
  • Manual check in a cflinuxfs5 container: OTEL_EXPORTER_OTLP_HEADERS is set at runtime and the key no longer shows up in JAVA_OPTS.

Runtime resolution
Resolving the headers at runtime is a nice direction: the key stays out of the droplet, a key rotation only needs a restart, and it works with CredHub references, which are interpolated at launch. The Node.js buildpack does something similar with jq in its profile.d scripts.

To my surprise, updating API keys with only a restart instead of a restage is not widely supported yet: the other agent frameworks in this buildpack (for example OpenTelemetry, Elastic APM, Splunk OTel and Azure Application Insights, among others) write their credentials into the droplet during staging, so a key rotation needs a restage there.

One issue: + in the key
The OTel Java agent URL-decodes header values, so a + turns into a space. With api_key: k+ey/== the collector receives Authorization=ApiKey k ey/==. A percent-encoded value arrives correctly, so encoding in jq should fix it:

jq -rn '"Authorization=ApiKey " + ("k+ey/=="|@uri)'
# Authorization=ApiKey k%2Bey%2F%3D%3D

The unit test with right+key/== currently expects the unencoded value, so it would need an update too.

Smaller points

  • The runtime jq test is skipped when jq is missing. jq is on cflinuxfs4 and cflinuxfs5, so I would let it fail instead of skipping, otherwise a missing jq goes unnoticed.
  • The service lookup now exists in Go (detection) and in jq (runtime). A short comment on both sides to keep them in sync would help future changes.

@jackshirazi

Copy link
Copy Markdown
Contributor Author

@stokpop Thanks again for the comprehensive testing! I have applied the suggested changes. Any further feedback is definitely welcome, but I won't be able to act on it for a week

@stokpop

stokpop commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Thanks @jackshirazi, d7114ba addresses all my points (encoded header values, jq test no longer skipped, sync comments).

I ran the tests again with this PR merged into current main: unit tests are green, and the integration tests on cflinuxfs5 pass (116 passed, 0 failed, 11 skipped), including Elastic OTel, Elastic APM, OpenTelemetry and Splunk.

All looks fine from my side. I will discuss with the other maintainers whether it can be merged.

@stokpop stokpop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stokpop
stokpop requested a review from kiril-keranov October 21, 2026 05:44
@stokpop

stokpop commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

FYI: found out that the otel match was wider in the recent buildpack than before v5 migration, which also caused double agents. See PR to revert this so it also matches the current docs: #1471

@stokpop
stokpop merged commit 74453d5 into cloudfoundry:main Oct 22, 2026
1 check passed
@stokpop stokpop linked an issue Oct 24, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

elastic-otel-java support

3 participants