newrelic_rpm 6.6.0.358 → 6.11.0.365

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +81 -5
  4. data/CHANGELOG.md +262 -0
  5. data/Gemfile +6 -2
  6. data/Guardfile +18 -1
  7. data/LICENSE +1 -1
  8. data/Rakefile +2 -0
  9. data/lib/new_relic/agent.rb +89 -7
  10. data/lib/new_relic/agent/agent.rb +115 -58
  11. data/lib/new_relic/agent/agent_logger.rb +4 -0
  12. data/lib/new_relic/agent/attribute_filter.rb +7 -7
  13. data/lib/new_relic/agent/attributes.rb +150 -0
  14. data/lib/new_relic/agent/autostart.rb +19 -14
  15. data/lib/new_relic/agent/configuration/default_source.rb +154 -9
  16. data/lib/new_relic/agent/configuration/event_harvest_config.rb +11 -5
  17. data/lib/new_relic/agent/configuration/manager.rb +0 -8
  18. data/lib/new_relic/agent/configuration/server_source.rb +3 -2
  19. data/lib/new_relic/agent/configuration/yaml_source.rb +11 -6
  20. data/lib/new_relic/agent/connect/request_builder.rb +5 -13
  21. data/lib/new_relic/agent/database.rb +1 -2
  22. data/lib/new_relic/agent/database/obfuscation_helpers.rb +1 -1
  23. data/lib/new_relic/agent/datastores/mongo.rb +1 -1
  24. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +2 -2
  25. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +8 -8
  26. data/lib/new_relic/agent/distributed_tracing.rb +155 -6
  27. data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +2 -1
  28. data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +60 -45
  29. data/lib/new_relic/agent/distributed_tracing/distributed_trace_intrinsics.rb +80 -0
  30. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  31. data/lib/new_relic/agent/{distributed_trace_payload.rb → distributed_tracing/distributed_trace_payload.rb} +24 -101
  32. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
  33. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
  34. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +126 -0
  35. data/lib/new_relic/agent/error_collector.rb +33 -16
  36. data/lib/new_relic/agent/error_event_aggregator.rb +7 -5
  37. data/lib/new_relic/agent/external.rb +7 -7
  38. data/lib/new_relic/agent/guid_generator.rb +28 -0
  39. data/lib/new_relic/agent/hostname.rb +7 -1
  40. data/lib/new_relic/agent/http_clients/abstract.rb +82 -0
  41. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +24 -19
  42. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +28 -13
  43. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +17 -21
  44. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +10 -11
  45. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +16 -4
  46. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +4 -6
  47. data/lib/new_relic/agent/http_clients/uri_util.rb +3 -2
  48. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +5 -7
  49. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +4 -0
  50. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +11 -2
  51. data/lib/new_relic/agent/instrumentation/active_record.rb +4 -2
  52. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +7 -2
  53. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +8 -4
  54. data/lib/new_relic/agent/instrumentation/bunny.rb +45 -28
  55. data/lib/new_relic/agent/instrumentation/curb.rb +59 -18
  56. data/lib/new_relic/agent/instrumentation/data_mapper.rb +3 -1
  57. data/lib/new_relic/agent/instrumentation/excon.rb +1 -1
  58. data/lib/new_relic/agent/instrumentation/excon/connection.rb +6 -3
  59. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +2 -1
  60. data/lib/new_relic/agent/instrumentation/grape.rb +5 -10
  61. data/lib/new_relic/agent/instrumentation/http.rb +6 -3
  62. data/lib/new_relic/agent/instrumentation/httpclient.rb +5 -3
  63. data/lib/new_relic/agent/instrumentation/memcache.rb +3 -1
  64. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +6 -2
  65. data/lib/new_relic/agent/instrumentation/mongo.rb +9 -3
  66. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +13 -0
  67. data/lib/new_relic/agent/instrumentation/net.rb +6 -3
  68. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +25 -1
  69. data/lib/new_relic/agent/instrumentation/redis.rb +9 -3
  70. data/lib/new_relic/agent/instrumentation/resque.rb +3 -0
  71. data/lib/new_relic/agent/instrumentation/sidekiq.rb +47 -23
  72. data/lib/new_relic/agent/instrumentation/typhoeus.rb +23 -6
  73. data/lib/new_relic/agent/logging.rb +139 -0
  74. data/lib/new_relic/agent/messaging.rb +5 -73
  75. data/lib/new_relic/agent/method_tracer.rb +18 -6
  76. data/lib/new_relic/agent/method_tracer_helpers.rb +2 -2
  77. data/lib/new_relic/agent/monitors.rb +27 -0
  78. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
  79. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
  80. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +1 -1
  81. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +2 -4
  82. data/lib/new_relic/agent/new_relic_service.rb +7 -6
  83. data/lib/new_relic/agent/noticible_error.rb +22 -0
  84. data/lib/new_relic/agent/span_event_aggregator.rb +1 -0
  85. data/lib/new_relic/agent/span_event_primitive.rb +86 -53
  86. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  87. data/lib/new_relic/agent/supported_versions.rb +2 -2
  88. data/lib/new_relic/agent/system_info.rb +12 -3
  89. data/lib/new_relic/agent/tracer.rb +65 -18
  90. data/lib/new_relic/agent/transaction.rb +84 -79
  91. data/lib/new_relic/agent/transaction/abstract_segment.rb +28 -2
  92. data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
  93. data/lib/new_relic/agent/transaction/distributed_tracing.rb +57 -146
  94. data/lib/new_relic/agent/transaction/external_request_segment.rb +29 -36
  95. data/lib/new_relic/agent/transaction/message_broker_segment.rb +3 -11
  96. data/lib/new_relic/agent/transaction/segment.rb +7 -1
  97. data/lib/new_relic/agent/transaction/trace.rb +2 -4
  98. data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
  99. data/lib/new_relic/agent/transaction/trace_node.rb +10 -8
  100. data/lib/new_relic/agent/transaction_error_primitive.rb +10 -15
  101. data/lib/new_relic/agent/transaction_event_primitive.rb +28 -39
  102. data/lib/new_relic/cli/commands/deployments.rb +1 -1
  103. data/lib/new_relic/cli/commands/install.rb +3 -2
  104. data/lib/new_relic/coerce.rb +31 -6
  105. data/lib/new_relic/constants.rb +38 -0
  106. data/lib/new_relic/control/instance_methods.rb +10 -1
  107. data/lib/new_relic/dependency_detection.rb +4 -4
  108. data/lib/new_relic/environment_report.rb +5 -1
  109. data/lib/new_relic/noticed_error.rb +38 -17
  110. data/lib/new_relic/rack/browser_monitoring.rb +5 -0
  111. data/lib/new_relic/supportability_helper.rb +14 -0
  112. data/lib/new_relic/version.rb +1 -1
  113. data/lib/tasks/multiverse.rb +25 -0
  114. data/lib/tasks/tests.rake +6 -1
  115. data/newrelic_rpm.gemspec +19 -8
  116. data/test/agent_helper.rb +323 -71
  117. metadata +100 -33
  118. data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
  119. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -40
  120. data/lib/new_relic/agent/http_clients/abstract_request.rb +0 -31
  121. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  122. data/lib/tasks/versions.html.erb +0 -28
  123. data/lib/tasks/versions.postface.html +0 -8
  124. data/lib/tasks/versions.preface.html +0 -9
  125. data/lib/tasks/versions.rake +0 -65
  126. data/lib/tasks/versions.txt.erb +0 -14
