Class: TingYun::Agent::CrossAppMonitor
- Inherits:
-
InboundRequestMonitor
- Object
- InboundRequestMonitor
- TingYun::Agent::CrossAppMonitor
- 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
- #build_payload(state) ⇒ Object
- #clear_client_tingyun_id_secret(state) ⇒ Object
- #cross_app_enabled? ⇒ Boolean
- #execute_duration(state) ⇒ Object
- #insert_response_header(state, response_headers) ⇒ Object
- #on_finished_configuring(events) ⇒ Object
-
#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.
- #same_account?(state) ⇒ Boolean
- #save_referring_transaction_info(state, request) ⇒ Object
- #set_response_headers(state, response_headers) ⇒ Object
- #slow_action_tracer?(state) ⇒ Boolean
Methods inherited from InboundRequestMonitor
Constructor Details
This class inherits a constructor from TingYun::Agent::InboundRequestMonitor
Instance Method Details
#build_payload(state) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 97 def build_payload(state) payload = { :id => TingYun::Agent.config[:tingyunIdSecret].split('|')[1], :action => state.current_transaction.best_name, :trId => state.transaction_sample_builder.trace.guid, :time => { :duration => state.web_duration, :qu => state.queue_duration, :db => state.sql_duration, :ex => state.external_duration, :rds => state.rds_duration, :mc => state.mc_duration, :mon => state.mon_duration, :code => execute_duration(state) } } payload[:tr] = 1 if slow_action_tracer?(state) payload[:r] = state.client_req_id unless state.client_req_id.nil? payload end |
#clear_client_tingyun_id_secret(state) ⇒ Object
78 79 80 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 78 def clear_client_tingyun_id_secret(state) state.client_tingyun_id_secret = nil end |
#cross_app_enabled? ⇒ Boolean
45 46 47 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 45 def cross_app_enabled? TingYun::Agent::CrossAppTracing.cross_app_enabled? end |
#execute_duration(state) ⇒ Object
126 127 128 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 126 def execute_duration(state) state.web_duration - state.queue_duration - state.sql_duration - state.external_duration - state.rds_duration - state.mc_duration - state.mon_duration end |
#insert_response_header(state, response_headers) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 68 def insert_response_header(state, response_headers) if same_account?(state) txn = state.current_transaction unless txn.nil? set_response_headers(state, response_headers) end clear_client_tingyun_id_secret(state) 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 25 def register_event_listeners(events) TingYun::Agent.logger.debug("Wiring up Cross Application Tracing to events after finished configuring") events.subscribe(:before_call) do |env| #THREAD_LOCAL_ACCESS if cross_app_enabled? state = TingYun::Agent::TransactionState.tl_get save_referring_transaction_info(state, env) unless env[TY_ID_HEADER].nil? end end events.subscribe(:after_call) do |_status_code, headers, _body| #THREAD_LOCAL_ACCESS state = TingYun::Agent::TransactionState.tl_get state.queue_duration = state.current_transaction.queue_time * 1000 state.web_duration = (Time.now - state.current_transaction.start_time) * 1000 insert_response_header(state, headers) end end |
#same_account?(state) ⇒ Boolean
82 83 84 85 86 87 88 89 90 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 82 def same_account?(state) server_info = TingYun::Agent.config[:tingyunIdSecret].split('|') client_info = (state.client_tingyun_id_secret || '').split('|') if !server_info[0].nil? && server_info[0] == client_info[0] && !server_info[0].empty? return true else return false end end |
#save_referring_transaction_info(state, request) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 50 def save_referring_transaction_info(state,request) info = request[TY_ID_HEADER].split(';') tingyun_id_secret = info[0] client_transaction_id = info.find do |e| e.match(/x=/) end.split('=')[1] rescue nil client_req_id = info.find do |e| e.match(/r=/) end.split('=')[1] rescue nil state.client_tingyun_id_secret = tingyun_id_secret state.client_transaction_id = client_transaction_id state.transaction_sample_builder.trace.tx_id = client_transaction_id state.client_req_id = client_req_id end |
#set_response_headers(state, response_headers) ⇒ Object
92 93 94 95 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 92 def set_response_headers(state, 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 |
#slow_action_tracer?(state) ⇒ Boolean
118 119 120 121 122 123 124 |
# File 'lib/ting_yun/agent/cross_app/cross_app_monitor.rb', line 118 def slow_action_tracer?(state) if state.web_duration > TingYun::Agent.config[:'nbs.action_tracer.action_threshold'] return true else return false end end |