-
Notifications
You must be signed in to change notification settings - Fork 604
/
Copy pathtracer.rb
509 lines (446 loc) · 17.5 KB
/
tracer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
require 'new_relic/agent/transaction'
require 'new_relic/agent/transaction/segment'
require 'new_relic/agent/transaction/datastore_segment'
require 'new_relic/agent/transaction/external_request_segment'
require 'new_relic/agent/transaction/message_broker_segment'
module NewRelic
module Agent
#
# This class helps you interact with the current transaction (if
# it exists), start new transactions/segments, etc.
#
# @api public
class Tracer
class << self
def state
state_for(Thread.current)
end
alias_method :tl_get, :state
# Returns +true+ unless called from within an
# +NewRelic::Agent.disable_all_tracing+ block.
#
# @api public
def tracing_enabled?
state.tracing_enabled?
end
# Returns the transaction in progress for this thread, or
# +nil+ if none exists.
#
# @api public
def current_transaction
state.current_transaction
end
# Returns the trace_id of the current_transaction, or +nil+ if
# none exists.
#
# @api public
def current_trace_id
if txn = current_transaction
txn.trace_id
end
end
alias_method :trace_id, :current_trace_id
# Returns the id of the current span, or +nil+ if none exists.
#
# @api public
def current_span_id
if span = current_segment
span.guid
end
end
alias_method :span_id, :current_span_id
# Returns a boolean indicating whether the current_transaction
# is sampled, or +nil+ if there is no current transaction.
#
# @api public
def transaction_sampled?
if txn = current_transaction
txn.sampled?
end
end
alias_method :sampled?, :transaction_sampled?
# Runs the given block of code in a transaction.
#
# @param [String] name reserved for New Relic internal use
#
# @param [String] partial_name a meaningful name for this
# transaction (e.g., +blogs/index+); the Ruby agent will add a
# New-Relic-specific prefix
#
# @param [Symbol] category +:web+ for web transactions or
# +:background+ for background transactions
#
# @param [Hash] options reserved for New Relic internal use
#
# @api public
def in_transaction(name: nil,
partial_name: nil,
category: nil,
options: {})
finishable = start_transaction_or_segment(
name: name,
partial_name: partial_name,
category: category,
options: options
)
begin
# We shouldn't raise from Tracer.start_transaction_or_segment, but
# only wrap the yield to be absolutely sure we don't report agent
# problems as app errors
yield
rescue => exception
current_transaction.notice_error(exception)
raise
ensure
finishable.finish if finishable
end
end
# Starts a segment on the current transaction (if one exists)
# or starts a new transaction otherwise.
#
# @param [String] name reserved for New Relic internal use
#
# @param [String] partial_name a meaningful name for this
# transaction (e.g., +blogs/index+); the Ruby agent will add a
# New-Relic-specific prefix
#
# @param [Symbol] category +:web+ for web transactions or
# +:task+ for background transactions
#
# @param [Hash] options reserved for New Relic internal use
#
# @return [Object, #finish] an object that responds to
# +finish+; you _must_ call +finish+ on it at the end of the
# code you're tracing
#
# @api public
def start_transaction_or_segment(name: nil,
partial_name: nil,
category: nil,
options: {})
if name.nil? && partial_name.nil?
raise ArgumentError, 'missing required argument: name or partial_name'
end
if category.nil?
raise ArgumentError, 'missing required argument: category'
end
if name
options[:transaction_name] = name
else
options[:transaction_name] = Transaction.name_from_partial(
partial_name,
category
)
end
if (txn = current_transaction)
txn.create_nested_segment(category, options)
else
Transaction.start_new_transaction(state, category, options)
end
rescue ArgumentError
raise
rescue => exception
log_error('start_transaction_or_segment', exception)
end
# Takes name or partial_name and a category.
# Returns a transaction instance or nil
def start_transaction(name: nil,
partial_name: nil,
category: nil,
**options)
if name.nil? && partial_name.nil?
raise ArgumentError, 'missing required argument: name or partial_name'
end
if category.nil?
raise ArgumentError, 'missing required argument: category'
end
return current_transaction if current_transaction
if name
options[:transaction_name] = name
else
options[:transaction_name] = Transaction.name_from_partial(
partial_name,
category
)
end
Transaction.start_new_transaction(state,
category,
options)
rescue ArgumentError
raise
rescue => exception
log_error('start_transaction', exception)
end
def create_distributed_trace_payload
return unless txn = current_transaction
txn.distributed_tracer.create_distributed_trace_payload
end
def accept_distributed_trace_payload(payload)
return unless txn = current_transaction
txn.distributed_tracer.accept_distributed_trace_payload(payload)
end
# Returns the currently active segment in the transaction in
# progress for this thread, or +nil+ if no segment or
# transaction exists.
#
# @api public
def current_segment
return unless txn = current_transaction
txn.current_segment
end
# Creates and starts a general-purpose segment used to time
# arbitrary code.
#
# @param [String] name full name of the segment; the agent
# will not add a prefix. Third-party users should begin the
# name with +Custom/+; e.g.,
# +Custom/UserMailer/send_welcome_email+
#
# @param [optional, String, Array] unscoped_metrics additional
# unscoped metrics to record using this segment's timing
# information
#
# @param start_time [optional, Time] a +Time+ instance
# denoting the start time of the segment. Value is set by
# AbstractSegment#start if not given.
#
# @param parent [optional, Segment] Use for the rare cases
# (such as async) where the parent segment should be something
# other than the current segment
#
# @return [Segment] the newly created segment; you _must_ call
# +finish+ on it at the end of the code you're tracing
#
# @api public
def start_segment(name:nil,
unscoped_metrics:nil,
start_time: nil,
parent: nil)
# ruby 2.0.0 does not support required kwargs
raise ArgumentError, 'missing required argument: name' if name.nil?
segment = Transaction::Segment.new name, unscoped_metrics, start_time
start_and_add_segment segment, parent
rescue ArgumentError
raise
rescue => exception
log_error('start_segment', exception)
end
UNKNOWN = "Unknown".freeze
OTHER = "other".freeze
# Creates and starts a datastore segment used to time
# datastore operations.
#
# @param [String] product the datastore name for use in metric
# naming, e.g. "FauxDB"
#
# @param [String] operation the name of the operation
# (e.g. "select"), often named after the method that's being
# instrumented.
#
# @param [optional, String] collection the collection name for use in
# statement-level metrics (i.e. table or model name)
#
# @param [optional, String] host the host this database
# instance is running on
#
# @param [optional, String] port_path_or_id TCP port, file
# path, UNIX domain socket, or other connection-related info
#
# @param [optional, String] database_name the name of this
# database
#
# @param start_time [optional, Time] a +Time+ instance
# denoting the start time of the segment. Value is set by
# AbstractSegment#start if not given.
#
# @param parent [optional, Segment] Use for the rare cases
# (such as async) where the parent segment should be something
# other than the current segment
#
# @return [DatastoreSegment] the newly created segment; you
# _must_ call +finish+ on it at the end of the code you're
# tracing
#
# @api public
def start_datastore_segment(product: nil,
operation: nil,
collection: nil,
host: nil,
port_path_or_id: nil,
database_name: nil,
start_time: nil,
parent: nil)
product ||= UNKNOWN
operation ||= OTHER
segment = Transaction::DatastoreSegment.new product, operation, collection, host, port_path_or_id, database_name
start_and_add_segment segment, parent
rescue ArgumentError
raise
rescue => exception
log_error('start_datastore_segment', exception)
end
# Creates and starts an external request segment using the
# given library, URI, and procedure. This is used to time
# external calls made over HTTP.
#
# @param [String] library a string of the class name of the library used to
# make the external call, for example, 'Net::HTTP'.
#
# @param [String, URI] uri indicates the URI to which the
# external request is being made. The URI should begin with the protocol,
# for example, 'https://github.com'.
#
# @param [String] procedure the HTTP method being used for the external
# request as a string, for example, 'GET'.
#
# @param start_time [optional, Time] a +Time+ instance
# denoting the start time of the segment. Value is set by
# AbstractSegment#start if not given.
#
# @param parent [optional, Segment] Use for the rare cases
# (such as async) where the parent segment should be something
# other than the current segment
#
# @return [ExternalRequestSegment] the newly created segment;
# you _must_ call +finish+ on it at the end of the code
# you're tracing
#
# @api public
def start_external_request_segment(library: nil,
uri: nil,
procedure: nil,
start_time: nil,
parent: nil)
# ruby 2.0.0 does not support required kwargs
raise ArgumentError, 'missing required argument: library' if library.nil?
raise ArgumentError, 'missing required argument: uri' if uri.nil?
raise ArgumentError, 'missing required argument: procedure' if procedure.nil?
segment = Transaction::ExternalRequestSegment.new library, uri, procedure, start_time
start_and_add_segment segment, parent
rescue ArgumentError
raise
rescue => exception
log_error('start_external_request_segment', exception)
end
# Will potentially capture and notice an error at the
# segment that was executing when error occurred.
# if passed +segment+ is something that doesn't
# respond to +notice_segment_error+ then this method
# is effectively just a yield to the given &block
def capture_segment_error segment
return unless block_given?
yield
rescue => exception
if segment && segment.is_a?(Transaction::AbstractSegment)
segment.notice_error exception
end
raise
end
# For New Relic internal use only.
def start_message_broker_segment(action: nil,
library: nil,
destination_type: nil,
destination_name: nil,
headers: nil,
parameters: nil,
start_time: nil,
parent: nil)
# ruby 2.0.0 does not support required kwargs
raise ArgumentError, 'missing required argument: action' if action.nil?
raise ArgumentError, 'missing required argument: library' if library.nil?
raise ArgumentError, 'missing required argument: destination_type' if destination_type.nil?
raise ArgumentError, 'missing required argument: destination_name' if destination_name.nil?
segment = Transaction::MessageBrokerSegment.new(
action: action,
library: library,
destination_type: destination_type,
destination_name: destination_name,
headers: headers,
parameters: parameters,
start_time: start_time
)
start_and_add_segment segment, parent
rescue ArgumentError
raise
rescue => exception
log_error('start_datastore_segment', exception)
end
# This method should only be used by Tracer for access to the
# current thread's state or to provide read-only accessors for other threads
#
# If ever exposed, this requires additional synchronization
def state_for(thread)
state = thread[:newrelic_tracer_state]
if state.nil?
state = Tracer::State.new
thread[:newrelic_tracer_state] = state
end
state
end
alias_method :tl_state_for, :state_for
def clear_state
Thread.current[:newrelic_tracer_state] = nil
end
alias_method :tl_clear, :clear_state
private
def start_and_add_segment segment, parent = nil
tracer_state = state
if (txn = tracer_state.current_transaction) &&
tracer_state.tracing_enabled?
txn.add_segment segment, parent
else
segment.record_metrics = false
end
segment.start
segment
end
def log_error(method_name, exception)
NewRelic::Agent.logger.error("Exception during Tracer.#{method_name}", exception)
nil
end
end
# This is THE location to store thread local information during a transaction
# Need a new piece of data? Add a method here, NOT a new thread local variable.
class State
def initialize
@untraced = []
@current_transaction = nil
@record_sql = nil
end
# This starts the timer for the transaction.
def reset(transaction=nil)
# We purposefully don't reset @untraced or @record_sql
# since those are managed by NewRelic::Agent.disable_* calls explicitly
# and (more importantly) outside the scope of a transaction
@current_transaction = transaction
@sql_sampler_transaction_data = nil
end
# Current transaction stack
attr_reader :current_transaction
# Execution tracing on current thread
attr_accessor :untraced
def push_traced(should_trace)
@untraced << should_trace
end
def pop_traced
@untraced.pop if @untraced
end
def is_execution_traced?
@untraced.nil? || @untraced.last != false
end
alias_method :tracing_enabled?, :is_execution_traced?
# TT's and SQL
attr_accessor :record_sql
def is_sql_recorded?
@record_sql != false
end
# Sql Sampler Transaction Data
attr_accessor :sql_sampler_transaction_data
end
end
TransactionState = Tracer
end
end