diff --git a/README.md b/README.md index 6661f44..bb03dbc 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This buildpack will participate if all the following conditions are met The buildpack will do the following for Java applications: -* Contributes the Datadog Java agent to a layer and configures `$JAVA_TOOL_OPTIONS` to use it +* Contributes the Datadog Java agent to a layer and configures `$JAVA_TOOL_OPTIONS` or `$BP_NATIVE_IMAGE_BUILD_ARGUMENTS` to use it The buildpack will do the following for Node.js applications: @@ -22,7 +22,7 @@ The buildpack will do the following for Node.js applications: | Environment Variable | Description | -------------------- | ----------- | `$BP_DATADOG_ENABLED` | whether to contribute the Datadog trace agent -| `$BPL_DATADOG_DISABLED` | whether to disable the Datadog trace agent (Java only!) +| `$BPL_DATADOG_DISABLED` | whether to disable the Datadog trace agent (non native-image Java applications only!) ## Usage diff --git a/buildpack.toml b/buildpack.toml index 31703ae..6ddcd8e 100644 --- a/buildpack.toml +++ b/buildpack.toml @@ -39,7 +39,7 @@ api = "0.7" [[metadata.configurations]] default = "false" - description = "whether to disable the Datadog trace agent (Java only!)" + description = "whether to disable the Datadog trace agent (non native-image Java applications only!)" launch = true name = "BPL_DATADOG_DISABLED" diff --git a/datadog/build.go b/datadog/build.go index 6d682c9..61a6938 100644 --- a/datadog/build.go +++ b/datadog/build.go @@ -35,11 +35,11 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) { return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency cache\n%w", err) } dc.Logger = b.Logger - + cr, err := libpak.NewConfigurationResolver(context.Buildpack, &b.Logger) - if (err != nil) { + if err != nil { return libcnb.BuildResult{}, fmt.Errorf("unable to create configuration resolver\n%w", err) - } + } if _, ok, err := pr.Resolve("datadog-java"); err != nil { return libcnb.BuildResult{}, fmt.Errorf("unable to resolve datadog-java plan entry\n%w", err) @@ -52,10 +52,12 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) { nativeImage := cr.ResolveBool("BP_NATIVE_IMAGE") result.Layers = append(result.Layers, NewJavaAgent(agentDependency, dc, b.Logger, nativeImage)) - h, be := libpak.NewHelperLayer(context.Buildpack, "toggle") - h.Logger = b.Logger - result.Layers = append(result.Layers, h) - result.BOM.Entries = append(result.BOM.Entries, be) + if !nativeImage { + h, be := libpak.NewHelperLayer(context.Buildpack, "toggle") + h.Logger = b.Logger + result.Layers = append(result.Layers, h) + result.BOM.Entries = append(result.BOM.Entries, be) + } } if _, ok, err := pr.Resolve("datadog-nodejs"); err != nil { diff --git a/datadog/build_test.go b/datadog/build_test.go index 3630cf0..2295908 100644 --- a/datadog/build_test.go +++ b/datadog/build_test.go @@ -24,7 +24,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { ctx libcnb.BuildContext ) - it("contributes Java agent API <= 0.6", func() { + it("contributes Java agent API <= 0.6 and toggle", func() { ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "datadog-java"}) ctx.Buildpack.Metadata = map[string]interface{}{ "dependencies": []map[string]interface{}{ @@ -46,7 +46,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { Expect(result.Layers[1].Name()).To(Equal("helper")) }) - it("contributes Java agent API >= 0.7", func() { + it("contributes Java agent API >= 0.7 and toggle", func() { ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "datadog-java"}) ctx.Buildpack.Metadata = map[string]interface{}{ "dependencies": []map[string]interface{}{ @@ -70,6 +70,35 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { Expect(result.Layers[1].Name()).To(Equal("helper")) }) + context("using Native Image", func() { + it.Before(func() { + t.Setenv("BP_NATIVE_IMAGE", "true") + }) + + it("contributes Java agent API >= 0.7 only", func() { + ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "datadog-java"}) + ctx.Buildpack.Metadata = map[string]interface{}{ + "dependencies": []map[string]interface{}{ + { + "id": "datadog-agent-java", + "version": "1.1.1", + "stacks": []interface{}{"test-stack-id"}, + "cpes": []interface{}{"cpe:2.3:a:datadog-agent:java-agent:1.1.1:*:*:*:*:*:*:*"}, + "purl": "pkg:generic/datadog-agent-java-agent@1.1.1?arch=amd64", + }, + }, + } + ctx.Buildpack.API = "0.7" + ctx.StackID = "test-stack-id" + + result, err := datadog.Build{}.Build(ctx) + Expect(err).NotTo(HaveOccurred()) + + Expect(result.Layers).To(HaveLen(1)) + Expect(result.Layers[0].Name()).To(Equal("datadog-agent-java")) + }) + }) + it("contributes NodeJS agent API <= 0.6", func() { ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "datadog-nodejs"}) ctx.Buildpack.Metadata = map[string]interface{}{ diff --git a/datadog/detect.go b/datadog/detect.go index 71440bc..effe8c4 100644 --- a/datadog/detect.go +++ b/datadog/detect.go @@ -15,7 +15,7 @@ import ( "github.com/paketo-buildpacks/libpak/bard" ) -type Detect struct{ +type Detect struct { Logger bard.Logger } diff --git a/datadog/detect_test.go b/datadog/detect_test.go index a1f2ff0..b330e85 100644 --- a/datadog/detect_test.go +++ b/datadog/detect_test.go @@ -99,6 +99,5 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { }, })) }) - }) } diff --git a/datadog/java_agent.go b/datadog/java_agent.go index ad3f8a8..25a11c6 100644 --- a/datadog/java_agent.go +++ b/datadog/java_agent.go @@ -26,7 +26,7 @@ type JavaAgent struct { func NewJavaAgent(dependency libpak.BuildpackDependency, cache libpak.DependencyCache, logger bard.Logger, nativeImage bool) JavaAgent { contrib, _ := libpak.NewDependencyLayer(dependency, cache, libcnb.LayerTypes{ - Build: nativeImage, + Build: nativeImage, Launch: true, }) return JavaAgent{LayerContributor: contrib, Logger: logger, NativeImage: nativeImage} @@ -43,7 +43,7 @@ func (j JavaAgent) Contribute(layer libcnb.Layer) (libcnb.Layer, error) { return libcnb.Layer{}, fmt.Errorf("unable to copy artifact to %s\n%w", file, err) } - if (j.NativeImage) { + if j.NativeImage { layer.BuildEnvironment.Appendf("BP_NATIVE_IMAGE_BUILD_ARGUMENTS", " ", "-J-javaagent:%s", file) } layer.LaunchEnvironment.Default("BPI_DATADOG_AGENT_PATH", file)