lib/instana/backend/host_agent.rb in instana-1.197.0.pre1 vs lib/instana/backend/host_agent.rb in instana-1.197.0.pre2
- old
+ new
@@ -3,26 +3,33 @@
module Instana
module Backend
# @since 1.197.0
class HostAgent
- def initialize(discovery: Concurrent::Atom.new(nil))
+ attr_reader :future
+
+ def initialize(discovery: Concurrent::Atom.new(nil), logger: ::Instana.logger)
@discovery = discovery
- @client = nil
+ @logger = logger
+ @future = nil
end
- def setup
+ def setup; end
+
+ def spawn_background_thread
return if ENV.key?('INSTANA_TEST')
- @client = HostAgentLookup.new.call
- @discovery
- .with_observer(HostAgentActivationObserver.new(@client, @discovery))
- .with_observer(HostAgentReportingObserver.new(@client, @discovery))
- end
+ @future = Concurrent::Promises.future do
+ client = until_not_nil { HostAgentLookup.new.call }
+ @discovery.delete_observers
+ @discovery
+ .with_observer(HostAgentActivationObserver.new(client, @discovery))
+ .with_observer(HostAgentReportingObserver.new(client, @discovery))
- def spawn_background_thread
- @discovery.swap { nil }
+ @discovery.swap { nil }
+ client
+ end
end
# @return [Boolean] true if the agent able to send spans to the backend
def ready?
ENV.key?('INSTANA_TEST') || !@discovery.value.nil?
@@ -45,9 +52,19 @@
def secret_values
discovery_value['secrets']
end
private
+
+ def until_not_nil
+ loop do
+ result = yield
+ return result unless result.nil?
+
+ @logger.debug("Waiting on a connection to the agent.")
+ sleep(1)
+ end
+ end
def discovery_value
v = @discovery.value
v || {}
end