Module: Wildsight::Context::Rack

Included in:
Context
Defined in:
lib/wildsight/context/rack.rb

Constant Summary collapse

RACK_ENV_KEY =
'wildsight.context'
REQUEST_INCLUDE_KEYS =
['SCRIPT_NAME', 'QUERY_STRING', 'PATH_INFO', 'REMOTE_ADDR']
REQUEST_EXCLUDE_KEYS =
['HTTP_COOKIE']
RESPONSE_EXCLUDE_KEYS =
['Set-Cookie']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.detect_context(env) ⇒ Object



12
13
14
# File 'lib/wildsight/context/rack.rb', line 12

def self.detect_context(env)
  env[RACK_ENV_KEY] || Wildsight::Context.detect
end

Instance Method Details

#rack_dataObject



20
21
22
# File 'lib/wildsight/context/rack.rb', line 20

def rack_data
  @data[:rack] ||= {}
end

#rack_instrument_request(env) ⇒ Object



16
17
18
# File 'lib/wildsight/context/rack.rb', line 16

def rack_instrument_request(env)
  env[RACK_ENV_KEY] = self
end

#rack_report(env, response = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wildsight/context/rack.rb', line 24

def rack_report(env, response = nil)
  env.delete(RACK_ENV_KEY)

  rack_data[:request_target] ||= env['SCRIPT_NAME'].to_s + env['PATH_INFO'].to_s

  env.each_pair do |key,value|
    name = key.to_s
    rack_data[:request_headers] ||= {}
    rack_data[:request_headers][key] = value if REQUEST_INCLUDE_KEYS.include?(name)
    rack_data[:request_headers][key] = value if name.start_with?('HTTP_')
    rack_data[:request_headers][key] = value if name.start_with?('REQUEST_')
    rack_data[:request_headers][key] = value if name.start_with?('SERVER_')
  end

  if !rack_data[:session_id] && !env['rack.session'].nil? && env['rack.session'].respond_to?(:id)
    rack_data[:session_id] ||= env['rack.session'].id
  end

  REQUEST_EXCLUDE_KEYS.each { |key| rack_data[:request_headers].delete(key) }

  if response
    rack_data[:response_code] ||= response[0]
    rack_data[:response_headers] ||= response[1].dup
    RESPONSE_EXCLUDE_KEYS.each { |key| rack_data[:response_headers].delete(key) }
  end

  exception = env['rack.exception'] || env['action_dispatch.exception'] || env['sinatra.error']

  if exception
    exception_report(exception)
  end

  self.submit
rescue => e
  @agent.logger.log(:error, e)
end