Module: TingYun::Instrumentation::MiddlewareTracing

Included in:
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



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

def call(env)
  first_middleware = note_transaction_started(env)

  state = TingYun::Agent::TransactionState.tl_get
  if first_middleware
    capture_request(state, env)
  end
  begin
    TingYun::Agent::Transaction.start(state, category, build_transaction_options(env, first_middleware))
    events.notify(:before_call, env) if first_middleware

    result = target.call(env)

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

    result
  rescue Exception => e

    TingYun::Agent.notice_error(e)
    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.http_response_code = result[0]
  end
end

#capture_request(state, env) ⇒ Object



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

def capture_request(state, env)
  if env.is_a? Hash

  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.response_content_type = headers[CONTENT_TYPE]
  end
end

#eventsObject



88
89
90
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 88

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



84
85
86
# File 'lib/ting_yun/instrumentation/middleware_tracing.rb', line 84

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