Sha256: 130ef92408cf581b81538aeb1405ccb08351a1ba8a35ace56088e7e0b907a01a

Contents?: true

Size: 1.98 KB

Versions: 20

Compression:

Stored size: 1.98 KB

Contents

# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2021

module Instana
  module Backend
    # @since 1.197.0
    class HostAgent
      attr_reader :future, :client

      def initialize(discovery: Concurrent::Atom.new(nil), logger: ::Instana.logger)
        @discovery = discovery
        @logger = logger
        @future = nil
        @client = nil
      end

      def setup; end

      def spawn_background_thread
        return if ENV.key?('INSTANA_TEST')

        @future = Concurrent::Promises.future do
          announce
        end
      end

      alias start spawn_background_thread

      def announce
        @client = until_not_nil { HostAgentLookup.new.call }
        @discovery.delete_observers
        @discovery
          .with_observer(HostAgentActivationObserver.new(@client, @discovery))
          .with_observer(HostAgentReportingObserver.new(@client, @discovery))

        @discovery.swap { nil }
        @client
      end

      alias after_fork announce

      # @return [Boolean] true if the agent able to send spans to the backend
      def ready?
        ENV.key?('INSTANA_TEST') || !@discovery.value.nil?
      end

      # @return [Hash, NilClass] the backend friendly description of the current in process collector
      def source
        {
          e: discovery_value['pid'],
          h: discovery_value['agentUuid']
        }.reject { |_, v| v.nil? }
      end

      # @return [Array] extra headers to include in the trace
      def extra_headers
        discovery_value['extraHeaders']
      end

      # @return [Hash] values which are removed from urls sent to the backend
      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
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
instana-1.209.2 lib/instana/backend/host_agent.rb
instana-1.209.1 lib/instana/backend/host_agent.rb
instana-1.209.0.pre3 lib/instana/backend/host_agent.rb
instana-1.209.0.pre2 lib/instana/backend/host_agent.rb
instana-1.209.0.pre1 lib/instana/backend/host_agent.rb
instana-1.208.0 lib/instana/backend/host_agent.rb
instana-1.207.0 lib/instana/backend/host_agent.rb
instana-1.206.0 lib/instana/backend/host_agent.rb
instana-1.205.0 lib/instana/backend/host_agent.rb
instana-1.204.0 lib/instana/backend/host_agent.rb
instana-1.204.0.pre3 lib/instana/backend/host_agent.rb
instana-1.204.0.pre2 lib/instana/backend/host_agent.rb
instana-1.204.0.pre1 lib/instana/backend/host_agent.rb
instana-1.203.2 lib/instana/backend/host_agent.rb
instana-1.203.1 lib/instana/backend/host_agent.rb
instana-1.203.0 lib/instana/backend/host_agent.rb
instana-1.202.0 lib/instana/backend/host_agent.rb
instana-1.201.0 lib/instana/backend/host_agent.rb
instana-1.201.0.pre1 lib/instana/backend/host_agent.rb
instana-1.200.0 lib/instana/backend/host_agent.rb