Module: TingYun::Instrumentation::MiddlewareTracing

Included in:
AgentMiddleware, MiddlewareProxy
Defined in:
lib/ting_yun/instrumentation/middleware_tracing.rb

Constant Summary collapse

TXN_STARTED_KEY =
'tingyun.transaction_started'.freeze
CONTENT_TYPE =

the trailing unless is for the benefit for Ruby 1.8.7 and can be removed when it is deprecated.

'Content-Type'.freeze

Instance Method Summary collapse

Instance Method Details

#_nr_has_middleware_tracingObject



15
16
17
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 15

def _nr_has_middleware_tracing
  true
end

#build_transaction_options(env, first_middleware) ⇒ Object



19
20
21
22
23
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 19

def build_transaction_options(env, first_middleware)
  opts = transaction_options
  opts = merge_first_middleware_options(opts, env) if first_middleware
  opts
end

#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
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 52

def call(env)
  return target.call(env) if sinatra_static?(env)

  first_middleware = note_transaction_started(env)
  state = TingYun::Agent::TransactionState.tl_get
  begin

    if first_middleware
      events.notify(:cross_app_before_call, env)
    end
    TingYun::Agent::Transaction.start(state, category, build_transaction_options(env, first_middleware))

    result = (target == self) ? traced_call(env) : target.call(env)

    if first_middleware
      capture_http_response_code(state, result)
      capture_response_content_type(state, result)
      events.notify(:cross_app_after_call, result)
    end

    result
  rescue Exception => e
    TingYun::Agent.notice_error(e,:type=>:error)
    raise e
  ensure
    TingYun::Agent::Transaction.stop(state)
  end
end

#capture_http_response_code(state, result) ⇒ Object



32
33
34
35
36
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 32

def capture_http_response_code(state, result)
  if result.is_a?(Array) && state.current_transaction
    state.current_transaction.attributes.add_agent_attribute(:httpStatus, result[0].to_s)
  end
end

#capture_response_content_type(state, result) ⇒ Object



41
42
43
44
45
46
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 41

def capture_response_content_type(state, result)
  if result.is_a?(Array) && state.current_transaction
    _, headers, _ = result
    state.current_transaction.attributes.add_agent_attribute(:contentType, headers[CONTENT_TYPE].to_s)
  end
end

#eventsObject



86
87
88
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 86

def events
  ::TingYun::Agent.instance.events
end

#merge_first_middleware_options(opts, env) ⇒ Object



25
26
27
28
29
30
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 25

def merge_first_middleware_options(opts, env)
  opts.merge(
      :request          => ::Rack::Request.new(env),
      :apdex_start_time => TingYun::Instrumentation::Support::QueueTime.parse_frontend_timestamp(env)
  )
end

#note_transaction_started(env) ⇒ Object



82
83
84
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 82

def note_transaction_started(env)
  env[TXN_STARTED_KEY] = true unless env[TXN_STARTED_KEY]
end

#sinatra_static?(env) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 48

def sinatra_static?(env)
  defined?(::Sinatra) && defined?(::Sinatra::Base) && env['REQUEST_URI'] && env['REQUEST_URI'] =~ /\.(css|js|html|png|jpg|jpeg|gif|bmp)\Z/i
end