@@ -1,17 +1,18 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+ # frozen_string_literal: true
4
5
 
5
6
  require 'new_relic/agent/instrumentation/queue_time'
6
7
  require 'new_relic/agent/transaction_metrics'
7
8
  require 'new_relic/agent/method_tracer_helpers'
8
- require 'new_relic/agent/transaction/attributes'
9
+ require 'new_relic/agent/attributes'
9
10
  require 'new_relic/agent/transaction/request_attributes'
10
11
  require 'new_relic/agent/transaction/tracing'
11
- require 'new_relic/agent/transaction/distributed_tracing'
12
- require 'new_relic/agent/cross_app_tracing'
12
+ require 'new_relic/agent/transaction/distributed_tracer'
13
13
  require 'new_relic/agent/transaction_time_aggregator'
14
14
  require 'new_relic/agent/deprecator'
15
+ require 'new_relic/agent/guid_generator'
15
16
 
16
17
  module NewRelic
17
18
  module Agent
@@ -21,26 +22,38 @@ module NewRelic
21
22
  # @api public
22
23
  class Transaction
23
24
  include Tracing
24
- include DistributedTracing
25
- include CrossAppTracing
26
25
 
27
26
  # for nested transactions
28
- SUBTRANSACTION_PREFIX = 'Nested/'.freeze
29
- CONTROLLER_PREFIX = 'Controller/'.freeze
30
- MIDDLEWARE_PREFIX = 'Middleware/Rack/'.freeze
31
- TASK_PREFIX = 'OtherTransaction/Background/'.freeze
32
- RAKE_PREFIX = 'OtherTransaction/Rake/'.freeze
33
- MESSAGE_PREFIX = 'OtherTransaction/Message/'.freeze
34
- RACK_PREFIX = 'Controller/Rack/'.freeze
35
- SINATRA_PREFIX = 'Controller/Sinatra/'.freeze
36
- GRAPE_PREFIX = 'Controller/Grape/'.freeze
37
- ACTION_CABLE_PREFIX = 'Controller/ActionCable/'.freeze
38
- OTHER_TRANSACTION_PREFIX = 'OtherTransaction/'.freeze
27
+ NESTED_TRANSACTION_PREFIX = "Nested/"
28
+ CONTROLLER_PREFIX = "Controller/"
29
+ MIDDLEWARE_PREFIX = "Middleware/Rack/"
30
+ OTHER_TRANSACTION_PREFIX = "OtherTransaction/"
31
+ TASK_PREFIX = "#{OTHER_TRANSACTION_PREFIX}Background/"
32
+ RAKE_PREFIX = "#{OTHER_TRANSACTION_PREFIX}Rake/"
33
+ MESSAGE_PREFIX = "#{OTHER_TRANSACTION_PREFIX}Message/"
34
+ RACK_PREFIX = "#{CONTROLLER_PREFIX}Rack/"
35
+ SINATRA_PREFIX = "#{CONTROLLER_PREFIX}Sinatra/"
36
+ GRAPE_PREFIX = "#{CONTROLLER_PREFIX}Grape/"
37
+ ACTION_CABLE_PREFIX = "#{CONTROLLER_PREFIX}ActionCable/"
39
38
 
