Class: ScoutApm::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/reporter.rb

Constant Summary collapse

CA_FILE =
File.join( File.dirname(__FILE__), *%w[.. .. data cacert.pem] )
VERIFY_MODE =
OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :checkin, config = Agent.instance.config, logger = Agent.instance.logger, instant_key = nil) ⇒ Reporter

Returns a new instance of Reporter.



13
14
15
16
17
18
# File 'lib/scout_apm/reporter.rb', line 13

def initialize(type = :checkin, config=Agent.instance.config, logger=Agent.instance.logger, instant_key=nil)
  @config = config
  @logger = logger
  @type = type
  @instant_key = instant_key
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/scout_apm/reporter.rb', line 8

def config
  @config
end

#instant_keyObject (readonly)

Returns the value of attribute instant_key.



11
12
13
# File 'lib/scout_apm/reporter.rb', line 11

def instant_key
  @instant_key
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/scout_apm/reporter.rb', line 9

def logger
  @logger
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/scout_apm/reporter.rb', line 10

def type
  @type
end

Instance Method Details

#can_report?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/scout_apm/reporter.rb', line 47

def can_report?
  case type
  when :deploy_hook
    %w(host key name).each do |k|
      if config.value(k).nil?
        logger.warn "/#{type} FAILED: missing required config value for #{k}"
        return false
      end
    end
    return true
  else
    return true
  end
end

#report(payload, headers = {}) ⇒ Object

TODO: Parse & return a real response object, not the HTTP Response object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/scout_apm/reporter.rb', line 21

def report(payload, headers = {})
  # Some posts (typically ones under development) bypass the ingestion pipeline and go directly to the webserver. They use direct_host instead of host
  hosts = [:deploy_hook, :instant_trace].include?(type) ? config.value('direct_host') : config.value('host')

  Array(hosts).each do |host|
    full_uri = uri(host)
    response = post(full_uri, payload, headers)
    unless response && response.is_a?(Net::HTTPSuccess)
      logger.warn "Error on checkin to #{full_uri.to_s}: #{response.inspect}"
    end
  end
end

#uri(host) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scout_apm/reporter.rb', line 34

def uri(host)
  case type
  when :checkin
    URI.parse("#{host}/apps/checkin.scout?key=#{config.value('key')}&name=#{CGI.escape(Environment.instance.application_name)}")
  when :app_server_load
    URI.parse("#{host}/apps/app_server_load.scout?key=#{config.value('key')}&name=#{CGI.escape(Environment.instance.application_name)}")
  when :deploy_hook
    URI.parse("#{host}/apps/deploy.scout?key=#{config.value('key')}&name=#{CGI.escape(config.value('name'))}")
  when :instant_trace
    URI.parse("#{host}/apps/instant_trace.scout?key=#{config.value('key')}&name=#{CGI.escape(config.value('name'))}&instant_key=#{instant_key}")
  end.tap{|u| logger.debug("Posting to #{u.to_s}")}
end