Skip to content

Commit

Permalink
Merge pull request #259 from DataDog/bbujon/fix-native-image-toggle
Browse files Browse the repository at this point in the history
Fix launch toggle for Native Image Java applications
  • Loading branch information
dmikusa authored Dec 7, 2023
2 parents 6ef138b + 239a0cc commit 2e4c590
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
16 changes: 9 additions & 7 deletions datadog/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
33 changes: 31 additions & 2 deletions datadog/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand All @@ -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{}{
Expand All @@ -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/[email protected]?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{}{
Expand Down
2 changes: 1 addition & 1 deletion datadog/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/paketo-buildpacks/libpak/bard"
)

type Detect struct{
type Detect struct {
Logger bard.Logger
}

Expand Down
1 change: 0 additions & 1 deletion datadog/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,5 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
}))
})

})
}
4 changes: 2 additions & 2 deletions datadog/java_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)
Expand Down

0 comments on commit 2e4c590

Please sign in to comment.