diff --git a/README.md b/README.md index 489bd72cf5..609f29c31a 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ For historical analysis documents from development sessions, see [`docs/archive/ * [DataDog](docs/framework-datadog_javaagent.md) ([Configuration](docs/framework-datadog_javaagent.md#configuration) * [Debug](docs/framework-debug.md) ([Configuration](docs/framework-debug.md#configuration)) * [Elastic APM Agent](docs/framework-elastic_apm_agent.md) ([Configuration](docs/framework-elastic_apm_agent.md#configuration)) + * [Elastic OTel Java Agent](docs/framework-elastic_otel_java_agent.md) ([Configuration](docs/framework-elastic_otel_java_agent.md#configuration)) * [Dynatrace SaaS/Managed OneAgent](docs/framework-dynatrace_one_agent.md) ([Configuration](docs/framework-dynatrace_one_agent.md#configuration)) * [Google Stackdriver Profiler](docs/framework-google_stackdriver_profiler.md) ([Configuration](docs/framework-google_stackdriver_profiler.md#configuration)) * [Introscope Agent](docs/framework-introscope_agent.md) ([Configuration](docs/framework-introscope_agent.md#configuration)) diff --git a/docs/framework-elastic_otel_java_agent.md b/docs/framework-elastic_otel_java_agent.md new file mode 100644 index 0000000000..308148afbf --- /dev/null +++ b/docs/framework-elastic_otel_java_agent.md @@ -0,0 +1,84 @@ +# Elastic OTel Java Agent Framework + +The Elastic OTel Java Agent Framework causes an application to be automatically configured to work with the [Elastic Distribution of OpenTelemetry Java][]. + + + + + + + + + + +
Detection CriterionExistence of a bound Elastic OTel service. The service must have a name containing, or a label or tag matching, elastic-otel, edot-java, or elastic-edot, and the combined service and environment configuration must provide an OTLP endpoint plus authentication credentials. The framework can also be enabled explicitly with ELASTIC_OTEL_AGENT when the same required configuration is present.
Tagselastic-otel-javaagent=<version>
+ +Tags are printed to standard output by the buildpack detect script. + +## User-Provided Service + +Users can provide their own Elastic OTel service. A user-provided service must have a name containing or a tag matching `elastic-otel`, `edot-java`, or `elastic-edot` so that the framework configures the application with the Elastic OTel Java agent. + +The credential payload can contain the following entries. + +| Name | Description | +| ---- | ----------- | +| `otel.exporter.otlp.endpoint` | The OTLP endpoint for your Elastic deployment or EDOT Collector. Aliases: `otlp_endpoint`, `otlpEndpoint`, `endpoint`. | +| `otel.exporter.otlp.headers` | Explicit OTLP headers, for example `Authorization=ApiKey `. If present, this takes precedence over derived authentication headers. | +| `api_key` | Elastic API key. Converted to `otel.exporter.otlp.headers=Authorization=ApiKey `. | +| `secret_token` | Bearer token. Converted to `otel.exporter.otlp.headers=Authorization=Bearer `. | +| `otel.*` | Any additional OpenTelemetry configuration applied as JVM system properties. | +| `elastic.otel.*` | Any additional Elastic OTel configuration applied as JVM system properties. | + +### Creating an Elastic OTel User-Provided Service + +Example minimal configuration: + +``` +cf cups my-elastic-otel-service -p '{"otel.exporter.otlp.endpoint":"https://my-deployment.ingest.us-west1.gcp.cloud.es.io","api_key":"my-api-key"}' +``` + +Example configuration with explicit OTLP headers and resource attributes: + +``` +cf cups my-elastic-otel-service -p '{"otel.exporter.otlp.endpoint":"https://my-deployment.ingest.us-west1.gcp.cloud.es.io","otel.exporter.otlp.headers":"Authorization=ApiKey my-api-key","otel.resource.attributes":"deployment.environment.name=production"}' +``` + +Bind your application to the service using: + +`cf bind-service my-app-name my-elastic-otel-service` + +or use the `services` block in the application manifest file. + +## Configuration + +For general information on configuring the buildpack, including how to specify configuration values through environment variables, refer to [Configuration and Extension][]. + +The framework uses the dependency version configured in `manifest.yml`. + +| Name | Description | +| ---- | ----------- | +| `version` | The version of Elastic OTel Java agent to use. The current buildpack manifest uses the `elastic-otel-javaagent` dependency. | + +The framework sets `otel.service.name` from service credentials, then `OTEL_SERVICE_NAME`, then `VCAP_APPLICATION.application_name`, using the first configured value. It also sets `deployment.environment.name` from `VCAP_APPLICATION.space_name` when `otel.resource.attributes` is not configured. Generic OTLP environment variables do not select the Elastic distribution by themselves; set `ELASTIC_OTEL_AGENT` to choose it explicitly. + +Authentication headers are provided through `OTEL_EXPORTER_OTLP_HEADERS` rather than a JVM system property. When the headers come from service credentials, the framework resolves them from `VCAP_SERVICES` when the application starts. This keeps credentials out of the JVM command line and allows credential rotation with an application restart instead of a restage. + +> **Warning** +> Do not bind this framework alongside the Elastic APM Agent, OpenTelemetry Javaagent, Splunk OTel Java Agent, or another Java agent framework for the same application. Running multiple Java agents in the same JVM can cause duplicate telemetry or conflicting instrumentation. + +## Troubleshooting: Multiple Agents or Wrong Agent + +- **Elastic and Splunk agents both start:** `OTEL_EXPORTER_OTLP_ENDPOINT` is set as an environment variable 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 authentication 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 does not reach Elastic:** `OTEL_EXPORTER_OTLP_ENDPOINT`, set as an environment variable during staging, overrides the endpoint from the service binding. Remove it, or make sure it points to Elastic. + +## Migration From Elastic APM Agent + +The Elastic OTel Java agent uses OTLP configuration. It does not use Elastic APM agent settings such as `server_url` or `secret_token` with the `elastic.apm.*` prefix. + +When migrating, create a new service binding named or tagged for Elastic OTel and provide an OTLP endpoint plus `otel.exporter.otlp.headers` or `api_key`. + +[Configuration and Extension]: ../README.md#configuration-and-extension +[Elastic Distribution of OpenTelemetry Java]: https://www.elastic.co/docs/reference/opentelemetry/edot-sdks/java diff --git a/docs/framework-ordering.md b/docs/framework-ordering.md index d6158fffbe..0ae902d02d 100644 --- a/docs/framework-ordering.md +++ b/docs/framework-ordering.md @@ -50,7 +50,8 @@ Line | Framework Name | Priority | Notes 75 | SpringAutoReconfiguration | 41 | Spring framework 76 | SplunkOtelJavaAgent | 42 | Observability agent 77 | SpringInsight | 43 | Spring monitoring -78 | SkyWalkingAgent | 44 | APM agent +78 | SkyWalkingAgent | 41 | APM agent +78a | ElasticOtelJavaAgent | 44 | Observability agent 79 | YourKitProfiler | 45 | Profiler 80 | JavaSecurity | 47 | Security configuration 81 | JavaOpts | 99 | ⚠️ USER-DEFINED OPTS (ALWAYS LAST) diff --git a/manifest.yml b/manifest.yml index 75fd64e81a..0b09d875c8 100644 --- a/manifest.yml +++ b/manifest.yml @@ -21,6 +21,7 @@ packaging_profiles: exclude: - datadog-javaagent - elastic-apm-agent + - elastic-otel-javaagent - azure-application-insights - skywalking-agent - splunk-otel-javaagent @@ -44,6 +45,7 @@ packaging_profiles: exclude: - datadog-javaagent - elastic-apm-agent + - elastic-otel-javaagent - azure-application-insights - skywalking-agent - splunk-otel-javaagent @@ -94,6 +96,8 @@ default_versions: version: 1.x - name: elastic-apm-agent version: 1.x +- name: elastic-otel-javaagent + version: 1.x - name: azure-application-insights version: 3.x - name: skywalking-agent @@ -182,6 +186,9 @@ url_to_dependency_map: - match: elastic-apm-agent-(\d+\.\d+\.\d+) name: elastic-apm-agent version: "$1" +- match: elastic-otel-javaagent[-_](\d+\.\d+\.\d+) + name: elastic-otel-javaagent + version: "$1" - match: applicationinsights-agent-(\d+\.\d+\.\d+) name: azure-application-insights version: "$1" @@ -321,6 +328,15 @@ dependencies: - cflinuxfs5 source: https://repo1.maven.org/maven2/co/elastic/apm/elastic-apm-agent/1.56.0/elastic-apm-agent-1.56.0.jar source_sha256: '' +- name: elastic-otel-javaagent + version: 1.11.0 + uri: https://repo1.maven.org/maven2/co/elastic/otel/elastic-otel-javaagent/1.11.0/elastic-otel-javaagent-1.11.0.jar + sha256: 61c0aae98e165ddfa3096d5d8ed16c159992b8ae284bf6296800839008cdb707 + cf_stacks: + - cflinuxfs4 + - cflinuxfs5 + source: https://repo1.maven.org/maven2/co/elastic/otel/elastic-otel-javaagent/1.11.0/elastic-otel-javaagent-1.11.0.jar + source_sha256: 61c0aae98e165ddfa3096d5d8ed16c159992b8ae284bf6296800839008cdb707 - name: google-stackdriver-profiler version: 0.4.0 uri: https://storage.googleapis.com/cloud-profiler/java/latest/profiler_java_agent.tar.gz diff --git a/src/integration/frameworks_test.go b/src/integration/frameworks_test.go index 1ae274d9bf..93eed912b0 100644 --- a/src/integration/frameworks_test.go +++ b/src/integration/frameworks_test.go @@ -400,6 +400,45 @@ func testFrameworks(platform switchblade.Platform, fixtures string) func(*testin }) }) + context("with Elastic OTel service binding", func() { + it("detects and installs Elastic OTel Java agent", func() { + deployment, logs, err := platform.Deploy. + WithServices(map[string]switchblade.Service{ + "elastic-otel": { + "otel.exporter.otlp.endpoint": "https://elastic-otel.example.com:443", + "api_key": "test-api-key", + }, + }). + WithEnv(map[string]string{ + "BP_JAVA_VERSION": "17", + }). + Execute(name, filepath.Join(fixtures, "apps", "integration_valid")) + Expect(err).NotTo(HaveOccurred(), logs.String) + + Expect(logs.String()).To(ContainSubstring("Elastic OTel")) + Eventually(deployment).Should(matchers.Serve(ContainSubstring("Hello from Java Main!"))) + }) + + it("configures Elastic OTel with explicit OTLP headers", func() { + deployment, logs, err := platform.Deploy. + WithServices(map[string]switchblade.Service{ + "my-elastic-otel": { + "otel.exporter.otlp.endpoint": "https://elastic-otel.production.example.com:443", + "otel.exporter.otlp.headers": "Authorization=ApiKey explicit-key", + "otel.service.name": "my-java-app", + }, + }). + WithEnv(map[string]string{ + "BP_JAVA_VERSION": "17", + }). + Execute(name, filepath.Join(fixtures, "containers", "spring_boot_staged")) + Expect(err).NotTo(HaveOccurred(), logs.String) + + Expect(logs.String()).To(ContainSubstring("Elastic OTel")) + Eventually(deployment).Should(matchers.Serve(ContainSubstring("Hello from Spring Boot"))) + }) + }) + context("with OpenTelemetry service binding", func() { it("detects and installs OpenTelemetry Javaagent", func() { deployment, logs, err := platform.Deploy. diff --git a/src/java/frameworks/elastic_otel_java_agent.go b/src/java/frameworks/elastic_otel_java_agent.go new file mode 100644 index 0000000000..9a85fbc418 --- /dev/null +++ b/src/java/frameworks/elastic_otel_java_agent.go @@ -0,0 +1,335 @@ +// Cloud Foundry Java Buildpack +// Copyright 2013-2026 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package frameworks + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/cloudfoundry/java-buildpack/src/java/common" +) + +// ElasticOtelJavaAgentFramework represents the Elastic Distribution of OpenTelemetry Java agent framework. +type ElasticOtelJavaAgentFramework struct { + context *common.Context + jarPath string + service *common.VCAPService +} + +// Keep this list and the matching precedence in findElasticOtelService synchronized with +// the runtime jq lookup in elasticOtelHeadersProfileTemplate. +var elasticOtelServiceIdentifiers = []string{"elastic-otel", "edot-java", "elastic-edot"} + +// NewElasticOtelJavaAgentFramework creates a new Elastic OTel Java agent framework instance. +func NewElasticOtelJavaAgentFramework(ctx *common.Context) *ElasticOtelJavaAgentFramework { + return &ElasticOtelJavaAgentFramework{context: ctx} +} + +// Detect checks if the Elastic OTel Java agent should be enabled. +func (e *ElasticOtelJavaAgentFramework) Detect() (string, error) { + service := e.findElasticOtelService() + + if os.Getenv("ELASTIC_OTEL_AGENT") != "" { + if !hasRequiredElasticOtelConfiguration(service) { + e.context.Log.Debug("Elastic OTel Java agent: ELASTIC_OTEL_AGENT requires an OTLP endpoint and authentication credentials") + return "", nil + } + e.service = service + e.context.Log.Debug("Elastic OTel Java agent framework detected via ELASTIC_OTEL_AGENT") + return "elastic-otel-javaagent", nil + } + + if service == nil { + e.context.Log.Debug("Elastic OTel Java agent: no elastic-otel service found") + return "", nil + } + if !hasRequiredElasticOtelConfiguration(service) { + e.context.Log.Debug("Elastic OTel Java agent: service missing OTLP endpoint or authentication credentials") + return "", nil + } + + e.service = service + e.context.Log.Debug("Elastic OTel Java agent framework detected") + return "elastic-otel-javaagent", nil +} + +// Supply downloads and installs the Elastic OTel Java agent. +func (e *ElasticOtelJavaAgentFramework) Supply() error { + e.context.Log.Debug("Installing Elastic OTel Java agent") + + dep, err := e.context.Manifest.DefaultVersion(e.DependencyIdentifier()) + if err != nil { + return fmt.Errorf("unable to find Elastic OTel Java agent in manifest: %w", err) + } + + agentDir := filepath.Join(e.context.Stager.DepDir(), "elastic_otel_java_agent") + if err := e.context.Installer.InstallDependency(dep, agentDir); err != nil { + return fmt.Errorf("failed to install Elastic OTel Java agent: %w", err) + } + if err := e.constructJarPath(agentDir); err != nil { + return fmt.Errorf("elastic OTel Java agent JAR path not found during supply: %w", err) + } + + e.context.Log.Info("Elastic OTel Java agent %s installed", dep.Version) + return nil +} + +// Finalize configures the Elastic OTel Java agent. +func (e *ElasticOtelJavaAgentFramework) Finalize() error { + agentDir := filepath.Join(e.context.Stager.DepDir(), "elastic_otel_java_agent") + if err := e.constructJarPath(agentDir); err != nil { + return fmt.Errorf("elastic OTel Java agent JAR path not found during finalize: %w", err) + } + + e.context.Log.BeginStep("Configuring Elastic OTel Java agent") + + relPath, err := filepath.Rel(e.context.Stager.DepDir(), e.jarPath) + if err != nil { + return fmt.Errorf("failed to determine relative path for Elastic OTel Java agent: %w", err) + } + runtimeJarPath := filepath.Join(fmt.Sprintf("$DEPS_DIR/%s", e.context.Stager.DepsIdx()), relPath) + + config := e.buildConfiguration() + opts := []string{fmt.Sprintf("-javaagent:%s", runtimeJarPath)} + for key, value := range config { + opts = append(opts, formatJavaSystemProperty(key, value)) + } + + if err := writeJavaOptsFile(e.context, 44, "elastic_otel_java_agent", strings.Join(opts, " ")); err != nil { + return fmt.Errorf("failed to write JAVA_OPTS for Elastic OTel Java agent: %w", err) + } + if err := e.context.Stager.WriteProfileD("elastic_otel_java_agent.sh", buildElasticOtelHeadersProfile()); err != nil { + return fmt.Errorf("failed to write Elastic OTel Java agent profile script: %w", err) + } + + e.context.Log.Debug("Elastic OTel Java agent configured") + return nil +} + +const elasticOtelHeadersProfileTemplate = `#!/usr/bin/env bash +# Resolve Elastic OTLP authentication at runtime so credentials are not added to JAVA_OPTS or the droplet. +if [ -z "${OTEL_EXPORTER_OTLP_HEADERS:-}" ] && [ -n "${VCAP_SERVICES:-}" ]; then + if ! command -v jq >/dev/null 2>&1; then + echo "WARNING: Elastic OTel Java agent could not resolve OTLP headers: jq is not available" >&2 + elif elastic_otel_headers="$( + printf '%%s' "$VCAP_SERVICES" | jq -r ' + [ + # Keep this lookup precedence synchronized with findElasticOtelService. + %s as $identifiers + | [ + to_entries[] + | .key as $label + | .value[] + | ((.tags // []) | map(ascii_downcase)) as $tags + | ((.name // "") | ascii_downcase) as $name + | ($identifiers | index(($label | ascii_downcase))) as $label_priority + | if $label_priority != null then + {priority: $label_priority, service: .} + elif (any($tags[]; . as $tag | $identifiers | index($tag) != null)) or + (any($identifiers[]; . as $identifier | $name | contains($identifier))) then + {priority: ($identifiers | length), service: .} + else + empty + end + ] + | sort_by(.priority) + | .[] + | (.service.credentials // {}) + | if ((."otel.exporter.otlp.headers" // "") | length) > 0 then + ."otel.exporter.otlp.headers" + elif ((.api_key // "") | length) > 0 then + "Authorization=ApiKey " + (.api_key | @uri) + elif ((.secret_token // "") | length) > 0 then + "Authorization=Bearer " + (.secret_token | @uri) + elif ((.access_token // "") | length) > 0 then + "Authorization=Bearer " + (.access_token | @uri) + else + empty + end + ][0] // empty + ' + )"; then + if [ -n "$elastic_otel_headers" ]; then + export OTEL_EXPORTER_OTLP_HEADERS="$elastic_otel_headers" + fi + unset elastic_otel_headers + else + echo "WARNING: Elastic OTel Java agent could not resolve OTLP headers from VCAP_SERVICES" >&2 + unset elastic_otel_headers + fi +fi +` + +func buildElasticOtelHeadersProfile() string { + identifiers, _ := json.Marshal(elasticOtelServiceIdentifiers) + return fmt.Sprintf(elasticOtelHeadersProfileTemplate, identifiers) +} + +func (e *ElasticOtelJavaAgentFramework) findElasticOtelService() *common.VCAPService { + vcapServices, err := GetVCAPServices() + if err != nil { + e.context.Log.Warning("Failed to parse VCAP_SERVICES: %s", err.Error()) + return nil + } + + return findElasticOtelService(vcapServices) +} + +func findElasticOtelService(vcapServices common.VCAPServices) *common.VCAPService { + for _, label := range elasticOtelServiceIdentifiers { + if service := vcapServices.GetService(label); service != nil { + return service + } + } + for _, services := range vcapServices { + for _, service := range services { + for _, identifier := range elasticOtelServiceIdentifiers { + if hasAnyTag(service, identifier) || common.ContainsIgnoreCase(service.Name, identifier) { + return &service + } + } + } + } + + return nil +} + +func hasRequiredElasticOtelConfiguration(service *common.VCAPService) bool { + endpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") + headers := os.Getenv("OTEL_EXPORTER_OTLP_HEADERS") + if service != nil && service.Credentials != nil { + if endpoint == "" { + endpoint = getOtlpEndpoint(service.Credentials) + } + if headers == "" { + headers = getOtlpHeaders(service.Credentials) + } + } + return endpoint != "" && headers != "" +} + +func (e *ElasticOtelJavaAgentFramework) buildConfiguration() map[string]string { + config := map[string]string{} + if service := e.findElasticOtelService(); service != nil { + e.service = service + for key, value := range service.Credentials { + strValue, ok := value.(string) + if !ok || strValue == "" { + continue + } + if key != "otel.exporter.otlp.headers" && + (strings.HasPrefix(key, "otel.") || strings.HasPrefix(key, "elastic.otel.")) { + config[key] = strValue + } + } + if endpoint := getOtlpEndpoint(service.Credentials); endpoint != "" { + config["otel.exporter.otlp.endpoint"] = endpoint + } + } + + if endpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"); endpoint != "" { + config["otel.exporter.otlp.endpoint"] = endpoint + } + if _, ok := config["otel.service.name"]; !ok { + if serviceName := os.Getenv("OTEL_SERVICE_NAME"); serviceName != "" { + config["otel.service.name"] = serviceName + } else if appName := GetApplicationName(false); appName != "" { + config["otel.service.name"] = appName + } + } + if _, ok := config["otel.resource.attributes"]; !ok { + if spaceName := getSpaceName(); spaceName != "" { + config["otel.resource.attributes"] = "deployment.environment.name=" + spaceName + } + } + + return config +} + +func getOtlpEndpoint(credentials map[string]interface{}) string { + for _, key := range []string{"otel.exporter.otlp.endpoint", "otlp_endpoint", "otlpEndpoint", "endpoint"} { + if value, ok := credentials[key].(string); ok && value != "" { + return value + } + } + return "" +} + +func getOtlpHeaders(credentials map[string]interface{}) string { + if value, ok := credentials["otel.exporter.otlp.headers"].(string); ok && value != "" { + return value + } + if value, ok := credentials["api_key"].(string); ok && value != "" { + return "Authorization=ApiKey " + value + } + if value, ok := credentials["secret_token"].(string); ok && value != "" { + return "Authorization=Bearer " + value + } + if value, ok := credentials["access_token"].(string); ok && value != "" { + return "Authorization=Bearer " + value + } + return "" +} + +func formatJavaSystemProperty(key, value string) string { + return fmt.Sprintf("-D%s=%s", key, shellEscape(value)) +} + +func hasAnyTag(service common.VCAPService, tags ...string) bool { + for _, actual := range service.Tags { + for _, expected := range tags { + if strings.EqualFold(actual, expected) { + return true + } + } + } + return false +} + +func getSpaceName() string { + vcapApp := os.Getenv("VCAP_APPLICATION") + if vcapApp == "" { + return "" + } + var appData map[string]interface{} + if err := json.Unmarshal([]byte(vcapApp), &appData); err != nil { + return "" + } + if spaceName, ok := appData["space_name"].(string); ok { + return spaceName + } + return "" +} + +func (e *ElasticOtelJavaAgentFramework) constructJarPath(agentDir string) error { + jarPattern := filepath.Join(agentDir, e.DependencyIdentifier()+"*.jar") + matches, err := filepath.Glob(jarPattern) + if err != nil { + return fmt.Errorf("failed to search for Elastic OTel javaagent jar: %w", err) + } + if len(matches) == 0 { + return fmt.Errorf("elastic OTel Java agent jar not found after installation in %s", agentDir) + } + e.jarPath = matches[0] + return nil +} + +func (e *ElasticOtelJavaAgentFramework) DependencyIdentifier() string { + return "elastic-otel-javaagent" +} diff --git a/src/java/frameworks/elastic_otel_java_agent_test.go b/src/java/frameworks/elastic_otel_java_agent_test.go new file mode 100644 index 0000000000..ef1267fe0c --- /dev/null +++ b/src/java/frameworks/elastic_otel_java_agent_test.go @@ -0,0 +1,382 @@ +// Cloud Foundry Java Buildpack +// Copyright 2013-2026 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package frameworks_test + +import ( + "bytes" + "os" + "os/exec" + "path/filepath" + + "github.com/cloudfoundry/java-buildpack/src/java/common" + "github.com/cloudfoundry/java-buildpack/src/java/frameworks" + "github.com/cloudfoundry/libbuildpack" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("ElasticOtelJavaAgentFramework", func() { + var ( + ctx *common.Context + framework *frameworks.ElasticOtelJavaAgentFramework + tmpDir string + depsDir string + ) + + BeforeEach(func() { + var err error + tmpDir, err = os.MkdirTemp("", "elastic-otel-javaagent-test-*") + Expect(err).NotTo(HaveOccurred()) + + depsDir = filepath.Join(tmpDir, "deps") + Expect(os.MkdirAll(filepath.Join(depsDir, "0"), 0755)).To(Succeed()) + + agentDir := filepath.Join(depsDir, "0", "elastic_otel_java_agent") + Expect(os.MkdirAll(agentDir, 0755)).To(Succeed()) + Expect(os.WriteFile( + filepath.Join(agentDir, "elastic-otel-javaagent-1.11.0.jar"), + []byte("fake jar"), 0644, + )).To(Succeed()) + + logger := libbuildpack.NewLogger(GinkgoWriter) + manifest := &libbuildpack.Manifest{} + stager := libbuildpack.NewStager([]string{tmpDir, "", depsDir, "0"}, logger, manifest) + + ctx = &common.Context{ + Stager: stager, + Manifest: manifest, + Log: logger, + } + framework = frameworks.NewElasticOtelJavaAgentFramework(ctx) + }) + + AfterEach(func() { + os.RemoveAll(tmpDir) + os.Unsetenv("VCAP_SERVICES") + os.Unsetenv("VCAP_APPLICATION") + os.Unsetenv("ELASTIC_OTEL_AGENT") + os.Unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT") + os.Unsetenv("OTEL_EXPORTER_OTLP_HEADERS") + os.Unsetenv("OTEL_SERVICE_NAME") + }) + + Describe("Detect", func() { + It("does not detect without service binding or explicit environment", func() { + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(BeEmpty()) + }) + + It("detects an elastic-otel service with endpoint and api key", func() { + os.Setenv("VCAP_SERVICES", `{ + "elastic-otel": [{ + "name": "my-elastic-otel", + "label": "elastic-otel", + "tags": [], + "credentials": { + "otel.exporter.otlp.endpoint": "https://elastic.example.com:443", + "api_key": "abc123" + } + }] + }`) + + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(Equal("elastic-otel-javaagent")) + }) + + It("detects a user-provided service tagged edot-java", func() { + os.Setenv("VCAP_SERVICES", `{ + "user-provided": [{ + "name": "telemetry", + "label": "user-provided", + "tags": ["edot-java"], + "credentials": { + "endpoint": "https://elastic.example.com:443", + "otel.exporter.otlp.headers": "Authorization=ApiKey abc123" + } + }] + }`) + + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(Equal("elastic-otel-javaagent")) + }) + + It("detects a service name containing elastic-otel", func() { + os.Setenv("VCAP_SERVICES", `{ + "user-provided": [{ + "name": "prod-elastic-otel", + "label": "user-provided", + "tags": [], + "credentials": { + "otlp_endpoint": "https://elastic.example.com:443", + "secret_token": "secret" + } + }] + }`) + + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(Equal("elastic-otel-javaagent")) + }) + + It("does not detect via ELASTIC_OTEL_AGENT without required configuration", func() { + os.Setenv("ELASTIC_OTEL_AGENT", "true") + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(BeEmpty()) + }) + + It("detects via ELASTIC_OTEL_AGENT with OTLP endpoint and headers", func() { + os.Setenv("ELASTIC_OTEL_AGENT", "true") + os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "https://elastic.example.com:443") + os.Setenv("OTEL_EXPORTER_OTLP_HEADERS", "Authorization=ApiKey abc123") + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(Equal("elastic-otel-javaagent")) + }) + + It("does not claim generic OTLP endpoint and headers configuration", func() { + os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "https://elastic.example.com:443") + os.Setenv("OTEL_EXPORTER_OTLP_HEADERS", "Authorization=ApiKey abc123") + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(BeEmpty()) + }) + + It("does not detect a generic OpenTelemetry collector service", func() { + os.Setenv("VCAP_SERVICES", `{ + "otel-collector": [{ + "name": "my-otel", + "label": "otel-collector", + "tags": ["otel"], + "credentials": { + "endpoint": "http://collector:4318" + } + }] + }`) + + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(BeEmpty()) + }) + + It("does not detect an Elastic APM service", func() { + os.Setenv("VCAP_SERVICES", `{ + "elastic-apm": [{ + "name": "my-elastic-apm", + "label": "elastic-apm", + "tags": ["elastic-apm"], + "credentials": { + "server_url": "https://apm.example.com:8200", + "secret_token": "secret" + } + }] + }`) + + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(BeEmpty()) + }) + + It("does not detect an elastic-otel service missing authentication", func() { + os.Setenv("VCAP_SERVICES", `{ + "elastic-otel": [{ + "name": "my-elastic-otel", + "label": "elastic-otel", + "tags": [], + "credentials": { + "endpoint": "https://elastic.example.com:443" + } + }] + }`) + + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(BeEmpty()) + }) + + It("does not detect on OTLP endpoint environment variable alone", func() { + os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "https://elastic.example.com:443") + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(BeEmpty()) + }) + }) + + Describe("Finalize", func() { + optsFile := func() string { + return filepath.Join(depsDir, "0", "java_opts", "44_elastic_otel_java_agent.opts") + } + profileFile := func() string { + return filepath.Join(depsDir, "0", "profile.d", "elastic_otel_java_agent.sh") + } + + It("writes javaagent configuration and resolves the API key header at runtime", func() { + os.Setenv("VCAP_APPLICATION", `{"application_name":"my-cf-app","space_name":"production"}`) + os.Setenv("VCAP_SERVICES", `{ + "elastic-otel": [{ + "name": "my-elastic-otel", + "label": "elastic-otel", + "tags": [], + "credentials": { + "otel.exporter.otlp.endpoint": "https://elastic.example.com:443", + "api_key": "abc123", + "elastic.otel.javaagent.log.level": "DEBUG" + } + }] + }`) + + Expect(framework.Finalize()).To(Succeed()) + + data, err := os.ReadFile(optsFile()) + Expect(err).NotTo(HaveOccurred()) + opts := string(data) + Expect(opts).To(ContainSubstring("-javaagent:$DEPS_DIR/0/elastic_otel_java_agent/elastic-otel-javaagent-1.11.0.jar")) + Expect(opts).To(ContainSubstring("-Dotel.exporter.otlp.endpoint=https://elastic.example.com:443")) + Expect(opts).NotTo(ContainSubstring("otel.exporter.otlp.headers")) + Expect(opts).NotTo(ContainSubstring("abc123")) + Expect(opts).To(ContainSubstring("-Delastic.otel.javaagent.log.level=DEBUG")) + Expect(opts).To(ContainSubstring("-Dotel.service.name=my-cf-app")) + Expect(opts).To(ContainSubstring("-Dotel.resource.attributes='deployment.environment.name=production'")) + Expect(opts).NotTo(ContainSubstring(tmpDir)) + + data, err = os.ReadFile(profileFile()) + Expect(err).NotTo(HaveOccurred()) + profile := string(data) + Expect(profile).To(ContainSubstring(`export OTEL_EXPORTER_OTLP_HEADERS="$elastic_otel_headers"`)) + Expect(profile).To(ContainSubstring(`"Authorization=ApiKey " + (.api_key | @uri)`)) + Expect(profile).To(ContainSubstring(`"$VCAP_SERVICES"`)) + Expect(profile).NotTo(ContainSubstring("abc123")) + }) + + It("uses explicit OTEL service configuration without copying headers into JAVA_OPTS", func() { + os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "https://env.elastic.example.com:443") + os.Setenv("OTEL_EXPORTER_OTLP_HEADERS", "Authorization=ApiKey env-key") + os.Setenv("OTEL_SERVICE_NAME", "env-service") + + Expect(framework.Finalize()).To(Succeed()) + + data, err := os.ReadFile(optsFile()) + Expect(err).NotTo(HaveOccurred()) + opts := string(data) + Expect(opts).To(ContainSubstring("-Dotel.exporter.otlp.endpoint=https://env.elastic.example.com:443")) + Expect(opts).NotTo(ContainSubstring("otel.exporter.otlp.headers")) + Expect(opts).NotTo(ContainSubstring("env-key")) + Expect(opts).To(ContainSubstring("-Dotel.service.name=env-service")) + + data, err = os.ReadFile(profileFile()) + Expect(err).NotTo(HaveOccurred()) + Expect(string(data)).To(ContainSubstring(`[ -z "${OTEL_EXPORTER_OTLP_HEADERS:-}" ]`)) + Expect(string(data)).NotTo(ContainSubstring("env-key")) + }) + + It("resolves headers at runtime and prioritizes an explicit service label", func() { + _, err := exec.LookPath("jq") + Expect(err).NotTo(HaveOccurred(), "jq is required to resolve Elastic OTel headers at runtime") + + runtimeServices := `{ + "user-provided": [{ + "name": "first-elastic-otel-service", + "label": "user-provided", + "tags": [], + "credentials": {"api_key": "wrong-key"} + }], + "elastic-otel": [{ + "name": "preferred-service", + "label": "elastic-otel", + "tags": [], + "credentials": {"api_key": "right+key/=="} + }] + }` + os.Setenv("VCAP_SERVICES", runtimeServices) + Expect(framework.Finalize()).To(Succeed()) + + cmd := exec.Command("bash", "-c", `. "$1"; printf '%s' "${OTEL_EXPORTER_OTLP_HEADERS:-}"`, "bash", profileFile()) + cmd.Env = []string{"PATH=" + os.Getenv("PATH"), "VCAP_SERVICES=" + runtimeServices} + output, err := cmd.Output() + Expect(err).NotTo(HaveOccurred()) + Expect(string(output)).To(Equal("Authorization=ApiKey right%2Bkey%2F%3D%3D")) + }) + + It("preserves an existing headers environment variable without requiring jq", func() { + Expect(framework.Finalize()).To(Succeed()) + + emptyPath := GinkgoT().TempDir() + cmd := exec.Command("bash", "-c", `. "$1"; printf '%s' "$OTEL_EXPORTER_OTLP_HEADERS"`, "bash", profileFile()) + cmd.Env = []string{ + "PATH=" + emptyPath, + "VCAP_SERVICES={invalid", + "OTEL_EXPORTER_OTLP_HEADERS=Authorization=ApiKey existing-key", + } + output, err := cmd.Output() + Expect(err).NotTo(HaveOccurred()) + Expect(string(output)).To(Equal("Authorization=ApiKey existing-key")) + }) + + It("warns but does not abort startup when jq is unavailable", func() { + Expect(framework.Finalize()).To(Succeed()) + + emptyPath := GinkgoT().TempDir() + cmd := exec.Command("bash", "-e", "-c", `. "$1"; printf '%s' "${OTEL_EXPORTER_OTLP_HEADERS:-}"`, "bash", profileFile()) + cmd.Env = []string{"PATH=" + emptyPath, `VCAP_SERVICES={"elastic-otel":[]}`} + var stderr bytes.Buffer + cmd.Stderr = &stderr + output, err := cmd.Output() + Expect(err).NotTo(HaveOccurred()) + Expect(output).To(BeEmpty()) + Expect(stderr.String()).To(ContainSubstring("jq is not available")) + }) + + It("warns but does not abort startup when jq cannot parse VCAP_SERVICES", func() { + Expect(framework.Finalize()).To(Succeed()) + + cmd := exec.Command("bash", "-e", "-c", `jq() { return 2; }; . "$1"; printf '%s' "${OTEL_EXPORTER_OTLP_HEADERS:-}"`, "bash", profileFile()) + cmd.Env = []string{`VCAP_SERVICES={invalid`} + var stderr bytes.Buffer + cmd.Stderr = &stderr + output, err := cmd.Output() + Expect(err).NotTo(HaveOccurred()) + Expect(output).To(BeEmpty()) + Expect(stderr.String()).To(ContainSubstring("could not resolve OTLP headers from VCAP_SERVICES")) + }) + + It("preserves the service name configured by the service binding", func() { + os.Setenv("OTEL_SERVICE_NAME", "env-service") + os.Setenv("VCAP_SERVICES", `{ + "elastic-otel": [{ + "name": "my-elastic-otel", + "label": "elastic-otel", + "tags": [], + "credentials": { + "otel.exporter.otlp.endpoint": "https://elastic.example.com:443", + "api_key": "abc123", + "otel.service.name": "binding-service" + } + }] + }`) + + Expect(framework.Finalize()).To(Succeed()) + + data, err := os.ReadFile(optsFile()) + Expect(err).NotTo(HaveOccurred()) + opts := string(data) + Expect(opts).To(ContainSubstring("-Dotel.service.name=binding-service")) + Expect(opts).NotTo(ContainSubstring("-Dotel.service.name=env-service")) + }) + }) +}) diff --git a/src/java/frameworks/framework.go b/src/java/frameworks/framework.go index bf51010af8..e2157f7f79 100644 --- a/src/java/frameworks/framework.go +++ b/src/java/frameworks/framework.go @@ -96,6 +96,7 @@ func (r *Registry) RegisterStandardFrameworks() { r.Register(NewGoogleStackdriverProfilerFramework(r.context)) r.Register(NewIntroscopeAgentFramework(r.context)) r.Register(NewOpenTelemetryJavaagentFramework(r.context)) + r.Register(NewElasticOtelJavaAgentFramework(r.context)) r.Register(NewRiverbedAppInternalsAgentFramework(r.context)) r.Register(NewSkyWalkingAgentFramework(r.context)) r.Register(NewSplunkOtelJavaAgentFramework(r.context)) diff --git a/src/java/frameworks/framework_test.go b/src/java/frameworks/framework_test.go index 35294cd82d..b769b0bf74 100644 --- a/src/java/frameworks/framework_test.go +++ b/src/java/frameworks/framework_test.go @@ -289,6 +289,28 @@ var _ = Describe("Framework Registry", func() { Expect(detected).To(HaveLen(2)) Expect(names).To(ContainElements("New Relic Agent", "AppDynamics Agent")) }) + + It("detects only Elastic OTel for an Elastic OTel service binding", func() { + registry.Register(frameworks.NewOpenTelemetryJavaagentFramework(ctx)) + registry.Register(frameworks.NewElasticOtelJavaAgentFramework(ctx)) + + os.Setenv("VCAP_SERVICES", `{ + "user-provided": [{ + "name": "my-elastic-otel-service", + "label": "user-provided", + "tags": [], + "credentials": { + "otel.exporter.otlp.endpoint": "https://my-deployment.ingest.us-west1.gcp.cloud.es.io", + "api_key": "my-api-key" + } + }] + }`) + + detected, names, err := registry.DetectAll() + Expect(err).NotTo(HaveOccurred()) + Expect(detected).To(HaveLen(1)) + Expect(names).To(ConsistOf("elastic-otel-javaagent")) + }) }) }) diff --git a/src/java/frameworks/java_opts_writer.go b/src/java/frameworks/java_opts_writer.go index 8e0dcfcd58..562f9cadd5 100644 --- a/src/java/frameworks/java_opts_writer.go +++ b/src/java/frameworks/java_opts_writer.go @@ -35,6 +35,7 @@ import ( // - 40: Seeker Security Provider // - 41: SkyWalking Agent // - 42: Splunk OTEL Java Agent +// - 44: Elastic OTel Java Agent // - 45: YourKit Profiler // - 46: Takipi Agent // - 99: User JAVA_OPTS (always last) diff --git a/src/java/frameworks/open_telemetry_javaagent.go b/src/java/frameworks/open_telemetry_javaagent.go index 8badd569f2..9f2fd0ff8d 100644 --- a/src/java/frameworks/open_telemetry_javaagent.go +++ b/src/java/frameworks/open_telemetry_javaagent.go @@ -28,6 +28,12 @@ func (o *OpenTelemetryJavaagentFramework) Detect() (string, error) { return "", nil } + if service := findElasticOtelService(vcapServices); service != nil && + hasRequiredElasticOtelConfiguration(service) { + o.context.Log.Debug("OpenTelemetry Javaagent skipped because an Elastic OTel service is bound") + return "", nil + } + // OpenTelemetry can be bound as: // - "otel-collector" service (required by Ruby implementation) // - Services with "otel" or "opentelemetry" tag @@ -152,4 +158,3 @@ func (o *OpenTelemetryJavaagentFramework) constructJarPath(agentDir string) erro func (o *OpenTelemetryJavaagentFramework) DependencyIdentifier() string { return "open-telemetry-javaagent" } - diff --git a/src/java/frameworks/splunk_otel_java_agent.go b/src/java/frameworks/splunk_otel_java_agent.go index c29e303606..8b924de0d2 100644 --- a/src/java/frameworks/splunk_otel_java_agent.go +++ b/src/java/frameworks/splunk_otel_java_agent.go @@ -42,6 +42,12 @@ func (s *SplunkOtelJavaAgentFramework) Detect() (string, error) { return "Splunk OTEL", nil } + // An explicit Elastic selection takes precedence over generic OTLP configuration. + if os.Getenv("ELASTIC_OTEL_AGENT") != "" { + s.context.Log.Debug("Splunk OTEL Java agent framework skipped because ELASTIC_OTEL_AGENT is set") + return "", nil + } + // Check for OTEL_EXPORTER_OTLP_ENDPOINT if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") != "" { s.context.Log.Debug("Splunk OTEL Java agent framework detected via OTEL_EXPORTER_OTLP_ENDPOINT") @@ -245,4 +251,3 @@ func (s *SplunkOtelJavaAgentFramework) constructJarPath(agentDir string) error { func (s *SplunkOtelJavaAgentFramework) DependencyIdentifier() string { return "splunk-otel-javaagent" } - diff --git a/src/java/frameworks/splunk_otel_java_agent_test.go b/src/java/frameworks/splunk_otel_java_agent_test.go index dcfb951cce..6a8e58340f 100644 --- a/src/java/frameworks/splunk_otel_java_agent_test.go +++ b/src/java/frameworks/splunk_otel_java_agent_test.go @@ -48,6 +48,7 @@ var _ = Describe("SplunkOtelJavaAgent", func() { os.RemoveAll(tmpDir) os.Unsetenv("VCAP_SERVICES") os.Unsetenv("SPLUNK_OTEL_AGENT") + os.Unsetenv("ELASTIC_OTEL_AGENT") os.Unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT") os.Unsetenv("SPLUNK_ACCESS_TOKEN") os.Unsetenv("SPLUNK_REALM") @@ -78,6 +79,15 @@ var _ = Describe("SplunkOtelJavaAgent", func() { Expect(err).NotTo(HaveOccurred()) Expect(name).To(Equal("Splunk OTEL")) }) + + It("does not detect when the Elastic OTel agent is explicitly selected", func() { + os.Setenv("ELASTIC_OTEL_AGENT", "true") + os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://collector:4318") + + name, err := framework.Detect() + Expect(err).NotTo(HaveOccurred()) + Expect(name).To(BeEmpty()) + }) }) Context("with splunk service label", func() {