40
39
  WEB_TRANSACTION_CATEGORIES = [:web, :controller, :uri, :rack, :sinatra, :grape, :middleware, :action_cable].freeze
41
40
  TRANSACTION_NAMING_SOURCES = [:child, :api].freeze
42
41
 
43
- MIDDLEWARE_SUMMARY_METRICS = ['Middleware/all'.freeze].freeze
42
+ MIDDLEWARE_SUMMARY_METRICS = ["Middleware/all"].freeze
43
+ WEB_SUMMARY_METRIC = "HttpDispatcher"
44
+ OTHER_SUMMARY_METRIC = "#{OTHER_TRANSACTION_PREFIX}all"
45
+ QUEUE_TIME_METRIC = "WebFrontend/QueueTime"
46
+
47
+ APDEX_S = "S"
48
+ APDEX_T = "T"
49
+ APDEX_F = "F"
50
+ APDEX_ALL_METRIC = "ApdexAll"
51
+ APDEX_METRIC = "Apdex"
52
+ APDEX_OTHER_METRIC = "ApdexOther"
53
+ APDEX_TXN_METRIC_PREFIX = "Apdex/"
54
+ APDEX_OTHER_TXN_METRIC_PREFIX = "ApdexOther/Transaction/"
55
+
56
+ JRUBY_CPU_TIME_ERROR = "Error calculating JRuby CPU Time"
44
57
 
