Class: NewRelic::Agent::CrossAppMonitor
- Inherits:
-
Object
- Object
- NewRelic::Agent::CrossAppMonitor
show all
- Includes:
- EncodingFunctions
- Defined in:
- lib/new_relic/agent/cross_app_monitor.rb
Defined Under Namespace
Modules: EncodingFunctions
Constant Summary
collapse
'X-NewRelic-ID'
'X-NewRelic-App-Data'
'X-NewRelic-Transaction'
%W{
#{NEWRELIC_TXN_HEADER} HTTP_X_NEWRELIC_TRANSACTION X_NEWRELIC_TRANSACTION
}
%W{
#{NEWRELIC_ID_HEADER} HTTP_X_NEWRELIC_ID X_NEWRELIC_ID
}
%w{Content-Length HTTP_CONTENT_LENGTH CONTENT_LENGTH}
- THREAD_ID_KEY =
Because we aren’t in the right spot when our transaction actually starts, hold client_cross_app_id we get thread local until then.
:newrelic_client_cross_app_id
- THREAD_TXN_KEY =
Same for the referring transaction guid
:newrelic_cross_app_referring_txn_info
Instance Method Summary
collapse
decode_with_key, encode_with_key, obfuscate_with_key
Constructor Details
Returns a new instance of CrossAppMonitor.
58
59
60
61
62
63
64
65
66
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 58
def initialize(events = nil)
events ||= Agent.instance.events
events.subscribe(:finished_configuring) do
register_event_listeners
end
end
|
Instance Method Details
#build_payload(timings, content_length) ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 169
def build_payload(timings, content_length)
transaction_name = timings.transaction_name.gsub(/["']/, "")
payload = [
NewRelic::Agent.config[:cross_process_id],
transaction_name,
timings.queue_time_in_seconds.to_f,
timings.app_time_in_seconds.to_f,
content_length,
transaction_guid()
]
key = NewRelic::Agent.config[:encoding_key]
payload = obfuscate_with_key( key, NewRelic.json_dump(payload) )
end
|
#clear_client_cross_app_id ⇒ Object
#clear_referring_transaction_info ⇒ Object
#client_cross_app_id ⇒ Object
#client_referring_transaction_guid ⇒ Object
124
125
126
127
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 124
def client_referring_transaction_guid
info = NewRelic::Agent::AgentThread.current[THREAD_TXN_KEY] or return nil
return info[0]
end
|
#client_referring_transaction_record_flag ⇒ Object
129
130
131
132
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 129
def client_referring_transaction_record_flag
info = NewRelic::Agent::AgentThread.current[THREAD_TXN_KEY] or return nil
return info[1]
end
|
#content_length_from_request(request) ⇒ Object
215
216
217
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 215
def content_length_from_request(request)
(request, CONTENT_LENGTH_HEADER_KEYS) || -1
end
|
#cross_app_enabled? ⇒ Boolean
#decoded_id(request) ⇒ Object
207
208
209
210
211
212
213
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 207
def decoded_id(request)
encoded_id = (request, NEWRELIC_ID_HEADER_KEYS)
return "" if encoded_id.nil?
key = NewRelic::Agent.config[:encoding_key]
decode_with_key( key, encoded_id )
end
|
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 134
def (, )
unless client_cross_app_id.nil?
timings = NewRelic::Agent::BrowserMonitoring.timings
content_length = content_length_from_request()
(, timings, content_length)
set_metrics(client_cross_app_id, timings)
clear_client_cross_app_id
end
end
|
#register_event_listeners ⇒ Object
Expected sequence of events:
:before_call will save our cross application request id to the thread
:start_transaction will get called when a transaction starts up
:after_call will write our response headers/metrics and clean up the thread
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 73
def register_event_listeners
NewRelic::Agent.logger.
debug("Wiring up Cross Application Tracing to events after finished configuring")
events = Agent.instance.events
events.subscribe(:before_call) do |env|
if should_process_request(env)
save_client_cross_app_id(env)
save_referring_transaction_info(env)
end
end
events.subscribe(:start_transaction) do |name|
set_transaction_custom_parameters
end
events.subscribe(:after_call) do |env, (status_code, , body)|
(env, )
end
events.subscribe(:notice_error) do |_, options|
set_error_custom_parameters(options)
end
end
|
#save_client_cross_app_id(request_headers) ⇒ Object
#save_referring_transaction_info(request_headers) ⇒ Object
#set_error_custom_parameters(options) ⇒ Object
197
198
199
200
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 197
def set_error_custom_parameters(options)
options[:client_cross_process_id] = client_cross_app_id() if client_cross_app_id()
end
|
#set_metrics(id, timings) ⇒ Object
202
203
204
205
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 202
def set_metrics(id, timings)
metric = NewRelic::Agent.instance.stats_engine.get_stats_no_scope("ClientApplication/#{id}/all")
metric.record_data_point(timings.app_time_in_seconds)
end
|
165
166
167
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 165
def (, timings, content_length)
[NEWRELIC_APPDATA_HEADER] = build_payload(timings, content_length)
end
|
#set_transaction_custom_parameters ⇒ Object
187
188
189
190
191
192
193
194
195
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 187
def set_transaction_custom_parameters
NewRelic::Agent.add_custom_parameters(:client_cross_process_id => client_cross_app_id()) if client_cross_app_id()
NewRelic::Agent.add_custom_parameters(:referring_transaction_guid => client_referring_transaction_guid()) if
client_referring_transaction_guid()
NewRelic::Agent.logger.debug "Referring transaction guid: %p" % [client_referring_transaction_guid()]
end
|
#should_process_request(request_headers) ⇒ Object
146
147
148
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 146
def should_process_request()
return cross_app_enabled? && trusts?()
end
|
#transaction_guid ⇒ Object
#trusts?(request) ⇒ Boolean
Expects an ID of format “12#345”, and will only accept that!
157
158
159
160
161
162
163
|
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 157
def trusts?(request)
id = decoded_id(request)
split_id = id.match(/(\d+)#\d+/)
return false if split_id.nil?
NewRelic::Agent.config[:trusted_account_ids].include?(split_id.captures.first.to_i)
end
|