Skip to content

Commit

Permalink
add setting for allocation tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jul 10, 2024
1 parent f93cd2a commit e6b3302
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 11 deletions.
46 changes: 37 additions & 9 deletions lib/datadog/ci/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,7 @@ def activate_ci!(settings)
settings.tracing.test_mode.writer_options = trace_writer_options

# @type ivar @test_optimisation: Datadog::CI::TestOptimisation::Component
@test_optimisation = TestOptimisation::Component.new(
api: test_visibility_api,
dd_env: settings.env,
config_tags: custom_configuration(settings),
coverage_writer: build_coverage_writer(settings, test_visibility_api),
enabled: settings.ci.enabled && settings.ci.itr_enabled,
bundle_location: settings.ci.itr_code_coverage_excluded_bundle_path,
use_single_threaded_coverage: settings.ci.itr_code_coverage_use_single_threaded_mode
)
@test_optimisation = build_test_optimisation(settings, test_visibility_api)

@test_visibility = TestVisibility::Component.new(
test_optimisation: @test_optimisation,
Expand All @@ -118,6 +110,42 @@ def activate_ci!(settings)
)
end

def build_test_optimisation(settings, test_visibility_api)
if settings.ci.itr_code_coverage_use_single_threaded_mode &&
settings.ci.itr_test_impact_analysis_use_allocation_tracing
Datadog.logger.warn(
"Intelligent test runner: Single threaded coverage mode is incompatible with allocation tracing. " \
"Allocation tracing will be disabled. It means that test impact analysis will not be able to detect " \
"instantiations of objects in your code, which is important for ActiveRecord models. " \
"Please add your app/model folder to the list of tracked files or disable single threaded coverage mode."
)

settings.ci.itr_test_impact_analysis_use_allocation_tracing = false
end

if RUBY_VERSION.start_with?("3.2.") && RUBY_VERSION < "3.2.3" &&
settings.ci.itr_test_impact_analysis_use_allocation_tracing
Datadog.logger.warn(
"Intelligent test runner: Allocation tracing is not supported in Ruby versions 3.2.0, 3.2.1 and 3.2.2 and will be forcibly " \
"disabled. This is due to a VM bug that can lead to crashes (https://bugs.ruby-lang.org/issues/19482). " \
"Please update your Ruby version or add your app/model folder to the list of tracked files." \
"Set env variable DD_CIVISIBILITY_ITR_TEST_IMPACT_ANALYSIS_USE_ALLOCATION_TRACING to 0 to disable this warning."
)
settings.ci.itr_test_impact_analysis_use_allocation_tracing = false
end

TestOptimisation::Component.new(
api: test_visibility_api,
dd_env: settings.env,
config_tags: custom_configuration(settings),
coverage_writer: build_coverage_writer(settings, test_visibility_api),
enabled: settings.ci.enabled && settings.ci.itr_enabled,
bundle_location: settings.ci.itr_code_coverage_excluded_bundle_path,
use_single_threaded_coverage: settings.ci.itr_code_coverage_use_single_threaded_mode,
use_allocation_tracing: settings.ci.itr_test_impact_analysis_use_allocation_tracing
)
end

def build_test_visibility_api(settings)
if settings.ci.agentless_mode_enabled
check_dd_site(settings)
Expand Down
6 changes: 6 additions & 0 deletions lib/datadog/ci/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def self.add_settings!(base)
o.default false
end

option :itr_test_impact_analysis_use_allocation_tracing do |o|
o.type :bool
o.env CI::Ext::Settings::ENV_ITR_TEST_IMPACT_ANALYSIS_USE_ALLOCATION_TRACING
o.default true
end

define_method(:instrument) do |integration_name, options = {}, &block|
return unless enabled

Expand Down
1 change: 1 addition & 0 deletions lib/datadog/ci/ext/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Settings
ENV_GIT_METADATA_UPLOAD_ENABLED = "DD_CIVISIBILITY_GIT_METADATA_UPLOAD_ENABLED"
ENV_ITR_CODE_COVERAGE_EXCLUDED_BUNDLE_PATH = "DD_CIVISIBILITY_ITR_CODE_COVERAGE_EXCLUDED_BUNDLE_PATH"
ENV_ITR_CODE_COVERAGE_USE_SINGLE_THREADED_MODE = "DD_CIVISIBILITY_ITR_CODE_COVERAGE_USE_SINGLE_THREADED_MODE"
ENV_ITR_TEST_IMPACT_ANALYSIS_USE_ALLOCATION_TRACING = "DD_CIVISIBILITY_ITR_TEST_IMPACT_ANALYSIS_USE_ALLOCATION_TRACING"

