Skip to content

Commit 2d2634b

Browse files
author
Daniel Mikusa
authored
Merge pull request #867 from tylerbenson/datadog-javaagent
Add support for Datadog APM Javaagent
2 parents 623c819 + b241ffa commit 2d2634b

File tree

6 files changed

+190
-0
lines changed

6 files changed

+190
-0
lines changed

config/components.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ frameworks:
4949
- "JavaBuildpack::Framework::ContainerCustomizer"
5050
- "JavaBuildpack::Framework::ContainerSecurityProvider"
5151
- "JavaBuildpack::Framework::ContrastSecurityAgent"
52+
- "JavaBuildpack::Framework::DatadogJavaagent"
5253
- "JavaBuildpack::Framework::Debug"
5354
- "JavaBuildpack::Framework::DynatraceAppmonAgent"
5455
- "JavaBuildpack::Framework::DynatraceOneAgent"

config/datadog_javaagent.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2013-2021 the original author or authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Configuration for the Datadog APM Javaagent
17+
---
18+
version: +
19+
repository_root: https://raw.githubusercontent.com/datadog/dd-trace-java/cloudfoundry/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Datadog APM Javaagent Framework
2+
The [Datadog APM]() Javaagent Framework allows your application to be dynamically instrumented [by][datadog-javaagent] `dd-java-agent.jar`.
3+
4+
<table>
5+
<tr>
6+
<td><strong>Detection Criterion</strong></td><td>One of the following environment variables configured:
7+
<ul>
8+
<li><code>DD_APM_ENABLED</code> configured as <code>true</code></li>
9+
<li><code>DD_API_KEY</code> defined with the assumption of a <a href='https://github.com/DataDog/datadog-cloudfoundry-buildpack'>datadog-cloudfoundry-buildpack</a> configured, and <code>DD_APM_ENABLED</code> not <code>false</code></li>
10+
</ul>
11+
</td>
12+
</tr>
13+
<tr>
14+
<td><strong>Tags</strong></td>
15+
<td><tt>datadog-javaagent=&lt;version&gt;</tt></td>
16+
</tr>
17+
</table>
18+
19+
Tags are printed to standard output by the buildpack detect script
20+
21+
## Configuration
22+
For general information on configuring the buildpack, including how to specify configuration values through environment variables, refer to [Configuration and Extension][].
23+
The framework uses the [`Repository` utility support][repositories] and so it supports the [version syntax][] defined there.
24+
25+
The javaagent can be configured directly via environment variables or system properties as defined in the [Configuration of Datadog Javaagent][] documentation.
26+
27+
28+
| Name | Description
29+
| ---- | -----------
30+
| `repository_root` | The URL of the Datadog Javaagent repository index ([details][repositories]).
31+
| `version` | The `dd-java-agent` version to use. Candidate versions can be found in [this listing][].
32+
33+
34+
[Configuration and Extension]: ../README.md#configuration-and-extension
35+
[Datadog APM]: https://www.datadoghq.com/product/apm/
36+
[datadog-javaagent]: https://github.com/datadog/dd-trace-java
37+
[Configuration of Datadog Javaagent]: https://docs.datadoghq.com/tracing/setup_overview/setup/java/#configuration
38+
[this listing]: https://raw.githubusercontent.com/datadog/dd-trace-java/cloudfoundry/index.yml
39+
[repositories]: extending-repositories.md
40+
[version syntax]: extending-repositories.md#version-syntax-and-ordering
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
# Cloud Foundry Java Buildpack
4+
# Copyright 2013-2021 the original author or authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'java_buildpack/component/versioned_dependency_component'
19+
require 'java_buildpack/framework'
20+
21+
module JavaBuildpack
22+
module Framework
23+
24+
# Encapsulates the functionality for enabling zero-touch Elastic APM support.
25+
class DatadogJavaagent < JavaBuildpack::Component::VersionedDependencyComponent
26+
include JavaBuildpack::Util
27+
28+
# (see JavaBuildpack::Component::BaseComponent#compile)
29+
def compile
30+
download_jar
31+
end
32+
33+
# (see JavaBuildpack::Component::BaseComponent#release)
34+
def release
35+
java_opts = @droplet.java_opts
36+
java_opts.add_javaagent(@droplet.sandbox + jar_name)
37+
38+
if !@application.environment.key?('DD_SERVICE')
39+
java_opts.add_system_property('dd.service', @application.details['application_name'])
40+
end
41+
42+
if @application.details['application_version']
43+
java_opts.add_system_property('dd.version', @application.details['application_version'])
44+
end
45+
end
46+
47+
protected
48+
49+
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
50+
def supports?
51+
api_key_defined = @application.environment.key?('DD_API_KEY') && !@application.environment['DD_API_KEY'].empty?
52+
apm_disabled = @application.environment['DD_APM_ENABLED'] == 'false'
53+
apm_enabled = @application.environment['DD_APM_ENABLED'] == 'true'
54+
(api_key_defined && !apm_disabled) || apm_enabled
55+
end
56+
end
57+
end
58+
end

rakelib/versions_task.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def initialize
6060
'container_customizer' => 'Spring Boot Container Customizer',
6161
'container_security_provider' => 'Container Security Provider',
6262
'contrast_security_agent' => 'Contrast Security Agent',
63+
'datadog_javaagent' => 'Datadog APM Javaagent',
6364
'dynatrace_appmon_agent' => 'Dynatrace Appmon Agent',
6465
'dynatrace_one_agent' => 'Dynatrace OneAgent',
6566
'elastic_apm_agent' => 'Elastic APM Agent',
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# frozen_string_literal: true
2+
3+
# Cloud Foundry Java Buildpack
4+
# Copyright 2013-2021 the original author or authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'spec_helper'
19+
require 'component_helper'
20+
require 'java_buildpack/framework/datadog_javaagent'
21+
require 'java_buildpack/util/tokenized_version'
22+
23+
describe JavaBuildpack::Framework::DatadogJavaagent do
24+
include_context 'with component help'
25+
26+
describe '#detect' do
27+
subject(:detect) { component.detect }
28+
29+
it 'does not detect without an api key' do
30+
expect(detect).to be nil
31+
end
32+
33+
context 'when api key is empty' do
34+
let(:environment) { { 'DD_API_KEY' => '' } }
35+
36+
it { is_expected.to be nil }
37+
end
38+
39+
context 'when apm is disabled' do
40+
let(:environment) { { 'DD_API_KEY' => 'foo', 'DD_APM_ENABLED' => 'false' } }
41+
42+
it { is_expected.to be nil }
43+
end
44+
45+
context 'when apm is enabled with no api key' do
46+
let(:environment) { { 'DD_APM_ENABLED' => 'true' } }
47+
48+
it { is_expected.to eq("datadog-javaagent=#{version}") }
49+
end
50+
51+
context 'when apm key is provided' do
52+
let(:environment) { { 'DD_API_KEY' => 'foo' } }
53+
54+
it { is_expected.to eq("datadog-javaagent=#{version}") }
55+
end
56+
end
57+
58+
context 'when apm key is provided' do
59+
let(:environment) { { 'DD_API_KEY' => 'foo' } }
60+
61+
it 'compile downloads datadog-javaagent JAR', cache_fixture: 'stub-datadog-javaagent.jar' do
62+
component.compile
63+
expect(sandbox + "datadog-javaagent-#{version}.jar").to exist
64+
end
65+
66+
it 'release updates JAVA_OPTS' do
67+
component.release
68+
expect(java_opts).to include("-javaagent:$PWD/.java-buildpack/datadog_javaagent/datadog_javaagent-#{version}.jar")
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)