lib/instana/agent.rb in instana-1.7.0 vs lib/instana/agent.rb in instana-1.7.1
- old
+ new
@@ -2,17 +2,20 @@
require 'net/http'
require 'socket'
require 'sys/proctable'
require 'timers'
require 'uri'
+require 'thread'
include Sys
module Instana
class Agent
attr_accessor :state
attr_accessor :agent_uuid
attr_accessor :process
+ attr_accessor :collect_thread
+ attr_accessor :thread_spawn_lock
LOCALHOST = '127.0.0.1'.freeze
MIME_JSON = 'application/json'.freeze
DISCOVERY_PATH = 'com.instana.plugin.ruby.discovery'.freeze
@@ -30,10 +33,12 @@
# Two timers, one for each state (unannounced & announced)
@timers = ::Timers::Group.new
@announce_timer = nil
@collect_timer = nil
+ @thread_spawn_lock = Mutex.new
+
# Detect platform flags
@is_linux = (RUBY_PLATFORM =~ /linux/i) ? true : false
@is_osx = (RUBY_PLATFORM =~ /darwin/i) ? true : false
# In case we're running in Docker, have the default gateway available
@@ -88,25 +93,28 @@
# end
# end
# end
#
def spawn_background_thread
- # The thread calling fork is the only thread in the created child process.
- # fork doesn’t copy other threads.
- # Restart our background thread
- Thread.new do
- start
- end
+ @thread_spawn_lock.synchronize {
+ if @collect_thread && @collect_thread.alive?
+ ::Instana.logger.info "[instana] Collect thread already started & alive. Not spawning another."
+ else
+ @collect_thread = Thread.new do
+ start
+ end
+ end
+ }
end
# Sets up periodic timers and starts the agent in a background thread.
#
def setup
# The announce timer
# We attempt to announce this ruby sensor to the host agent.
# In case of failure, we try again in 30 seconds.
- @announce_timer = @timers.every(30) do
+ @announce_timer = @timers.now_and_every(30) do
if @state == :unannounced
if host_agent_ready? && announce_sensor
transition_to(:announced)
::Instana.logger.warn "Host agent available. We're in business. (#{@state} pid:#{Process.pid} #{@process[:name]})"
end
@@ -141,9 +149,10 @@
# in the setup method. This is blocking and should only be
# called from an already initialized background thread.
#
def start
::Instana.logger.warn "Host agent not available. Will retry periodically." unless host_agent_ready?
+
loop do
if @state == :unannounced
@collect_timer.pause
@announce_timer.resume
else