Skip to content

Commit 7c68c04

Browse files
committed
Merge branch '526-dynatrace-skip-errors'
2 parents fd3fed2 + c3023e9 commit 7c68c04

3 files changed

Lines changed: 92 additions & 12 deletions

File tree

docs/framework-dynatrace_one_agent.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The credential payload of the service may contain the following entries:
2828
| `apitoken` | The token for integrating your Dynatrace environment with Cloud Foundry. You can find it in the deploy Dynatrace section within your environment.
2929
| `apiurl` | (Optional) The base URL of the Dynatrace API. If you are using Dynatrace Managed you will need to set this property to `https://<your-managed-server-url>/e/<environmentId>/api`. If you are using Dynatrace SaaS you don't need to set this property.
3030
| `environmentid` | Your Dynatrace environment ID is the unique identifier of your Dynatrace environment. You can find it in the deploy Dynatrace section within your environment.
31+
| `skiperrors` | (Optional) The errors during agent download are skipped and the injection is disabled. Use this option at your own risk. Possible values are 'true' and 'false'. This option is disabled by default!
3132

3233
## Configuration
3334
For general information on configuring the buildpack, including how to specify configuration values through environment variables, refer to [Configuration and Extension][].

lib/java_buildpack/framework/dynatrace_one_agent.rb

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'java_buildpack/component/versioned_dependency_component'
1818
require 'java_buildpack/framework'
1919
require 'java_buildpack/util/cache/internet_availability'
20+
require 'java_buildpack/util/to_b'
2021
require 'json'
2122

2223
module JavaBuildpack
@@ -31,6 +32,7 @@ class DynatraceOneAgent < JavaBuildpack::Component::VersionedDependencyComponent
3132
def initialize(context)
3233
super(context)
3334
@version, @uri = agent_download_url if supports?
35+
@logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger DynatraceOneAgent
3436
end
3537

3638
# (see JavaBuildpack::Component::BaseComponent#compile)
@@ -42,23 +44,27 @@ def compile
4244
end
4345

4446
@droplet.copy_resources
47+
rescue StandardError => e
48+
raise unless skip_errors?
49+
50+
@logger.error { "Dynatrace OneAgent download failed: #{e}" }
51+
@logger.warn { "Agent injection disabled because of #{SKIP_ERRORS} credential is set to true!" }
52+
FileUtils.mkdir_p(error_file.parent)
53+
File.write(error_file, e.to_s)
4554
end
4655

4756
# (see JavaBuildpack::Component::BaseComponent#release)
4857
def release
49-
credentials = @application.services.find_service(FILTER, APITOKEN, ENVIRONMENTID)['credentials']
50-
environment_variables = @droplet.environment_variables
51-
manifest = agent_manifest
58+
if error_file.exist?
59+
@logger.warn { "Dynatrace OneAgent injection disabled due to download error: #{File.read(error_file)}" }
60+
return
61+
end
5262

53-
@droplet.java_opts.add_agentpath(agent_path(manifest))
63+
manifest = agent_manifest
5464

55-
environment_variables
56-
.add_environment_variable(DT_TENANT, credentials[ENVIRONMENTID])
57-
.add_environment_variable(DT_TENANTTOKEN, tenanttoken(manifest))
58-
.add_environment_variable(DT_CONNECTION_POINT, endpoints(manifest))
65+
@droplet.java_opts.add_agentpath(agent_path(manifest))
5966

60-
environment_variables.add_environment_variable(DT_APPLICATION_ID, application_id) unless application_id?
61-
environment_variables.add_environment_variable(DT_HOST_ID, host_id) unless host_id?
67+
dynatrace_environment_variables(manifest)
6268
end
6369

6470
protected
@@ -88,11 +94,12 @@ def supports?
8894

8995
FILTER = /dynatrace/
9096

97+
SKIP_ERRORS = 'skiperrors'.freeze
98+
9199
private_constant :APIURL, :APITOKEN, :DT_APPLICATION_ID, :DT_CONNECTION_POINT, :DT_HOST_ID, :DT_TENANT,
92-
:DT_TENANTTOKEN, :ENVIRONMENTID, :FILTER
100+
:DT_TENANTTOKEN, :ENVIRONMENTID, :FILTER, :SKIP_ERRORS
93101