45
58
  # reference to the transaction state managing this transaction
46
59
  attr_accessor :state
@@ -59,7 +72,8 @@ module NewRelic
59
72
  :process_cpu_start,
60
73
  :http_response_code,
61
74
  :response_content_length,
62
- :response_content_type
75
+ :response_content_type,
76
+ :parent_span_id
63
77
 
64
78
  attr_reader :guid,
65
79
  :metrics,
@@ -137,23 +151,22 @@ module NewRelic
137
151
 
138
152
  def self.nested_transaction_name(name)
139
153
  if name.start_with?(CONTROLLER_PREFIX) || name.start_with?(OTHER_TRANSACTION_PREFIX)
140
- "#{SUBTRANSACTION_PREFIX}#{name}"
154
+ "#{NESTED_TRANSACTION_PREFIX}#{name}"
141
155
  else
142
156
  name
143
157
  end
144
158
  end
145
159
 
146
- # Indicate that you don't want to keep the currently saved transaction
147
- # information
148
- def self.abort_transaction! #THREAD_LOCAL_ACCESS
149
- txn = Tracer.current_transaction
150
- txn.abort_transaction! if txn
160
+ # discards the currently saved transaction information
161
+ def self.abort_transaction!
162
+ if txn = Tracer.current_transaction
163
+ txn.abort_transaction!
164
+ end
151
165
  end
152
166
 
153
167
  # See NewRelic::Agent.notice_error for options and commentary
154
- def self.notice_error(e, options={}) #THREAD_LOCAL_ACCESS
155
- txn = Tracer.current_transaction
156
- if txn
168
+ def self.notice_error(e, options={})
169
+ if txn = Tracer.current_transaction
157
170
  txn.notice_error(e, options)
158
171
  elsif NewRelic::Agent.instance
159
172
  NewRelic::Agent.instance.error_collector.notice_error(e, options)
@@ -242,7 +255,7 @@ module NewRelic
242
255
 
243
256
  @exceptions = {}
244
257
  @metrics = TransactionMetrics.new
245
- @guid = generate_guid
258
+ @guid = NewRelic::Agent::GuidGenerator.generate_guid
246
259
 
247
260
  @ignore_this_transaction = false
248
261
  @ignore_apdex = options.fetch(:ignore_apdex, false)
@@ -265,6 +278,10 @@ module NewRelic
265
278
  end
266
279
  end
267
280
 
281
+ def distributed_tracer
282
+ @distributed_tracer ||= DistributedTracer.new(self)
283
+ end
284
+
268
285
  def sampled?
269
286
  return unless Agent.config[:'distributed_tracing.enabled']
270
287
  if @sampled.nil?
@@ -273,12 +290,16 @@ module NewRelic
273
290
  @sampled
274
291
  end
275
292
 
293
+ def trace_id
294
+ @trace_id ||= NewRelic::Agent::GuidGenerator.generate_guid 32
295
+ end
296
+
297
+ def trace_id=(value)
298
+ @trace_id = value
299
+ end
300
+
276
301
  def priority
277
- if @priority.nil?
278
- @priority = rand.round(6)
279
- @priority += 1 if sampled?
280
- end
281
- @priority
302
+ @priority ||= (sampled? ? 1.0 + rand : rand).round(NewRelic::PRIORITY_PRECISION)
282
303
  end
283
304
 
284
305
  def referer
@@ -349,8 +370,10 @@ module NewRelic
349
370
  end
350
371
 
351
372
  def best_name
352
- @frozen_name || @overridden_name ||
353
- @default_name || NewRelic::Agent::UNKNOWN_METRIC
373
+ @frozen_name ||
374
+ @overridden_name ||
375
+ @default_name ||
376
+ NewRelic::Agent::UNKNOWN_METRIC
354
377
  end
355
378
 
356
379
  # For common interface with Trace
@@ -437,9 +460,9 @@ module NewRelic
437
460
 
438
461
  nest_initial_segment if nesting_max_depth == 1
