Skip to content

Commit

Permalink
introduce Datadog::CI::Contrib::Instrumentation module to extract the…
Browse files Browse the repository at this point in the history
… logic from Settings
  • Loading branch information
anmarchenko committed Nov 8, 2024
1 parent a0b41cc commit 4905314
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
24 changes: 3 additions & 21 deletions lib/datadog/ci/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "../contrib/instrumentation"
require_relative "../ext/settings"
require_relative "../utils/bundle"

Expand All @@ -8,8 +9,6 @@ module CI
module Configuration
# Adds CI behavior to ddtrace settings
module Settings
InvalidIntegrationError = Class.new(StandardError)

def self.extended(base)
base = base.singleton_class unless base.is_a?(Class)
add_settings!(base)
Expand Down Expand Up @@ -126,23 +125,11 @@ def self.add_settings!(base)
define_method(:instrument) do |integration_name, options = {}, &block|
return unless enabled

integration = fetch_integration(integration_name)
integration.configure(options, &block)

return unless integration.enabled

patch_results = integration.patch
next if patch_results == true

error_message = <<-ERROR
Available?: #{patch_results[:available]}, Loaded?: #{patch_results[:loaded]},
Compatible?: #{patch_results[:compatible]}, Patchable?: #{patch_results[:patchable]}"
ERROR
Datadog.logger.warn("Unable to patch #{integration_name} (#{error_message})")
Contrib::Instrumentation.instrument(integration_name, options, &block)
end

define_method(:[]) do |integration_name|
fetch_integration(integration_name).configuration
Contrib::Instrumentation.fetch_integration(integration_name).configuration
end

option :trace_flush
Expand All @@ -151,11 +138,6 @@ def self.add_settings!(base)
o.type :hash
o.default({})
end

define_method(:fetch_integration) do |name|
Datadog::CI::Contrib::Integration.registry[name] ||
raise(InvalidIntegrationError, "'#{name}' is not a valid integration.")
end
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions lib/datadog/ci/contrib/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Datadog
module CI
module Contrib
module Instrumentation
class InvalidIntegrationError < StandardError; end

def self.instrument(integration_name, options = {}, &block)
integration = fetch_integration(integration_name)
integration.configure(options, &block)

return unless integration.enabled

patch_results = integration.patch
return if patch_results == true

error_message = <<-ERROR
Available?: #{patch_results[:available]}, Loaded?: #{patch_results[:loaded]},
Compatible?: #{patch_results[:compatible]}, Patchable?: #{patch_results[:patchable]}"
ERROR
Datadog.logger.warn("Unable to patch #{integration_name} (#{error_message})")
end

def self.fetch_integration(name)
Datadog::CI::Contrib::Integration.registry[name] ||
raise(InvalidIntegrationError, "'#{name}' is not a valid integration.")
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/knapsack/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.included(base)
end

module InstanceMethods
# TODO: this is coupled to RSpec integration being present
# TODO: this is coupled to RSpec integration being present, not sure if it's bad or not at this point
def knapsack__run_specs(*args)
return super if ::RSpec.configuration.dry_run? && !datadog_configuration[:dry_run_enabled]
return super unless datadog_configuration[:enabled]
Expand Down
14 changes: 14 additions & 0 deletions sig/datadog/ci/contrib/instrumentation.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Datadog
module CI
module Contrib
module Instrumentation
class InvalidIntegrationError < StandardError
end

def self.instrument: (Symbol integration_name, ?::Hash[untyped, untyped] options) { (?) -> untyped } -> void

def self.fetch_integration: (Symbol name) -> untyped
end
end
end
end
2 changes: 1 addition & 1 deletion spec/datadog/ci/configuration/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ def patcher
let(:integration_name) { :not_existing }

it "does not patch the integration" do
expect { instrument }.to raise_error(Datadog::CI::Configuration::Settings::InvalidIntegrationError)
expect { instrument }.to raise_error(Datadog::CI::Contrib::Instrumentation::InvalidIntegrationError)
end
end

Expand Down

0 comments on commit 4905314

Please sign in to comment.