Skip to content

Commit 5254a19

Browse files
author
Daniel Mikusa
authored
Fix class count discrepancy when using DataDog agent. (#890)
Resolves #887. This fix will go through the DataDog agent file and count the number of `.classdata` hidden classes. It then creates a shadow JAR file with the same number of fake classes in it. The Java buildpack's normal class counting mechanism will now see these classes and pick a correct class count.
1 parent 3a824b2 commit 5254a19

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

lib/java_buildpack/framework/datadog_javaagent.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ class DatadogJavaagent < JavaBuildpack::Component::VersionedDependencyComponent
2727

2828
def initialize(context)
2929
super(context)
30-
@datadog_buildpack = File.exist? File.join(@droplet.root, 'datadog')
30+
@datadog_buildpack = File.exist? File.join(@droplet.root, '.datadog')
3131
@logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger DatadogJavaagent
3232
end
3333

3434
# (see JavaBuildpack::Component::BaseComponent#compile)
3535
def compile
3636
@logger.error 'Datadog Buildpack is required, but not found' unless @datadog_buildpack
3737

38-
download_jar if @datadog_buildpack
38+
return unless @datadog_buildpack
39+
40+
download_jar
41+
fix_class_count
3942
end
4043

4144
# (see JavaBuildpack::Component::BaseComponent#release)
@@ -63,6 +66,31 @@ def supports?
6366
apm_disabled = @application.environment['DD_APM_ENABLED'] == 'false'
6467
(api_key_defined && !apm_disabled)
6568
end
69+
70+
# fixes issue where some classes are not counted by adding shadow class files
71+
def fix_class_count
72+
cnt = classdata_count(@droplet.sandbox + jar_name)
73+
zipdir = "#{@droplet.sandbox}/datadog_fakeclasses"
74+
zipfile = "#{@droplet.sandbox}/datadog_fakeclasses.jar"
75+
76+
File.delete(zipfile) if File.exist? zipfile
77+
FileUtils.rm_rf(zipdir)
78+
FileUtils.mkdir_p(zipdir)
79+
80+
1.upto(cnt) do |i|
81+
File.open("#{zipdir}/#{i}.class", 'w') do |f|
82+
f.write(i.to_s)
83+
end
84+
end
85+
86+
`cd #{zipdir} && zip -r #{zipfile} .`
87+
FileUtils.rm_rf(zipdir)
88+
end
89+
90+
# count hidden class files in the agent JAR
91+
def classdata_count(archive)
92+
`unzip -l #{archive} | grep '\\(\\.classdata\\)$' | wc -l`.to_i
93+
end
6694
end
6795
end
6896
end
13.9 KB
Binary file not shown.

spec/java_buildpack/framework/datadog_javaagent_spec.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,28 @@
6767

6868
context 'when datadog buildpack is present' do
6969
before do
70-
FileUtils.mkdir_p File.join(context[:droplet].root, 'datadog')
70+
FileUtils.mkdir_p File.join(context[:droplet].root, '.datadog')
7171
end
7272

7373
after do
74-
FileUtils.rmdir File.join(context[:droplet].root, 'datadog')
74+
FileUtils.rmdir File.join(context[:droplet].root, '.datadog')
7575
end
7676

7777
it 'compile downloads datadog-javaagent JAR', cache_fixture: 'stub-datadog-javaagent.jar' do
7878
component.compile
7979
expect(sandbox + "datadog_javaagent-#{version}.jar").to exist
8080
end
8181

82+
it 'makes a jar with fake class files', cache_fixture: 'stub-datadog-javaagent.jar' do
83+
component.compile
84+
expect(sandbox + "datadog_javaagent-#{version}.jar").to exist
85+
expect(sandbox + 'datadog_fakeclasses.jar').to exist
86+
expect(sandbox + 'datadog_fakeclasses').not_to exist
87+
88+
cnt = `unzip -l #{sandbox}/datadog_fakeclasses.jar | grep '\\(\\.class\\)$' | wc -l`.to_i
89+
expect(cnt).to equal(34)
90+
end
91+
8292
it 'release updates JAVA_OPTS' do
8393
component.release
8494

@@ -114,11 +124,11 @@
114124
end
115125

116126
before do
117-
FileUtils.mkdir_p File.join(context[:droplet].root, 'datadog')
127+
FileUtils.mkdir_p File.join(context[:droplet].root, '.datadog')
118128
end
119129

120130
after do
121-
FileUtils.rmdir File.join(context[:droplet].root, 'datadog')
131+
FileUtils.rmdir File.join(context[:droplet].root, '.datadog')
122132
end
123133

124134
it 'release updates JAVA_OPTS with env variable version' do

0 commit comments

Comments
 (0)