# Source: https://docs.datadoghq.com/getting_started/site/
DD_SITE_ALLOWLIST = %w[
Expand Down
4 changes: 3 additions & 1 deletion lib/datadog/ci/test_optimisation/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def initialize(
coverage_writer: nil,
enabled: false,
bundle_location: nil,
use_single_threaded_coverage: false
use_single_threaded_coverage: false,
use_allocation_tracing: true
)
@enabled = enabled
@api = api
Expand All @@ -45,6 +46,7 @@ def initialize(
bundle_location
end
@use_single_threaded_coverage = use_single_threaded_coverage
@use_allocation_tracing = use_allocation_tracing

@test_skipping_enabled = false
@code_coverage_enabled = false
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/configuration/components.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Datadog

def activate_ci!: (untyped settings) -> untyped

def build_test_optimisation: (untyped settings, Datadog::CI::Transport::Api::Base? api) -> Datadog::CI::TestOptimisation::Component

def build_test_visibility_api: (untyped settings) -> Datadog::CI::Transport::Api::Base?

def serializers_factory: (untyped settings) -> (singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestSuiteLevel) | singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel))
Expand Down
1 change: 1 addition & 0 deletions sig/datadog/ci/ext/settings.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Datadog
ENV_GIT_METADATA_UPLOAD_ENABLED: String
ENV_ITR_CODE_COVERAGE_EXCLUDED_BUNDLE_PATH: String
ENV_ITR_CODE_COVERAGE_USE_SINGLE_THREADED_MODE: String
ENV_ITR_TEST_IMPACT_ANALYSIS_USE_ALLOCATION_TRACING: String

DD_SITE_ALLOWLIST: Array[String]
end
Expand Down
4 changes: 3 additions & 1 deletion sig/datadog/ci/test_optimisation/component.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ module Datadog
@api: Datadog::CI::Transport::Api::Base?
@dd_env: String?
@config_tags: Hash[String, String]

@bundle_location: String?
@use_single_threaded_coverage: bool
@use_allocation_tracing: bool

@skipped_tests_count: Integer
@mutex: Thread::Mutex
Expand All @@ -24,7 +26,7 @@ module Datadog
attr_reader skipped_tests_count: Integer
attr_reader correlation_id: String?

def initialize: (dd_env: String?, ?enabled: bool, ?coverage_writer: Datadog::CI::TestOptimisation::Coverage::Writer?, ?api: Datadog::CI::Transport::Api::Base?, ?config_tags: Hash[String, String]?, ?bundle_location: String?, ?use_single_threaded_coverage: bool) -> void
def initialize: (dd_env: String?, ?enabled: bool, ?coverage_writer: Datadog::CI::TestOptimisation::Coverage::Writer?, ?api: Datadog::CI::Transport::Api::Base?, ?config_tags: Hash[String, String]?, ?bundle_location: String?, ?use_single_threaded_coverage: bool, ?use_allocation_tracing: bool) -> void

def configure: (Hash[String, untyped] remote_configuration, test_session: Datadog::CI::TestSession, git_tree_upload_worker: Datadog::CI::Worker) -> void

Expand Down
15 changes: 15 additions & 0 deletions spec/datadog/ci/configuration/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
settings.ci.force_test_level_visibility = force_test_level_visibility
settings.ci.agentless_url = agentless_url
settings.ci.itr_enabled = itr_enabled
settings.ci.itr_code_coverage_use_single_threaded_mode = itr_code_coverage_use_single_threaded_mode
settings.ci.itr_test_impact_analysis_use_allocation_tracing = itr_test_impact_analysis_use_allocation_tracing
settings.site = dd_site
settings.api_key = api_key

Expand Down Expand Up @@ -97,6 +99,8 @@
let(:evp_proxy_v4_supported) { false }
let(:itr_enabled) { false }
let(:tracing_enabled) { true }
let(:itr_code_coverage_use_single_threaded_mode) { false }
let(:itr_test_impact_analysis_use_allocation_tracing) { true }

context "is enabled" do
let(:enabled) { true }
Expand Down Expand Up @@ -249,6 +253,17 @@

it "creates test visibility component with ITR enabled" do
expect(components.test_visibility.itr_enabled?).to eq(true)
expect(settings.ci.itr_test_impact_analysis_use_allocation_tracing).to eq(true)
end

context "when single threaded mode for line coverage is enabled" do
let(:itr_code_coverage_use_single_threaded_mode) { true }

it "logs a warning and disables allocation tracing for ITR" do
expect(Datadog.logger).to have_received(:warn)

expect(settings.ci.itr_test_impact_analysis_use_allocation_tracing).to eq(false)
end
end
end
end
Expand Down
41 changes: 41 additions & 0 deletions spec/datadog/ci/configuration/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,47 @@ def patcher
end
end

describe "#itr_test_impact_analysis_use_allocation_tracing" do
subject(:itr_test_impact_analysis_use_allocation_tracing) { settings.ci.itr_test_impact_analysis_use_allocation_tracing }

it { is_expected.to be true }

context "when #{Datadog::CI::Ext::Settings::ENV_ITR_TEST_IMPACT_ANALYSIS_USE_ALLOCATION_TRACING}" do
around do |example|
ClimateControl.modify(Datadog::CI::Ext::Settings::ENV_ITR_TEST_IMPACT_ANALYSIS_USE_ALLOCATION_TRACING => enable) do
example.run
end
end

context "is not defined" do
let(:enable) { nil }

it { is_expected.to be true }
end

context "is set to true" do
let(:enable) { "true" }

it { is_expected.to be true }
end

context "is set to false" do
let(:enable) { "false" }

it { is_expected.to be false }
end
end
end

describe "#itr_test_impact_analysis_use_allocation_tracing=" do
it "updates the #enabled setting" do
expect { settings.ci.itr_test_impact_analysis_use_allocation_tracing = false }
.to change { settings.ci.itr_test_impact_analysis_use_allocation_tracing }
.from(true)
.to(false)
end
end

describe "#instrument" do
let(:integration_name) { :fake }

Expand Down

0 comments on commit e6b3302

Please sign in to comment.