Skip to content

Commit

Permalink
working auto-instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 21, 2024
1 parent 49258a8 commit a9ce584
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
57 changes: 43 additions & 14 deletions lib/datadog/ci/contrib/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "datadog/core/utils/only_once"

module Datadog
module CI
module Contrib
Expand All @@ -24,18 +26,7 @@ def self.register_integration(integration_class)
def self.auto_instrument
Datadog.logger.debug("Auto instrumenting all integrations...")

auto_instrumented_integrations = []
@registry.each do |name, integration|
# ignore integrations that are not in the Gemfile or have incompatible versions
next unless integration.compatible?

# late instrumented integrations will be patched when the test session starts
next if integration.late_instrument?

Datadog.logger.debug("#{name} should be auto instrumented")
auto_instrumented_integrations << integration
end

auto_instrumented_integrations = fetch_auto_instrumented_integrations
if auto_instrumented_integrations.empty?
Datadog.logger.warn(
"Auto instrumentation was requested, but no available integrations were found. " \
Expand All @@ -44,15 +35,26 @@ def self.auto_instrument
return
end

script_compiled_tracepoint = TracePoint.new(:script_compiled) do
script_compiled_tracepoint = TracePoint.new(:script_compiled) do |tp|
all_patched = true

auto_instrumented_integrations.each do |integration|
next if integration.patched?

all_patched = false
next unless integration.loaded?

Datadog.logger.debug("#{integration.class} is loaded")
auto_configure_datadog

Datadog.logger.debug("#{integration.class} is loaded")
patch_integration(integration)
end

if all_patched
Datadog.logger.debug("All expected integrations are patched, disabling the script_compiled tracepoint")

tp.disable
end
end
script_compiled_tracepoint.enable
end
Expand Down Expand Up @@ -126,6 +128,33 @@ def self.patch_integration(integration, with_dependencies: false)
Datadog.logger.debug("Attention: #{integration.class} is not patched (#{patch_results})")
end
end

def self.fetch_auto_instrumented_integrations
@registry.filter_map do |name, integration|
# ignore integrations that are not in the Gemfile or have incompatible versions
next unless integration.compatible?

# late instrumented integrations will be patched when the test session starts
next if integration.late_instrument?

Datadog.logger.debug("#{name} should be auto instrumented")
integration
end
end

def self.auto_configure_datadog
configure_once.run do
Datadog.logger.debug("Applying Datadog configuration in CI mode...")
Datadog.configure do |c|
c.ci.enabled = true
c.tracing.enabled = true
end
end
end

def self.configure_once
@configure_once ||= Datadog::Core::Utils::OnlyOnce.new
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions sig/datadog/ci/contrib/instrumentation.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Datadog

self.@registry: Hash[Symbol, untyped]

self.@configure_once: Datadog::Core::Utils::OnlyOnce

def self.registry: () -> Hash[Symbol, untyped]

def self.auto_instrument: () -> void
Expand All @@ -23,6 +25,11 @@ module Datadog

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

def self.fetch_auto_instrumented_integrations: () -> Array[Contrib::Integration]

def self.auto_configure_datadog: () -> void

def self.configure_once: () -> Datadog::Core::Utils::OnlyOnce
end
end
end
Expand Down

0 comments on commit a9ce584

Please sign in to comment.