Class: TraceView::Rack

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

Direct Known Subclasses

Oboe::Rack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.



11
12
13
# File 'lib/traceview/inst/rack.rb', line 11

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/traceview/inst/rack.rb', line 9

def app
  @app
end

Instance Method Details

#call(env) ⇒ Object



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
116
117
118
119
# File 'lib/traceview/inst/rack.rb', line 52

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 TraceView.tracing? && TraceView.layer == 'rack'
    rack_skipped = true
    TraceView.logger.debug "[traceview/rack] Rack skipped!"
    return @app.call(env)
  end

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

  if TraceView::Config[:rack][:log_args]
    report_kvs[:URL] = ::CGI.unescape(req.fullpath)
  else
    report_kvs[:URL] = ::CGI.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 && TraceView::XTrace.valid?(xtrace)

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

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

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

    # We log an info event with the HTTP KVs found in TraceView::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.
    TraceView::API.log_info('rack', report_kvs)

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

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

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

#collect(req, env) ⇒ Object



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
47
48
49
50
# File 'lib/traceview/inst/rack.rb', line 15

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[:Method]            = req.request_method
    report_kvs['AJAX']             = true if req.xhr?
    report_kvs['ClientIP']         = req.ip

    if TraceView::Config[:rack][:log_args]
      report_kvs['Query-String']     = ::CGI.unescape(req.query_string) unless req.query_string.empty?
    end

    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.TraceView.Version'] = ::TraceView::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.
    TraceView.logger.debug "[traceview/debug] Rack KV collection error: #{e.inspect}"
  end
  report_kvs
end