Class: TingYun::Agent::CrossAppMonitor

Inherits:
InboundRequestMonitor show all
Defined in:
lib/ting_yun/agent/cross_app/cross_app_monitor.rb

Constant Summary collapse

TY_ID_HEADER =
'HTTP_X_TINGYUN_ID'.freeze
TY_DATA_HEADER =
'X-Tingyun-Tx-Data'.freeze

Instance Method Summary collapse

Methods inherited from InboundRequestMonitor

#initialize

Constructor Details

This class inherits a constructor from TingYun::Agent::InboundRequestMonitor

Instance Method Details

#build_payload(state) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 56

def build_payload(state)
  timings = state.timings

  payload = {
    :id => TingYun::Agent.config[:tingyunIdSecret].split('|')[1],
    :action => state.transaction_name,
    :trId => state.trace_id,
    :time => {
      :duration => timings.app_time_in_millis,
      :qu => timings.queue_time_in_millis,
      :db => timings.sql_duration,
      :ex => timings.external_duration,
      :rds => timings.rds_duration,
      :mc => timings.mc_duration,
      :mon => timings.mon_duration,
      :code => timings.app_execute_duration
    }
  }
  payload[:tr] = 1 if timings.slow_action_tracer?
  payload[:r] = state.client_req_id unless state.client_req_id.nil?
  payload
end

#insert_response_header(response_headers) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 43

def insert_response_header(response_headers)
  state = TingYun::Agent::TransactionState.tl_get
  if state.same_account?
    txn = state.current_transaction
    if txn
      # set_response_headers
      response_headers[TY_DATA_HEADER] = TingYun::Support::Serialize::JSONWrapper.dump build_payload(state)
      TingYun::Agent.logger.debug("now,cross app will send response_headers  #{response_headers[TY_DATA_HEADER]}")
    end
  end
end

#on_finished_configuring(events) ⇒ Object



18
19
20
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 18

def on_finished_configuring(events)
  register_event_listeners(events)
end

#register_event_listeners(events) ⇒ Object

Expected sequence of events:

:before_call will save our cross application request id to the thread
:after_call will write our response headers/metrics and clean up the thread


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 26

def register_event_listeners(events)
  TingYun::Agent.logger.debug("Wiring up Cross Application Tracing to events after finished configuring")

  events.subscribe(:cross_app_before_call) do |env| #THREAD_LOCAL_ACCESS
    if TingYun::Agent::CrossAppTracing.cross_app_enabled?
      state = TingYun::Agent::TransactionState.tl_get
      state.save_referring_transaction_info(env[TY_ID_HEADER].split(';')) if env[TY_ID_HEADER]
    end
  end

  events.subscribe(:cross_app_after_call) do |_status_code, headers, _body| #THREAD_LOCAL_ACCESS
    insert_response_header(headers) if TingYun::Agent::CrossAppTracing.cross_app_enabled?
  end

end