439
462
  nested_name = self.class.nested_transaction_name options[:transaction_name]
440
- result = create_segment nested_name
463
+ segment = create_segment nested_name
441
464
  set_default_transaction_name(options[:transaction_name], category)
442
- result
465
+ segment
443
466
  end
444
467
 
445
468
  def nest_initial_segment
@@ -453,9 +476,6 @@ module NewRelic
453
476
  @ignore_trace = true
454
477
  end
455
478
 
456
- WEB_SUMMARY_METRIC = 'HttpDispatcher'.freeze
457
- OTHER_SUMMARY_METRIC = 'OtherTransaction/all'.freeze
458
-
459
479
  def summary_metrics
460
480
  if @frozen_name.start_with?(CONTROLLER_PREFIX)
461
481
  [WEB_SUMMARY_METRIC]
@@ -523,8 +543,7 @@ module NewRelic
523
543
  record_total_time_metrics
524
544
  record_apdex unless ignore_apdex?
525
545
  record_queue_time
526
- record_cross_app_metrics
527
- record_distributed_tracing_metrics
546
+ distributed_tracer.record_metrics
528
547
 
529
548
  record_exceptions
530
549
  record_transaction_event
@@ -576,11 +595,7 @@ module NewRelic
576
595
  attributes.add_intrinsic_attribute(:synthetics_monitor_id, synthetics_monitor_id)
577
596
  end
578
597
 
579
- if Agent.config[:'distributed_tracing.enabled']
580
- assign_distributed_trace_intrinsics
581
- elsif is_cross_app?
582
- assign_cross_app_intrinsics
583
- end
598
+ distributed_tracer.assign_intrinsics
584
599
  end
585
600
 
586
601
  def calculate_gc_time
@@ -588,6 +603,16 @@ module NewRelic
588
603
  NewRelic::Agent::StatsEngine::GCProfiler.record_delta(gc_start_snapshot, gc_stop_snapshot)
589
604
  end
590
605
 
606
+ # This method returns transport_duration in seconds. Transport duration
607
+ # is stored in milliseconds on the payload, but it's needed in seconds
608
+ # for metrics and intrinsics.
609
+ def calculate_transport_duration distributed_trace_payload
610
+ return unless distributed_trace_payload
611
+
612
+ duration = (start_time.to_f * 1000 - distributed_trace_payload.timestamp) / 1000
613
+ duration < 0 ? 0 : duration
614
+ end
615
+
591
616
  # The summary metrics recorded by this method all end up with a duration
592
617
  # equal to the transaction itself, and an exclusive time of zero.
593
618
  def record_summary_metrics(outermost_node_name)
@@ -614,14 +639,13 @@ module NewRelic
614
639
  :priority => priority
615
640
  }
616
641
 
617
- append_cat_info(@payload)
618
- append_distributed_trace_info(@payload)
642
+ distributed_tracer.append_payload(@payload)
619
643
  append_apdex_perf_zone(@payload)
620
644
  append_synthetics_to(@payload)
621
645
  end
622
646
 
623
647
  def include_guid?
624
- is_cross_app? || is_synthetics_request?
648
+ distributed_tracer.is_cross_app? || is_synthetics_request?
625
649
  end
626
650
 
627
651
  def is_synthetics_request?
@@ -653,10 +677,6 @@ module NewRelic
653
677
  info[4]
654
678
  end
655
679
 
656
- APDEX_S = 'S'.freeze
657
- APDEX_T = 'T'.freeze
658
- APDEX_F = 'F'.freeze
659
-
660
680
  def append_apdex_perf_zone(payload)
661
681
  if recording_web_transaction?
662
682
  bucket = apdex_bucket(duration, apdex_t)
@@ -695,13 +715,21 @@ module NewRelic
695
715
  options[:metric] = best_name
696
716
  options[:attributes] = @attributes
697
717
 
698
- error_recorded = !!agent.error_collector.notice_error(exception, options) || error_recorded
718
+ span_id = options.delete :span_id
719
+ error_recorded = !!agent.error_collector.notice_error(exception, options, span_id) || error_recorded
699
720
  end