94102
def agent_download_url
95-
credentials = @application.services.find_service(FILTER, APITOKEN, ENVIRONMENTID)['credentials']
96103
download_uri = "#{api_base_url(credentials)}/v1/deployment/installer/agent/unix/paas/latest?include=java" \
97104
"&bitness=64&Api-Token=#{credentials[APITOKEN]}"
98105
['latest', download_uri]
@@ -121,10 +128,30 @@ def application_id?
121128
@application.environment.key?(DT_APPLICATION_ID)
122129
end
123130

131+
def credentials
132+
@application.services.find_service(FILTER, APITOKEN, ENVIRONMENTID)['credentials']
133+
end
134+
135+
def dynatrace_environment_variables(manifest)
136+
environment_variables = @droplet.environment_variables
137+
138+
environment_variables
139+
.add_environment_variable(DT_TENANT, credentials[ENVIRONMENTID])
140+
.add_environment_variable(DT_TENANTTOKEN, tenanttoken(manifest))
141+
.add_environment_variable(DT_CONNECTION_POINT, endpoints(manifest))
142+
143+
environment_variables.add_environment_variable(DT_APPLICATION_ID, application_id) unless application_id?
144+
environment_variables.add_environment_variable(DT_HOST_ID, host_id) unless host_id?
145+
end
146+
124147
def endpoints(manifest)
125148
"\"#{manifest['communicationEndpoints'].join(';')}\""
126149
end
127150

151+
def error_file
152+
@droplet.sandbox + 'dynatrace_download_error'
153+
end
154+
128155
def expand(file)
129156
with_timing "Expanding Dynatrace OneAgent to #{@droplet.sandbox.relative_path_from(@droplet.root)}" do
130157
Dir.mktmpdir do |root|
@@ -143,6 +170,10 @@ def host_id?
143170
@application.environment.key?(DT_HOST_ID)
144171
end
145172

173+
def skip_errors?
174+
credentials[SKIP_ERRORS].to_b
175+
end
176+
146177
def tenanttoken(manifest)
147178
manifest['tenantToken']
148179
end

spec/java_buildpack/framework/dynatrace_one_agent_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,54 @@
9292

9393
end
9494

95+
context do
96+
97+
before do
98+
allow(services).to receive(:one_service?).with(/dynatrace/, 'apitoken', 'environmentid').and_return(true)
99+
allow(services).to receive(:find_service).and_return('credentials' => { 'environmentid' => 'test-environmentid',
100+
'apiurl' => 'test-apiurl',
101+
'apitoken' => 'test-apitoken' })
102+
allow(application_cache).to receive(:get)
103+
.with('test-apiurl/v1/deployment/installer/agent/unix/paas/latest?include=java&bitness=64' \
104+
'&Api-Token=test-apitoken')
105+
.and_raise(RuntimeError.new('service interrupt'))
106+
end
107+
108+
it 'fails on download error on default' do
109+
expect { component.compile }.to raise_error(RuntimeError)
110+
end
111+
112+
end
113+
114+
context do
115+
116+
before do
117+
allow(services).to receive(:one_service?).with(/dynatrace/, 'apitoken', 'environmentid').and_return(true)
118+
allow(services).to receive(:find_service).and_return('credentials' => { 'environmentid' => 'test-environmentid',
119+
'apiurl' => 'test-apiurl',
120+
'apitoken' => 'test-apitoken',
121+
'skiperrors' => 'true' })
122+
allow(application_cache).to receive(:get)
123+
.with('test-apiurl/v1/deployment/installer/agent/unix/paas/latest?include=java&bitness=64' \
124+
'&Api-Token=test-apitoken')
125+
.and_raise(RuntimeError.new('service interrupt'))
126+
end
127+
128+
it 'skips errors during compile and writes error file' do
129+
component.compile
130+
expect(sandbox + 'dynatrace_download_error').to exist
131+
end
132+
133+
it 'does not do anything during release' do
134+
component.compile
135+
component.release
136+
137+
expect(java_opts).not_to include('-agentpath:$PWD/.java-buildpack/dynatrace_one_agent/agent/lib64/' \
138+
'liboneagentloader.so')
139+
end
140+
141+
end
142+
95143
end
96144

97145
end

0 commit comments

Comments
 (0)