From 22bedb8a816b043ec50db7d09352b45f4a3502bf Mon Sep 17 00:00:00 2001 From: Bruce Bujon Date: Fri, 24 Nov 2023 13:36:25 +0100 Subject: [PATCH 1/4] chore: Update documentation about native image --- README.md | 4 ++-- buildpack.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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" From 639c43ee45473bb890b0eb7bc896a5bb46c11acc Mon Sep 17 00:00:00 2001 From: Bruce Bujon Date: Fri, 24 Nov 2023 13:38:21 +0100 Subject: [PATCH 2/4] fix: Disable toggle for native applications --- datadog/build.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/datadog/build.go b/datadog/build.go index 6d682c9..e438c8d 100644 --- a/datadog/build.go +++ b/datadog/build.go @@ -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 { From 78b87c1c6950c5bb963c03eff04aa5bf946b41a8 Mon Sep 17 00:00:00 2001 From: Bruce Bujon Date: Tue, 5 Dec 2023 09:45:58 +0100 Subject: [PATCH 3/4] =?UTF-8?q?chore:=E2=80=AFApply=20go=20fmt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datadog/build.go | 8 ++++---- datadog/detect.go | 2 +- datadog/detect_test.go | 1 - datadog/java_agent.go | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/datadog/build.go b/datadog/build.go index e438c8d..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,7 +52,7 @@ 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)) - if (!nativeImage) { + if !nativeImage { h, be := libpak.NewHelperLayer(context.Buildpack, "toggle") h.Logger = b.Logger result.Layers = append(result.Layers, h) 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) From 239a0cce148bc5c42adf7a8a5d389c90fa1ae5e0 Mon Sep 17 00:00:00 2001 From: Bruce Bujon Date: Tue, 5 Dec 2023 09:52:41 +0100 Subject: [PATCH 4/4] feat: Add test to ensure toggle layer not added when using Native Image --- datadog/build_test.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) 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{}{