From 88f52fc829b09868713e4a23b3b34d1dc80b1523 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 16 Dec 2024 16:58:46 -0500 Subject: [PATCH] move current_component --- lib/datadog/di.rb | 37 ------------------------------------- lib/datadog/di/base.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/datadog/di.rb b/lib/datadog/di.rb index 844388a8267..221c32864f6 100644 --- a/lib/datadog/di.rb +++ b/lib/datadog/di.rb @@ -47,8 +47,6 @@ def enabled? # Expose DI to global shared objects Extensions.activate! - LOCK = Mutex.new - class << self # This method is called from DI Remote handler to issue DI operations @@ -63,41 +61,6 @@ class << self def component Datadog.send(:components).dynamic_instrumentation end - - # DI code tracker is instantiated globally before the regular set of - # components is created, but the code tracker needs to call out to the - # "current" DI component to perform instrumentation when application - # code is loaded. Because this call may happen prior to Datadog - # components having been initialized, we maintain the "current component" - # which contains a reference to the most recently instantiated - # DI::Component. This way, if a DI component hasn't been instantiated, - # we do not try to reference Datadog.components. - # In other words, this method exists so that we never attempt to call - # Datadog.components from the code tracker. - def current_component - LOCK.synchronize do - @current_components&.last - end - end - - # To avoid potential races with DI::Component being added and removed, - # we maintain a list of the components. Normally the list should contain - # either zero or one component depending on whether DI is enabled in - # Datadog configuration. However, if a new instance of DI::Component - # is created while the previous instance is still running, we are - # guaranteed to not end up with no component when one is running. - def add_current_component(component) - LOCK.synchronize do - @current_components ||= [] - @current_components << component - end - end - - def remove_current_component(component) - LOCK.synchronize do - @current_components&.delete(component) - end - end end end end diff --git a/lib/datadog/di/base.rb b/lib/datadog/di/base.rb index 62a9b8fdc75..9f8fece15ce 100644 --- a/lib/datadog/di/base.rb +++ b/lib/datadog/di/base.rb @@ -16,6 +16,8 @@ module Datadog # @api private module DI + LOCK = Mutex.new + class << self attr_reader :code_tracker @@ -75,6 +77,41 @@ def deactivate_tracking! def code_tracking_active? code_tracker&.active? || false end + + # DI code tracker is instantiated globally before the regular set of + # components is created, but the code tracker needs to call out to the + # "current" DI component to perform instrumentation when application + # code is loaded. Because this call may happen prior to Datadog + # components having been initialized, we maintain the "current component" + # which contains a reference to the most recently instantiated + # DI::Component. This way, if a DI component hasn't been instantiated, + # we do not try to reference Datadog.components. + # In other words, this method exists so that we never attempt to call + # Datadog.components from the code tracker. + def current_component + LOCK.synchronize do + @current_components&.last + end + end + + # To avoid potential races with DI::Component being added and removed, + # we maintain a list of the components. Normally the list should contain + # either zero or one component depending on whether DI is enabled in + # Datadog configuration. However, if a new instance of DI::Component + # is created while the previous instance is still running, we are + # guaranteed to not end up with no component when one is running. + def add_current_component(component) + LOCK.synchronize do + @current_components ||= [] + @current_components << component + end + end + + def remove_current_component(component) + LOCK.synchronize do + @current_components&.delete(component) + end + end end end end