class Instana::Backend::ServerlessAgent

@since 1.197.0

Constants

DEFAULT_SECRETS

Attributes

timer[R]

Public Class Methods

new(snapshots, timer_class: Concurrent::TimerTask, processor: ::Instana.processor, logger: ::Instana.logger, backend_uri: ENV['INSTANA_ENDPOINT_URL'], secrets: ENV.fetch('INSTANA_SECRETS', DEFAULT_SECRETS), headers: ENV.fetch('INSTANA_EXTRA_HTTP_HEADERS', '')) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/instana/backend/serverless_agent.rb, line 12
def initialize(snapshots,
               timer_class: Concurrent::TimerTask,
               processor: ::Instana.processor,
               logger: ::Instana.logger,
               backend_uri: ENV['INSTANA_ENDPOINT_URL'],
               secrets: ENV.fetch('INSTANA_SECRETS', DEFAULT_SECRETS), headers: ENV.fetch('INSTANA_EXTRA_HTTP_HEADERS', ''))
  @snapshots = snapshots
  @processor = processor
  @logger = logger
  @timer = timer_class.new(execution_interval: 1, run_now: true) { send_bundle }
  @backend_uri = URI(backend_uri)
  @client = Backend::RequestClient.new(@backend_uri.host, @backend_uri.port, use_ssl: @backend_uri.scheme == "https")
  @secrets = secrets
  @headers = headers
end

Public Instance Methods

after_fork()
extra_headers() click to toggle source

@return [Array] extra headers to include in the trace

# File lib/instana/backend/serverless_agent.rb, line 56
def extra_headers
  @headers.split(';')
end
ready?() click to toggle source

@return [Boolean] true if the agent able to send spans to the backend

# File lib/instana/backend/serverless_agent.rb, line 39
def ready?
  true
end
secret_values() click to toggle source

@return [Hash] values which are removed from urls sent to the backend

# File lib/instana/backend/serverless_agent.rb, line 61
def secret_values
  matcher, *keys = @secrets.split(/[:,]/)
  {'matcher' => matcher, 'list' => keys}
end
send_bundle() click to toggle source
# File lib/instana/backend/serverless_agent.rb, line 66
def send_bundle
  spans = @processor.queued_spans
  bundle = {
    spans: spans,
    metrics: {
      plugins: agent_snapshots
    }
  }

  path = "#{@backend_uri.path}/bundle"
  response = @client.send_request('POST', path, bundle, request_headers)

  return if response.ok?

  @logger.warn("Recived a `#{response.code}` when sending data.")
end
setup() click to toggle source

rubocop:enable Metrics/ParameterLists

# File lib/instana/backend/serverless_agent.rb, line 29
def setup; end
source() click to toggle source

@return [Hash, NilClass] the backend friendly description of the current in process collector

# File lib/instana/backend/serverless_agent.rb, line 44
def source
  snapshot = @snapshots.detect { |s| s.respond_to?(:source) }

  if snapshot
    snapshot.source
  else
    @logger.warn('Unable to find a snapshot which provides a source.')
    {}
  end
end
spawn_background_thread() click to toggle source
# File lib/instana/backend/serverless_agent.rb, line 31
def spawn_background_thread
  @timer.execute
end
Also aliased as: start, after_fork
start()

Private Instance Methods

agent_snapshots() click to toggle source
# File lib/instana/backend/serverless_agent.rb, line 93
def agent_snapshots
  @snapshots.map do |snapshot|
    begin # rubocop:disable Style/RedundantBegin, Lint/RedundantCopDisableDirective
      snapshot.snapshot
    rescue StandardError => e
      @logger.error(e.message)
      nil
    end
  end.reject { |_, v| v.nil? }
end
host_name() click to toggle source
# File lib/instana/backend/serverless_agent.rb, line 104
def host_name
  snapshot = @snapshots.detect { |s| s.respond_to?(:host_name) }

  if snapshot
    snapshot.host_name
  else
    @logger.warn('Unable to find a snapshot which provides a host_name.')
    ''
  end
end
request_headers() click to toggle source
# File lib/instana/backend/serverless_agent.rb, line 85
def request_headers
  {
    'X-Instana-Host' => host_name,
    'X-Instana-Key' => ENV['INSTANA_AGENT_KEY'],
    'X-Instana-Time' => (Time.now.to_i * 1000).to_s
  }
end