Class: Oboe::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/oboe/inst/rack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.



10
11
12
# File 'lib/oboe/inst/rack.rb', line 10

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/oboe/inst/rack.rb', line 8

def app
  @app
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/oboe/inst/rack.rb', line 48

def call(env)
  rack_skipped = false

  # In the case of nested Ruby apps such as Grape inside of Rails
  # or Grape inside of Grape, each app has it's own instance
  # of rack middleware.  We avoid tracing rack more than once and
  # instead start instrumenting from the first rack pass.

  # If we're already tracing a rack layer, dont't start another one.
  if Oboe.tracing? && Oboe.layer == 'rack'
    rack_skipped = true
    Oboe.logger.debug "[oboe/rack] Rack skipped!"
    return @app.call(env)
  end

  req = ::Rack::Request.new(env)
  report_kvs = {}

  if Oboe::Config[:rack][:log_args]
    report_kvs[:URL] = URI.unescape(req.fullpath)
  else
    report_kvs[:URL] = URI.unescape(req.path)
  end

  # Check for and validate X-Trace request header to pick up tracing context
  xtrace = env.is_a?(Hash) ? env['HTTP_X_TRACE'] : nil
  xtrace_header = xtrace if xtrace && Oboe::XTrace.valid?(xtrace)

  # Under JRuby, JOboe may have already started a trace.  Make note of this
  # if so and don't clear context on log_end (see oboe/api/logging.rb)
  Oboe.has_incoming_context = Oboe.tracing?
  Oboe.has_xtrace_header = xtrace_header
  Oboe.is_continued_trace = Oboe.has_incoming_context or Oboe.has_xtrace_header

  Oboe::API.log_start('rack', xtrace_header, report_kvs)

  # We only trace a subset of requests based off of sample rate so if
  # Oboe::API.log_start really did start a trace, we act accordingly here.
  if Oboe.tracing?
    report_kvs = collect(req, env)

    # We log an info event with the HTTP KVs found in Oboe::Rack.collect
    # This is done here so in the case of stacks that try/catch/abort
    # (looking at you Grape) we're sure the KVs get reported now as
    # this code may not be returned to later.
    Oboe::API.log_info('rack', report_kvs)

    status, headers, response = @app.call(env)

    xtrace = Oboe::API.log_end('rack', :Status => status)
  else
    status, headers, response = @app.call(env)
  end

  [status, headers, response]
rescue Exception => e
  unless rack_skipped
    Oboe::API.log_exception('rack', e)
    xtrace = Oboe::API.log_end('rack', :Status => 500)
  end
  raise
ensure
  if !rack_skipped && headers && Oboe::XTrace.valid?(xtrace)
    unless defined?(JRUBY_VERSION) && Oboe.is_continued_trace?
      headers['X-Trace'] = xtrace if headers.is_a?(Hash)
    end
  end
end

#collect(req, env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/oboe/inst/rack.rb', line 14

def collect(req, env)
  report_kvs = {}

  begin
    report_kvs['HTTP-Host']        = req.host
    report_kvs['Port']             = req.port
    report_kvs['Proto']            = req.scheme
    report_kvs['Query-String']     = URI.unescape(req.query_string) unless req.query_string.empty?
    report_kvs[:Method]            = req.request_method
    report_kvs['AJAX']             = true if req.xhr?
    report_kvs['ClientIP']         = req.ip

    report_kvs['X-TV-Meta']         = env['HTTP_X_TV_META']          if env.key?('HTTP_X_TV_META')

    # Report any request queue'ing headers.  Report as 'Request-Start' or the summed Queue-Time
    report_kvs['Request-Start']     = env['HTTP_X_REQUEST_START']    if env.key?('HTTP_X_REQUEST_START')
    report_kvs['Request-Start']     = env['HTTP_X_QUEUE_START']      if env.key?('HTTP_X_QUEUE_START')
    report_kvs['Queue-Time']        = env['HTTP_X_QUEUE_TIME']       if env.key?('HTTP_X_QUEUE_TIME')

    report_kvs['Forwarded-For']     = env['HTTP_X_FORWARDED_FOR']    if env.key?('HTTP_X_FORWARDED_FOR')
    report_kvs['Forwarded-Host']    = env['HTTP_X_FORWARDED_HOST']   if env.key?('HTTP_X_FORWARDED_HOST')
    report_kvs['Forwarded-Proto']   = env['HTTP_X_FORWARDED_PROTO']  if env.key?('HTTP_X_FORWARDED_PROTO')
    report_kvs['Forwarded-Port']    = env['HTTP_X_FORWARDED_PORT']   if env.key?('HTTP_X_FORWARDED_PORT')

    report_kvs['Ruby.Oboe.Version'] = ::Oboe::Version::STRING
    report_kvs['ProcessID']         = Process.pid
    report_kvs['ThreadID']          = Thread.current.to_s[/0x\w*/]
  rescue StandardError => e
    # Discard any potential exceptions. Debug log and report whatever we can.
    Oboe.logger.debug "[oboe/debug] Rack KV collection error: #{e.inspect}"
  end
  report_kvs
end