Module: TingYun::Agent::CrossAppTracing
- Extended by:
- Instrumentation::Support::ExternalError
- Defined in:
- lib/ting_yun/agent/cross_app/cross_app_tracing.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- TY_ID_HEADER =
The cross app id header for “outgoing” calls
'X-Tingyun-Id'.freeze
- TY_DATA_HEADER =
'X-Tingyun-Tx-Data'.freeze
Class Method Summary collapse
- .add_transaction_trace_info(request, response) ⇒ Object
- .common_metrics(request) ⇒ Object
- .cross_app_enabled? ⇒ Boolean
- .cross_application_tracer_enabled? ⇒ Boolean
- .finish_trace(state, t0, node, request, response) ⇒ Object
-
.inject_request_headers(state, request) ⇒ Object
Inject the X-Process header into the outgoing
request. - .metrics_for(state, request, response) ⇒ Object
-
.metrics_for_cross_app_response(request, response) ⇒ Object
Return the set of metric objects appropriate for the given cross app
response. - .metrics_for_regular_request(request) ⇒ Object
-
.response_is_cross_app?(response) ⇒ Boolean
Returns
trueif Cross Application Tracing is enabled, and the givenresponsehas the appropriate headers. - .start_trace(state, t0, request) ⇒ Object
- .stats_engine ⇒ Object
- .tl_trace_http_request(request) ⇒ Object
- .transaction_sampler ⇒ Object
- .valid_tingyun_secret_id? ⇒ Boolean
- .web_action_tracer_enabled? ⇒ Boolean
Methods included from Instrumentation::Support::ExternalError
capture_exception, handle_error, set_attributes
Class Method Details
.add_transaction_trace_info(request, response) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 90 def add_transaction_trace_info(request, response) state = TingYun::Agent::TransactionState.tl_get filtered_uri = TingYun::Agent::HTTPClients::URIUtil.filter_uri request.uri transaction_sampler.add_node_info(:uri => filtered_uri) if response && response_is_cross_app?( response ) transaction_sampler.tl_builder.current_node[:txId] = state.client_transaction_id || state.request_guid my_data = TingYun::Support::Serialize::JSONWrapper.load response[TY_DATA_HEADER].gsub("'",'"') transaction_sampler.tl_builder.current_node[:txData] = my_data end end |
.common_metrics(request) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 120 def common_metrics(request) metrics = [ "External/NULL/ALL" ] if TingYun::Agent::Transaction.recording_web_transaction? metrics << "External/NULL/AllWeb" else metrics << "External/NULL/AllBackground" end return metrics end |
.cross_app_enabled? ⇒ Boolean
150 151 152 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 150 def cross_app_enabled? valid_tingyun_secret_id? && web_action_tracer_enabled? && cross_application_tracer_enabled? end |
.cross_application_tracer_enabled? ⇒ Boolean
158 159 160 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 158 def cross_application_tracer_enabled? TingYun::Agent.config[:'nbs.transaction_tracer.enabled'] end |
.finish_trace(state, t0, node, request, response) ⇒ Object
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 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 59 def finish_trace(state, t0, node, request, response) t1 = Time.now.to_f duration = (t1- t0) * 1000 state.external_duration = duration begin if request metrics = metrics_for(state, request, response) node_name = metrics.pop scoped_metric = metrics.pop stats_engine.record_scoped_and_unscoped_metrics(state, scoped_metric, metrics, duration) if node node.name = node_name add_transaction_trace_info(request, response) end end ensure if node stack = state.traced_method_stack stack.pop_frame(state, node, node_name, t1) end end rescue => err TingYun::Agent.logger.error "Uncaught exception while finishing an HTTP request trace", err raise end |
.inject_request_headers(state, request) ⇒ Object
Inject the X-Process header into the outgoing request.
167 168 169 170 171 172 173 174 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 167 def inject_request_headers(state, request) cross_app_id = TingYun::Agent.config[:tingyunIdSecret] or raise TingYun::Agent::CrossAppTracing::Error, "no tingyunIdSecret configured" txn_guid = state.client_transaction_id || state.request_guid state.transaction_sample_builder.trace.tx_id = txn_guid request[TY_ID_HEADER] = "#{cross_app_id};c=1;x=#{txn_guid}" end |
.metrics_for(state, request, response) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 101 def metrics_for(state, request, response) metrics = common_metrics(request) if response && response_is_cross_app?( response ) begin metrics.concat metrics_for_cross_app_response( request, response ) rescue => err # Fall back to regular metrics if there's a problem with x-process metrics TingYun::Agent.logger.debug "%p while fetching x-process metrics: %s" % [ err.class, err. ] metrics.concat metrics_for_regular_request( request ) end else metrics.concat metrics_for_regular_request( request ) end return metrics end |
.metrics_for_cross_app_response(request, response) ⇒ Object
Return the set of metric objects appropriate for the given cross app response.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 188 def metrics_for_cross_app_response(request, response ) state = TingYun::Agent::TransactionState.tl_get my_data = TingYun::Support::Serialize::JSONWrapper.load response[TY_DATA_HEADER].gsub("'",'"') uri = "#{request.uri.to_s.gsub('/','%2F')}/net%2Fhttp" metrics = [] metrics << "cross_app;#{my_data["id"]};#{my_data["action"]};#{uri}" metrics << "External/#{my_data["action"]}:#{uri}" return metrics end |
.metrics_for_regular_request(request) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 132 def metrics_for_regular_request( request ) state = TingYun::Agent::TransactionState.tl_get metrics = [] metrics << "External/#{request.uri.to_s.gsub('/','%2F')}/net%2Fhttp" metrics << "External/#{request.uri.to_s.gsub('/','%2F')}/net%2Fhttp" return metrics end |
.response_is_cross_app?(response) ⇒ Boolean
Returns true if Cross Application Tracing is enabled, and the given response has the appropriate headers.
178 179 180 181 182 183 184 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 178 def response_is_cross_app?( response ) return false unless cross_app_enabled? unless response[TY_DATA_HEADER] return false end return true end |
.start_trace(state, t0, request) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 51 def start_trace(state, t0, request) inject_request_headers(state, request) if cross_app_enabled? stack = state.traced_method_stack node = stack.push_frame(state,:http_request,t0) return node end |
.stats_engine ⇒ Object
141 142 143 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 141 def stats_engine ::TingYun::Agent.instance.stats_engine end |
.tl_trace_http_request(request) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 31 def tl_trace_http_request(request) t0 = Time.now.to_f state = TingYun::Agent::TransactionState.tl_get return yield unless state.execution_traced? return yield unless state.current_transaction #如果还没有创建Transaction,就发生跨应用,就直接先跳过跟踪。 begin node = start_trace(state, t0, request) response = yield capture_exception(response,request,'net%2Fhttp') rescue => e klass = "External/#{request.uri.to_s.gsub('/','%2F')}/net%2Fhttp" handle_error(e,klass) raise e ensure finish_trace(state, t0, node, request, response) end return response end |
.transaction_sampler ⇒ Object
145 146 147 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 145 def transaction_sampler ::TingYun::Agent.instance.transaction_sampler end |
.valid_tingyun_secret_id? ⇒ Boolean
162 163 164 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 162 def valid_tingyun_secret_id? TingYun::Agent.config[:tingyunIdSecret] && TingYun::Agent.config[:tingyunIdSecret].size > 0 end |
.web_action_tracer_enabled? ⇒ Boolean
154 155 156 |
# File 'lib/ting_yun/agent/cross_app/cross_app_tracing.rb', line 154 def web_action_tracer_enabled? TingYun::Agent.config[:'nbs.action_tracer.enabled'] end |