700
721
  payload[:error] = error_recorded if payload
701
722
  end
702
723
 
703
724
  # Do not call this. Invoke the class method instead.
704
725
  def notice_error(error, options={}) # :nodoc:
726
+
727
+ # Only the last error is kept
728
+ if @current_segment
729
+ @current_segment.notice_error error, expected: options[:expected]
730
+ options[:span_id] = @current_segment.guid
731
+ end
732
+
705
733
  if @exceptions[error]
706
734
  @exceptions[error].merge! options
707
735
  else
@@ -713,8 +741,6 @@ module NewRelic
713
741
  agent.transaction_event_recorder.record payload
714
742
  end
715
743
 
716
- QUEUE_TIME_METRIC = 'WebFrontend/QueueTime'.freeze
717
-
718
744
  def queue_time
719
745
  @apdex_start ? @start_time - @apdex_start : 0
720
746
  end
@@ -730,17 +756,9 @@ module NewRelic
730
756
  end
731
757
  end
732
758
 
733
- APDEX_ALL_METRIC = 'ApdexAll'.freeze
734
-
735
- APDEX_METRIC = 'Apdex'.freeze
736
- APDEX_OTHER_METRIC = 'ApdexOther'.freeze
737
-
738
- APDEX_TXN_METRIC_PREFIX = 'Apdex/'.freeze
739
- APDEX_OTHER_TXN_METRIC_PREFIX = 'ApdexOther/Transaction/'.freeze
740
-
741
759
  def had_error_affecting_apdex?
742
760
  @exceptions.each do |exception, options|
743
- ignored = NewRelic::Agent.instance.error_collector.error_is_ignored?(exception)
761
+ ignored = NewRelic::Agent.instance.error_collector.error_is_ignored?(exception)
744
762
  expected = options[:expected]
745
763
 
746
764
  return true unless ignored || expected
@@ -879,7 +897,6 @@ module NewRelic
879
897
  p.stime + p.utime
880
898
  end
881
899
 
882
- JRUBY_CPU_TIME_ERROR = "Error calculating JRuby CPU Time".freeze
883
900
  def jruby_cpu_time
884
901
  return nil unless @@java_classes_loaded
885
902
  threadMBean = Java::JavaLangManagement::ManagementFactory.getThreadMXBean()
@@ -905,18 +922,6 @@ module NewRelic
905
922
  def sql_sampler
906
923
  agent.sql_sampler
907
924
  end
908
-
909
- HEX_DIGITS = (0..15).map{|i| i.to_s(16)}
910
- GUID_LENGTH = 16
911
-
912
- # generate a random 64 bit uuid
913
- def generate_guid
914
- guid = ''
915
- GUID_LENGTH.times do |a|
916
- guid << HEX_DIGITS[rand(16)]
917
- end
918
- guid
919
- end
920
925
  end
921
926
  end
922
927
  end
@@ -3,7 +3,7 @@
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
5
  require 'new_relic/agent/range_extensions'
6
- require 'securerandom'
6
+ require 'new_relic/agent/guid_generator'
7
7
 
8
8
  module NewRelic
9
9
  module Agent
@@ -23,11 +23,12 @@ module NewRelic
23
23
  attr_reader :start_time, :end_time, :duration, :exclusive_duration, :guid
24
24
  attr_accessor :name, :parent, :children_time, :transaction
25
25
  attr_writer :record_metrics, :record_scoped_metric, :record_on_finish
26
+ attr_reader :noticed_error
26
27
 
27
28
  def initialize name=nil, start_time=nil
28
29
  @name = name
29
30
  @transaction = nil
30
- @guid = SecureRandom.hex(8)
31
+ @guid = NewRelic::Agent::GuidGenerator.generate_guid
31
32
  @parent = nil
32
33
  @params = nil
33
34
  @start_time = start_time if start_time
@@ -42,6 +43,7 @@ module NewRelic
42
43
  @record_metrics = true
