Skip to content

Commit

Permalink
Merge branch 'master' into appsec-56187-collect-and-export-rasp-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Strech authored Jan 22, 2025
2 parents 728e95d + 97d8583 commit 7a47cef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/datadog/tracing/contrib/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def configure(&block)
module Settings
InvalidIntegrationError = Class.new(StandardError)

# Used to avoid concurrency issues between registering integrations (e.g. mutation) and reporting the
# current integrations for logging/debugging/telemetry purposes (e.g. iteration) in the
# `@instrumented_integrations` hash.
#
# See https://github.com/DataDog/dd-trace-rb/issues/2851 for details on the original issue.
INSTRUMENTED_INTEGRATIONS_LOCK = Mutex.new

def self.included(base)
base.class_eval do
settings :contrib do
Expand Down Expand Up @@ -161,7 +168,10 @@ def instrument(integration_name, options = {}, &block)
configuration_name = options[:describes] || :default
filtered_options = options.reject { |k, _v| k == :describes }
integration.configure(configuration_name, filtered_options, &block)
instrumented_integrations[integration_name] = integration
INSTRUMENTED_INTEGRATIONS_LOCK.synchronize do
@instrumented_integrations ||= {}
@instrumented_integrations[integration_name] = integration
end

# Add to activation list
integrations_pending_activation << integration
Expand Down Expand Up @@ -192,14 +202,16 @@ def integrations_pending_activation
@integrations_pending_activation ||= Set.new
end

# This method is only for logging/debugging/telemetry purposes (e.g. iteration) in the
# `@instrumented_integrations` hash.
# @!visibility private
def instrumented_integrations
@instrumented_integrations ||= {}
INSTRUMENTED_INTEGRATIONS_LOCK.synchronize { (@instrumented_integrations&.dup || {}).freeze }
end

# @!visibility private
def reset!
instrumented_integrations.clear
INSTRUMENTED_INTEGRATIONS_LOCK.synchronize { @instrumented_integrations&.clear }
super
end

Expand Down
10 changes: 10 additions & 0 deletions spec/datadog/tracing/contrib/extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
before { registry.add(integration_name, integration) }
end

before do
allow(Datadog.logger).to receive(:warn)
end

context 'for' do
describe Datadog do
describe '#configure' do
Expand Down Expand Up @@ -244,6 +248,12 @@ def self.patch
end
end
end

describe '#instrumented_integrations' do
subject(:instrumented_integrations) { settings.instrumented_integrations }

it { is_expected.to be_frozen }
end
end
end
end
Expand Down

0 comments on commit 7a47cef

Please sign in to comment.