Skip to content

Commit ce37f5e

Browse files
author
Christopher Frost
committed
Provide extra configuration to New Relic
This commit allows additional configuration values to be specified on New Relic through the Credentials payload of the bound New Relic service. This also includes the ability to provide an application name in place of the default provided by Cloud Foundry. All configuration names follow those defined by New Relic. [#92119348] [resolves cloudfoundry#173]
1 parent 7a538fb commit ce37f5e

3 files changed

Lines changed: 48 additions & 19 deletions

File tree

docs/framework-new_relic_agent.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ The credential payload of the service may contain the following entries:
2323

2424
| Name | Description
2525
| ---- | -----------
26-
| `licenseKey` | The license key to use when authenticating
26+
| `license_key` | (Optional) Either this credential or `licenseKey` must be provided. If both are provided then the value for `license_key` will always win. The license key to use when authenticating.
27+
| `licenseKey` | (Optional) As above.
28+
| `***` | (Optional) Any additional entries will be applied as a system property appended to `-Dnewrelic.config.` to allow full configuration of the agent.
2729

2830
## Configuration
2931
For general information on configuring the buildpack, refer to [Configuration and Extension][].

lib/java_buildpack/framework/new_relic_agent.rb

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,58 @@ class NewRelicAgent < JavaBuildpack::Component::VersionedDependencyComponent
2626

2727
# (see JavaBuildpack::Component::BaseComponent#compile)
2828
def compile
29-
FileUtils.mkdir_p logs_dir
3029
download_jar
3130
@droplet.copy_resources
3231
end
3332

3433
# (see JavaBuildpack::Component::BaseComponent#release)
3534
def release
36-
@droplet.java_opts
37-
.add_javaagent(@droplet.sandbox + jar_name)
38-
.add_system_property('newrelic.home', @droplet.sandbox)
39-
.add_system_property('newrelic.config.license_key', license_key)
40-
.add_system_property('newrelic.config.app_name', "#{application_name}")
41-
.add_system_property('newrelic.config.log_file_path', logs_dir)
42-
@droplet.java_opts.add_system_property('newrelic.enable.java.8', 'true') if @droplet.java_home.java_8_or_later?
35+
credentials = @application.services.find_service(FILTER)['credentials']
36+
java_opts = @droplet.java_opts
37+
configuration = {}
38+
39+
apply_configuration(credentials, configuration)
40+
apply_user_configuration(credentials, configuration)
41+
write_java_opts(java_opts, configuration)
42+
43+
java_opts.add_javaagent(@droplet.sandbox + jar_name)
44+
.add_system_property('newrelic.home', @droplet.sandbox)
45+
java_opts.add_system_property('newrelic.enable.java.8', 'true') if @droplet.java_home.java_8_or_later?
4346
end
4447

4548
protected
4649

4750
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
4851
def supports?
49-
@application.services.one_service? FILTER, 'licenseKey'
52+
@application.services.one_service? FILTER, [LICENSE_KEY, LICENSE_KEY_USER]
5053
end
5154

5255
private
5356

5457
FILTER = /newrelic/.freeze
5558

56-
private_constant :FILTER
59+
LICENSE_KEY = 'licenseKey'.freeze
60+
61+
LICENSE_KEY_USER = 'license_key'.freeze
62+
63+
private_constant :FILTER, :LICENSE_KEY, :LICENSE_KEY_USER
5764

58-
def application_name
59-
@application.details['application_name']
65+
def apply_configuration(credentials, configuration)
66+
configuration['log_file_name'] = 'STDOUT'
67+
configuration[LICENSE_KEY_USER] = credentials[LICENSE_KEY]
68+
configuration['app_name'] = @application.details['application_name']
6069
end
6170

62-
def license_key
63-
@application.services.find_service(FILTER)['credentials']['licenseKey']
71+
def apply_user_configuration(credentials, configuration)
72+
credentials.each do |key, value|
73+
configuration[key] = value
74+
end
6475
end
6576

66-
def logs_dir
67-
@droplet.sandbox + 'logs'
77+
def write_java_opts(java_opts, configuration)
78+
configuration.each do |key, value|
79+
java_opts.add_system_property("newrelic.config.#{key}", value)
80+
end
6881
end
6982

7083
end

spec/java_buildpack/framework/new_relic_agent_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
context do
3030

3131
before do
32-
allow(services).to receive(:one_service?).with(/newrelic/, 'licenseKey').and_return(true)
32+
allow(services).to receive(:one_service?).with(/newrelic/, %w(licenseKey license_key)).and_return(true)
3333
end
3434

3535
it 'detects with newrelic-n/a service' do
@@ -62,7 +62,21 @@
6262
expect(java_opts).to include('-Dnewrelic.home=$PWD/.java-buildpack/new_relic_agent')
6363
expect(java_opts).to include('-Dnewrelic.config.license_key=test-license-key')
6464
expect(java_opts).to include('-Dnewrelic.config.app_name=test-application-name')
65-
expect(java_opts).to include('-Dnewrelic.config.log_file_path=$PWD/.java-buildpack/new_relic_agent/logs')
65+
expect(java_opts).to include('-Dnewrelic.config.log_file_name=STDOUT')
66+
end
67+
68+
it 'updates JAVA_OPTS with additional options' do
69+
allow(services).to receive(:find_service).and_return('credentials' => { 'licenseKey' => 'test-license-key',
70+
'license_key' => 'different-license-key',
71+
'app_name' => 'different-name',
72+
'foo' => 'bar' })
73+
allow(java_home).to receive(:java_8_or_later?).and_return(JavaBuildpack::Util::TokenizedVersion.new('1.7.0_u10'))
74+
75+
component.release
76+
77+
expect(java_opts).to include('-Dnewrelic.config.license_key=different-license-key')
78+
expect(java_opts).to include('-Dnewrelic.config.app_name=different-name')
79+
expect(java_opts).to include('-Dnewrelic.config.foo=bar')
6680
end
6781

6882
it 'updates JAVA_OPTS on Java 8' do

0 commit comments

Comments
 (0)