43
44
  @record_scoped_metric = true
44
45
  @record_on_finish = false
46
+ @noticed_error = nil
45
47
  end
46
48
 
47
49
  def start
@@ -119,6 +121,30 @@ module NewRelic
119
121
  def transaction_assigned
120
122
  end
121
123
 
124
+ def set_noticed_error noticed_error
125
+ if @noticed_error
126
+ NewRelic::Agent.logger.debug \
127
+ "Segment: #{name} overwriting previously noticed " \
128
+ "error: #{@noticed_error.inspect} with: #{noticed_error.inspect}"
129
+ end
130
+ @noticed_error = noticed_error
131
+ end
132
+
133
+ def notice_error exception, options={}
134
+ if Agent.config[:high_security]
135
+ NewRelic::Agent.logger.debug \
136
+ "Segment: #{name} ignores notice_error for " \
137
+ "error: #{exception.inspect} because :high_security is enabled"
138
+ else
139
+ NewRelic::Agent.instance.error_collector.notice_segment_error self, exception, options
140
+ end
141
+ end
142
+
143
+ def noticed_error_attributes
144
+ return unless @noticed_error
145
+ @noticed_error.attributes_from_notice_error
146
+ end
147
+
122
148
  protected
123
149
 
124
150
  attr_writer :range_recorded
