Skip to content

Commit

Permalink
extract patch_integration method in Contrib::Instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 20, 2024
1 parent 9c8d52b commit ced48ce
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
53 changes: 28 additions & 25 deletions lib/datadog/ci/contrib/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,12 @@ def self.register_integration(integration_class)
# This method is called when user has `c.ci.instrument :integration_name` in their code.
def self.instrument(integration_name, options = {}, &block)
integration = fetch_integration(integration_name)
# when manually instrumented, it might be configured via code
integration.configure(options, &block)

return unless integration.enabled

patch_results = integration.patch
if patch_results[:ok]
# try to patch dependant integrations (for example knapsack that depends on rspec)
dependants = integration.dependants
.map { |name| fetch_integration(name) }
.filter { |integration| integration.patchable? }

Datadog.logger.debug("Found dependent integrations for #{integration_name}: #{dependants}")

dependants.each do |dependent_integration|
dependent_integration.patch
end
else
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
patch_integration(integration, with_dependencies: true)
end

# This method instruments all additional test libraries (ex: selenium-webdriver) that need to be instrumented
Expand All @@ -58,15 +41,11 @@ def self.instrument_on_session_start

@registry.each do |name, integration|
next unless integration.late_instrument?
next unless integration.enabled

Datadog.logger.debug "#{name} is allowed to be late instrumented"

patch_results = integration.patch
if patch_results[:ok]
Datadog.logger.debug("#{name} is patched")
else
Datadog.logger.debug("#{name} is not patched (#{patch_results})")
end
patch_integration(integration)
end
end

Expand All @@ -82,6 +61,30 @@ def self.integration_name(subclass)
raise "Integration name could not be derived for #{subclass}" if result.nil?
result
end

def self.patch_integration(integration, with_dependencies: false)
patch_results = integration.patch

if patch_results[:ok]
Datadog.logger.debug("#{integration.class} is patched")

return unless with_dependencies

# try to patch dependant integrations (for example knapsack that depends on rspec)
dependants = integration.dependants
.map { |name| fetch_integration(name) }
.filter { |integration| integration.patchable? }

Datadog.logger.debug("Found dependent integrations for #{integration.class}: #{dependants}")

dependants.each do |dependent_integration|
patch_integration(dependent_integration, with_dependencies: true)
end

else
Datadog.logger.debug("Attention: #{integration.class} is not patched (#{patch_results})")
end
end
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion sig/datadog/ci/contrib/instrumentation.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ module Datadog

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

def self.instrument_on_session_start: () -> void

def self.fetch_integration: (Symbol name) -> untyped

def self.integration_name: (Class) -> Symbol

def self.register_integration: (Class integration) -> void

def self.instrument_on_session_start: () -> void
def self.patch_integration: (Contrib::Integration integration, ?with_dependencies: bool) -> void

end
end
end
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/contrib/integration.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module Datadog

def patch: () -> Hash[Symbol, bool]

def dependants: () -> Array[Symbol]

def late_instrument?: () -> bool

def new_configuration: () -> Datadog::CI::Contrib::Settings
Expand Down

0 comments on commit ced48ce

Please sign in to comment.