Class: NewRelic::Agent::CrossAppMonitor

Inherits:
Object
  • Object
show all
Includes:
EncodingFunctions
Defined in:
lib/new_relic/agent/cross_app_monitor.rb

Defined Under Namespace

Modules: EncodingFunctions

Constant Summary collapse

NEWRELIC_ID_HEADER =
'X-NewRelic-ID'
NEWRELIC_APPDATA_HEADER =
'X-NewRelic-App-Data'
NEWRELIC_TXN_HEADER =
'X-NewRelic-Transaction'
NEWRELIC_TXN_HEADER_KEYS =
%W{
  #{NEWRELIC_TXN_HEADER} HTTP_X_NEWRELIC_TRANSACTION X_NEWRELIC_TRANSACTION
}
NEWRELIC_ID_HEADER_KEYS =
%W{
  #{NEWRELIC_ID_HEADER} HTTP_X_NEWRELIC_ID X_NEWRELIC_ID
}
CONTENT_LENGTH_HEADER_KEYS =
%w{Content-Length HTTP_CONTENT_LENGTH CONTENT_LENGTH}

Instance Method Summary collapse

Methods included from EncodingFunctions

decode_with_key, encode_with_key, obfuscate_with_key

Constructor Details

#initialize(events = nil) ⇒ CrossAppMonitor

Returns a new instance of CrossAppMonitor.



56
57
58
59
60
61
62
63
64
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 56

def initialize(events = nil)
  # When we're starting up for real in the agent, we get passed the events
  # Other spots can pull from the agent, during startup the agent doesn't exist yet!
  events ||= Agent.instance.events

  events.subscribe(:finished_configuring) do
    register_event_listeners
  end
end

Instance Method Details

#build_payload(timings, content_length) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 164

def build_payload(timings, content_length)
  payload = [
    NewRelic::Agent.config[:cross_process_id],
    timings.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_idObject



100
101
102
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 100

def clear_client_cross_app_id
  TransactionState.get.client_cross_app_id = nil
end

#client_cross_app_idObject



104
105
106
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 104

def client_cross_app_id
  TransactionState.get.client_cross_app_id
end

#client_referring_transaction_guidObject



118
119
120
121
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 118

def client_referring_transaction_guid
  info = TransactionState.get.referring_transaction_info or return nil
  return info[0]
end

#client_referring_transaction_record_flagObject



123
124
125
126
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 123

def client_referring_transaction_record_flag
  info = TransactionState.get.referring_transaction_info or return nil
  return info[1]
end

#content_length_from_request(request) ⇒ Object



207
208
209
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 207

def content_length_from_request(request)
  from_headers(request, CONTENT_LENGTH_HEADER_KEYS) || -1
end

#cross_app_enabled?Boolean

Returns:

  • (Boolean)


145
146
147
148
149
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 145

def cross_app_enabled?
  NewRelic::Agent.config[:cross_process_id] &&
    (NewRelic::Agent.config[:"cross_application_tracer.enabled"] ||
     NewRelic::Agent.config[:cross_application_tracing])
end

#decoded_id(request) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 199

def decoded_id(request)
  encoded_id = from_headers(request, NEWRELIC_ID_HEADER_KEYS)
  return "" if encoded_id.nil?

  key = NewRelic::Agent.config[:encoding_key]
  decode_with_key( key, encoded_id )
end

#insert_response_header(request_headers, response_headers) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 128

def insert_response_header(request_headers, response_headers)
  unless client_cross_app_id.nil?
    NewRelic::Agent::Transaction.freeze_name
    timings = NewRelic::Agent::TransactionState.get.timings
    content_length = content_length_from_request(request_headers)

    set_response_headers(response_headers, timings, content_length)
    set_metrics(client_cross_app_id, timings)

    clear_client_cross_app_id
  end
end

#register_event_listenersObject

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


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 71

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
    set_transaction_custom_parameters
  end

  events.subscribe(:after_call) do |env, (status_code, headers, body)|
    insert_response_header(env, headers)
  end

  events.subscribe(:notice_error) do |_, options|
    set_error_custom_parameters(options)
  end
end

#save_client_cross_app_id(request_headers) ⇒ Object



96
97
98
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 96

def save_client_cross_app_id(request_headers)
  TransactionState.get.client_cross_app_id = decoded_id(request_headers)
end

#save_referring_transaction_info(request_headers) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 108

def save_referring_transaction_info(request_headers)
  key = NewRelic::Agent.config[:encoding_key]
  txn_header = from_headers( request_headers, NEWRELIC_TXN_HEADER_KEYS ) or return
  txn_header = decode_with_key( key, txn_header )
  txn_info = NewRelic.json_load( txn_header )
  NewRelic::Agent.logger.debug "Referring txn_info: %p" % [ txn_info ]

  TransactionState.get.referring_transaction_info = txn_info
end

#set_error_custom_parameters(options) ⇒ Object



189
190
191
192
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 189

def set_error_custom_parameters(options)
  options[:client_cross_process_id] = client_cross_app_id() if client_cross_app_id()
  # [MG] TODO: Should the CAT metrics be set here too?
end

#set_metrics(id, timings) ⇒ Object



194
195
196
197
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 194

def set_metrics(id, timings)
  metric_name = "ClientApplication/#{id}/all"
  NewRelic::Agent.record_metric(metric_name, timings.app_time_in_seconds)
end

#set_response_headers(response_headers, timings, content_length) ⇒ Object



160
161
162
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 160

def set_response_headers(response_headers, timings, content_length)
  response_headers[NEWRELIC_APPDATA_HEADER] = build_payload(timings, content_length)
end

#set_transaction_custom_parametersObject



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 177

def set_transaction_custom_parameters
  # We expect to get the before call to set the id (if we have it) before
  # this, and then write our custom parameter when the transaction starts
  NewRelic::Agent.add_custom_parameters(:client_cross_process_id => client_cross_app_id()) if client_cross_app_id()

  referring_guid = client_referring_transaction_guid()
  if referring_guid
    NewRelic::Agent.logger.debug "Referring transaction guid: %p" % [referring_guid]
    NewRelic::Agent.add_custom_parameters(:referring_transaction_guid => referring_guid)
  end
end

#should_process_request(request_headers) ⇒ Object



141
142
143
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 141

def should_process_request(request_headers)
  return cross_app_enabled? && trusts?(request_headers)
end

#transaction_guidObject



211
212
213
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 211

def transaction_guid
  NewRelic::Agent::TransactionState.get.request_guid
end

#trusts?(request) ⇒ Boolean

Expects an ID of format “12#345”, and will only accept that!

Returns:

  • (Boolean)


152
153
154
155
156
157
158
# File 'lib/new_relic/agent/cross_app_monitor.rb', line 152

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