@@ -0,0 +1,171 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+ # frozen_string_literal: true
5
+
6
+ require 'new_relic/agent/transaction/trace_context'
7
+ require 'new_relic/agent/transaction/distributed_tracing'
8
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
9
+
10
+ module NewRelic
11
+ module Agent
12
+ class Transaction
13
+ class DistributedTracer
14
+ include NewRelic::Agent::CrossAppTracing
15
+ include DistributedTracing
16
+ include TraceContext
17
+
18
+ attr_reader :transaction
19
+ attr_accessor :parent_transaction_id
20
+
21
+ def parent_guid
22
+ if trace_context_header_data
23
+ trace_context_header_data.parent_id
24
+ elsif distributed_trace_payload
25
+ distributed_trace_payload.id
26
+ end
27
+ end
28
+
29
+ def accept_incoming_request request, transport_type=nil
30
+ accept_incoming_transport_type request, transport_type
31
+ if trace_parent_header_present? request
32
+ accept_trace_context_incoming_request request
33
+ else
34
+ accept_distributed_tracing_incoming_request request
35
+ end
36
+ end
37
+
38
+ def caller_transport_type
39
+ @caller_transport_type ||= "Unknown"
40
+ end
41
+
42
+ def accept_transport_type_from_api value
43
+ @caller_transport_type = DistributedTraceTransportType.from value
44
+ end
45
+
46
+ def accept_incoming_transport_type request, transport_type
47
+ if transport_type.to_s == NewRelic::EMPTY_STR
48
+ @caller_transport_type = DistributedTraceTransportType.for_rack_request request
49
+ else
50
+ @caller_transport_type = DistributedTraceTransportType.from transport_type
51
+ end
52
+ end
53
+
54
+ def initialize transaction
55
+ @transaction = transaction
56
+ end
57
+
58
+ def record_metrics
59
+ record_cross_app_metrics
60
+ DistributedTraceMetrics.record_metrics_for_transaction transaction
61
+ end
62
+
63
+ def append_payload payload
64
+ append_cat_info payload
65
+ DistributedTraceIntrinsics.copy_from_transaction \
66
+ transaction,
67
+ trace_state_payload || distributed_trace_payload,
68
+ payload
69
+ end
70
+
71
+ def insert_headers request
72
+ insert_trace_context_header request
73
+ insert_distributed_trace_header request
74
+ insert_cross_app_header request
75
+ end
76
+
77
+ def consume_message_headers headers, tracer_state, transport_type
78
+ consume_message_distributed_tracing_headers headers, transport_type
79
+ consume_message_cross_app_tracing_headers headers, tracer_state
80
+ consume_message_synthetics_headers headers
81
+ rescue => e
82
+ NewRelic::Agent.logger.error "Error in consume_message_headers", e
83
+ end
84
+
85
+ def assign_intrinsics
86
+ if Agent.config[:'distributed_tracing.enabled']
87
+ DistributedTraceIntrinsics.copy_to_attributes transaction.payload, transaction.attributes
88
+ elsif is_cross_app?
89
+ assign_cross_app_intrinsics
90
+ end
91
+ end
92
+
93
+ def insert_distributed_trace_header request
94
+ return unless Agent.config[:'distributed_tracing.enabled']
95
+ return if Agent.config[:'exclude_newrelic_header']
96
+ payload = create_distributed_trace_payload
97
+ request[NewRelic::NEWRELIC_KEY] = payload.http_safe if payload
98
+ end
99
+
100
+ def insert_cat_headers headers
101
+ return unless CrossAppTracing.cross_app_enabled?
102
+ @is_cross_app_caller = true
103
+ insert_message_headers headers,
104
+ transaction.guid,
105
+ cat_trip_id,
106
+ cat_path_hash,
107
+ transaction.raw_synthetics_header
108
+ end
109
+
110
+ private
111
+
112
+ def consume_message_synthetics_headers headers
113
+ synthetics_header = headers[CrossAppTracing::NR_MESSAGE_BROKER_SYNTHETICS_HEADER]
114
+ if synthetics_header and
115
+ incoming_payload = ::JSON.load(deobfuscate(synthetics_header)) and
116
+ SyntheticsMonitor.is_valid_payload?(incoming_payload) and
117
+ SyntheticsMonitor.is_supported_version?(incoming_payload) and
118
+ SyntheticsMonitor.is_trusted?(incoming_payload)
119
+
120
+ transaction.raw_synthetics_header = synthetics_header
121
+ transaction.synthetics_payload = incoming_payload
122
+ end
123
+ rescue => e
124
+ NewRelic::Agent.logger.error "Error in consume_message_synthetics_header", e
125
+ end
126
+
127
+ def consume_message_distributed_tracing_headers headers, transport_type
128
+ return unless Agent.config[:'distributed_tracing.enabled']
129
+
130
+ accept_incoming_transport_type headers, transport_type
131
+
132
+ newrelic_trace_key = NewRelic::CANDIDATE_NEWRELIC_KEYS.detect do |key|
133
+ headers.has_key?(key)
134
+ end
135
+ return unless newrelic_trace_key && (payload = headers[newrelic_trace_key])
136
+
137
+ accept_distributed_trace_payload payload
138
+ end
139
+
140
+ def consume_message_cross_app_tracing_headers headers, tracer_state
141
+ return unless CrossAppTracing.cross_app_enabled?
142
+ return unless CrossAppTracing.message_has_crossapp_request_header?(headers)
143
+
144
+ accept_cross_app_payload headers, tracer_state
145
+ CrossAppTracing.assign_intrinsic_transaction_attributes tracer_state
146
+ end
147
+
148
+ def accept_cross_app_payload headers, tracer_state
149
+ encoded_id = headers[CrossAppTracing::NR_MESSAGE_BROKER_ID_HEADER]
150
+ decoded_id = encoded_id.nil? ? EMPTY_STRING : deobfuscate(encoded_id)
151
+
152
+ return unless CrossAppTracing.trusted_valid_cross_app_id?(decoded_id)
153
+ txn_header = headers[CrossAppTracing::NR_MESSAGE_BROKER_TXN_HEADER]
154
+ txn_info = ::JSON.load(deobfuscate(txn_header))
155
+ payload = CrossAppPayload.new(decoded_id, transaction, txn_info)
156
+
157
+ @cross_app_payload = payload
158
+ rescue => e
159
+ NewRelic::Agent.logger.debug("Failure deserializing encoded header in #{self.class}, #{e.class}, #{e.message}")
160
+ nil
161
+ end
162
+
163
+ def deobfuscate message
164
+ CrossAppTracing.obfuscator.deobfuscate message
165
+ end
166
+
167
+ end
168
+ end
169
+ end
170
+ end
171
+