newrelic_rpm 7.0.0 → 8.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/CHANGELOG.md +226 -0
  4. data/CONTRIBUTING.md +13 -2
  5. data/README.md +5 -1
  6. data/lib/new_relic/agent/adaptive_sampler.rb +2 -2
  7. data/lib/new_relic/agent/agent.rb +8 -7
  8. data/lib/new_relic/agent/commands/thread_profiler_session.rb +7 -3
  9. data/lib/new_relic/agent/configuration/default_source.rb +229 -158
  10. data/lib/new_relic/agent/configuration/event_harvest_config.rb +28 -12
  11. data/lib/new_relic/agent/configuration/manager.rb +1 -2
  12. data/lib/new_relic/agent/configuration/server_source.rb +3 -2
  13. data/lib/new_relic/agent/configuration/yaml_source.rb +22 -1
  14. data/lib/new_relic/agent/connect/request_builder.rb +4 -2
  15. data/lib/new_relic/agent/custom_event_aggregator.rb +2 -1
  16. data/lib/new_relic/agent/database.rb +5 -2
  17. data/lib/new_relic/agent/datastores/mongo.rb +5 -10
  18. data/lib/new_relic/agent/datastores.rb +7 -7
  19. data/lib/new_relic/agent/distributed_tracing/cross_app_payload.rb +5 -5
  20. data/lib/new_relic/agent/distributed_tracing/cross_app_tracing.rb +10 -6
  21. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +0 -1
  22. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +1 -1
  23. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +6 -6
  24. data/lib/new_relic/agent/error_collector.rb +52 -37
  25. data/lib/new_relic/agent/error_filter.rb +175 -0
  26. data/lib/new_relic/agent/event_loop.rb +6 -6
  27. data/lib/new_relic/agent/external.rb +0 -32
  28. data/lib/new_relic/agent/http_clients/abstract.rb +2 -2
  29. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +7 -1
  30. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +1 -1
  31. data/lib/new_relic/agent/instrumentation/active_merchant.rb +3 -3
  32. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +1 -3
  33. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +1 -1
  34. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +2 -1
  35. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +4 -4
  36. data/lib/new_relic/agent/instrumentation/excon.rb +4 -23
  37. data/lib/new_relic/agent/instrumentation/logger/chain.rb +21 -0
  38. data/lib/new_relic/agent/instrumentation/logger/instrumentation.rb +59 -0
  39. data/lib/new_relic/agent/instrumentation/logger/prepend.rb +13 -0
  40. data/lib/new_relic/agent/instrumentation/logger.rb +25 -0
  41. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +5 -1
  42. data/lib/new_relic/agent/instrumentation/memcache/helper.rb +5 -0
  43. data/lib/new_relic/agent/instrumentation/memcache/prepend.rb +8 -2
  44. data/lib/new_relic/agent/instrumentation/mongo.rb +3 -141
  45. data/lib/new_relic/agent/instrumentation/net_http.rb +6 -1
  46. data/lib/new_relic/agent/instrumentation/padrino/chain.rb +23 -19
  47. data/lib/new_relic/agent/instrumentation/padrino/prepend.rb +1 -1
  48. data/lib/new_relic/agent/instrumentation/padrino.rb +4 -2
  49. data/lib/new_relic/agent/instrumentation/queue_time.rb +4 -4
  50. data/lib/new_relic/agent/instrumentation/rack/chain.rb +3 -2
  51. data/lib/new_relic/agent/instrumentation/rack/prepend.rb +2 -1
  52. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +5 -41
  53. data/lib/new_relic/agent/instrumentation/redis.rb +4 -0
  54. data/lib/new_relic/agent/instrumentation/resque/helper.rb +19 -0
  55. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +5 -3
  56. data/lib/new_relic/agent/instrumentation/resque.rb +13 -4
  57. data/lib/new_relic/agent/instrumentation/sidekiq.rb +6 -1
  58. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +8 -2
  59. data/lib/new_relic/agent/instrumentation/tilt/chain.rb +24 -0
  60. data/lib/new_relic/agent/instrumentation/tilt/instrumentation.rb +41 -0
  61. data/lib/new_relic/agent/instrumentation/tilt/prepend.rb +13 -0
  62. data/lib/new_relic/agent/instrumentation/tilt.rb +25 -0
  63. data/lib/new_relic/agent/javascript_instrumentor.rb +15 -10
  64. data/lib/new_relic/agent/logging.rb +5 -6
  65. data/lib/new_relic/agent/messaging.rb +10 -24
  66. data/lib/new_relic/agent/method_tracer.rb +137 -138
  67. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +8 -1
  68. data/lib/new_relic/agent/new_relic_service.rb +16 -12
  69. data/lib/new_relic/agent/pipe_channel_manager.rb +10 -6
  70. data/lib/new_relic/agent/pipe_service.rb +1 -1
  71. data/lib/new_relic/agent/samplers/cpu_sampler.rb +1 -1
  72. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +9 -6
  73. data/lib/new_relic/agent/span_event_aggregator.rb +2 -2
  74. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  75. data/lib/new_relic/agent/stats_engine/stats_hash.rb +1 -1
  76. data/lib/new_relic/agent/stats_engine.rb +1 -1
  77. data/lib/new_relic/agent/threading/backtrace_service.rb +4 -5
  78. data/lib/new_relic/agent/threading/thread_profile.rb +1 -1
  79. data/lib/new_relic/agent/tracer.rb +15 -37
  80. data/lib/new_relic/agent/transaction/abstract_segment.rb +3 -3
  81. data/lib/new_relic/agent/transaction/datastore_segment.rb +3 -0
  82. data/lib/new_relic/agent/transaction/distributed_tracer.rb +1 -0
  83. data/lib/new_relic/agent/transaction/external_request_segment.rb +3 -0
  84. data/lib/new_relic/agent/transaction/message_broker_segment.rb +5 -11
  85. data/lib/new_relic/agent/transaction/segment.rb +3 -0
  86. data/lib/new_relic/agent/transaction.rb +7 -28
  87. data/lib/new_relic/agent/transaction_time_aggregator.rb +5 -5
  88. data/lib/new_relic/agent/vm/snapshot.rb +1 -1
  89. data/lib/new_relic/agent/worker_loop.rb +5 -5
  90. data/lib/new_relic/agent.rb +10 -7
  91. data/lib/new_relic/cli/commands/deployments.rb +2 -2
  92. data/lib/new_relic/constants.rb +0 -4
  93. data/lib/new_relic/noticed_error.rb +4 -4
  94. data/lib/new_relic/version.rb +2 -2
  95. data/lib/newrelic_rpm.rb +10 -34
  96. data/lib/tasks/all.rb +1 -1
  97. data/lib/tasks/config.html.erb +14 -25
  98. data/lib/tasks/config.rake +8 -7
  99. data/newrelic.yml +580 -3
  100. data/newrelic_rpm.gemspec +1 -1
  101. data/test/agent_helper.rb +27 -2
  102. metadata +13 -11
  103. data/lib/new_relic/agent/datastores/mongo/statement_formatter.rb +0 -53
  104. data/lib/new_relic/agent/instrumentation/excon/connection.rb +0 -49
  105. data/lib/new_relic/agent/instrumentation/merb/controller.rb +0 -44
  106. data/lib/new_relic/agent/instrumentation/merb/errors.rb +0 -33
  107. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +0 -125
  108. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +0 -46
  109. data/lib/new_relic/agent/supported_versions.rb +0 -275
  110. data/lib/new_relic/control/frameworks/merb.rb +0 -29
@@ -2,30 +2,34 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
4
 
5
- module NewRelic::Agent::Instrumentation
6
- module Padrino
7
- module Chain
8
- def self.instrument!
9
- ::Padrino::Routing::InstanceMethods.module_eval do
10
- include NewRelic::Agent::Instrumentation::Sinatra
5
+ module NewRelic
6
+ module Agent
7
+ module Instrumentation
8
+ module PadrinoTracer
9
+ module Chain
10
+ def self.instrument!
11
+ ::Padrino::Routing::InstanceMethods.module_eval do
12
+ include NewRelic::Agent::Instrumentation::Sinatra
11
13
 
12
- def dispatch_with_newrelic
13
- dispatch_with_tracing { dispatch_without_newrelic }
14
- end
14
+ def dispatch_with_newrelic
15
+ dispatch_with_tracing { dispatch_without_newrelic }
16
+ end
15
17
 
16
- alias dispatch_without_newrelic dispatch!
17
- alias dispatch! dispatch_with_newrelic
18
+ alias dispatch_without_newrelic dispatch!
19
+ alias dispatch! dispatch_with_newrelic
18
20
 
19
- # Padrino 0.13 mustermann routing
20
- if private_method_defined?(:invoke_route)
21
- include NewRelic::Agent::Instrumentation::Padrino
21
+ # Padrino 0.13 mustermann routing
22
+ if private_method_defined?(:invoke_route)
23
+ include NewRelic::Agent::Instrumentation::Padrino
22
24
 
23
- def invoke_route_with_newrelic(*args, &block)
24
- invoke_route_with_tracing(*args) { invoke_route_without_newrelic(*args, &block) }
25
- end
25
+ def invoke_route_with_newrelic(*args, &block)
26
+ invoke_route_with_tracing(*args) { invoke_route_without_newrelic(*args, &block) }
27
+ end
26
28
 
27
- alias invoke_route_without_newrelic invoke_route
28
- alias invoke_route invoke_route_with_newrelic
29
+ alias invoke_route_without_newrelic invoke_route
30
+ alias invoke_route invoke_route_with_newrelic
31
+ end
32
+ end
29
33
  end
30
34
  end
31
35
  end
@@ -3,7 +3,7 @@
3
3
  # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
4
 
5
5
  module NewRelic::Agent::Instrumentation
6
- module Padrino
6
+ module PadrinoTracer
7
7
  module Prepend
8
8
  include NewRelic::Agent::Instrumentation::Sinatra
9
9
  include NewRelic::Agent::Instrumentation::Padrino
@@ -24,12 +24,14 @@ DependencyDetection.defer do
24
24
  executes do
25
25
  ::NewRelic::Agent.logger.info 'Installing Padrino instrumentation'
26
26
  if use_prepend?
27
- prepend_instrument ::Padrino::Routing::InstanceMethods, NewRelic::Agent::Instrumentation::Padrino::Prepend
27
+ prepend_instrument ::Padrino::Application, NewRelic::Agent::Instrumentation::PadrinoTracer::Prepend
28
28
  else
29
- chain_instrument NewRelic::Agent::Instrumentation::Padrino::Chain
29
+ chain_instrument NewRelic::Agent::Instrumentation::PadrinoTracer::Chain
30
30
  end
31
31
  end
32
32
  end
33
33
 
34
34
 
35
35
 
36
+
37
+
@@ -15,7 +15,7 @@ module NewRelic
15
15
  ALL_QUEUE_METRIC = 'WebFrontend/QueueTime'.freeze
16
16
  # any timestamps before this are thrown out and the parser
17
17
  # will try again with a larger unit (2000/1/1 UTC)
18
- EARLIEST_ACCEPTABLE_TIME = Time.at(946684800)
18
+ EARLIEST_ACCEPTABLE_TIME = Time.at(946684800).to_f
19
19
 
20
20
  CANDIDATE_HEADERS = [
21
21
  REQUEST_START_HEADER,
@@ -27,7 +27,7 @@ module NewRelic
27
27
 
28
28
  module_function
29
29
 
30
- def parse_frontend_timestamp(headers, now=Time.now)
30
+ def parse_frontend_timestamp(headers, now=Process.clock_gettime(Process::CLOCK_REALTIME))
31
31
  earliest = nil
32
32
 
33
33
  CANDIDATE_HEADERS.each do |header|
@@ -40,7 +40,7 @@ module NewRelic
40
40
  end
41
41
 
42
42
  if earliest && earliest > now
43
- NewRelic::Agent.logger.debug("Negative queue time detected, treating as zero: start=#{earliest.to_f} > now=#{now.to_f}")
43
+ NewRelic::Agent.logger.debug("Negative queue time detected, treating as zero: start=#{earliest.to_f} > now=#{now}")
44
44
  earliest = now
45
45
  end
46
46
 
@@ -59,7 +59,7 @@ module NewRelic
59
59
  def parse_timestamp(string)
60
60
  DIVISORS.each do |divisor|
61
61
  begin
62
- t = Time.at(string.to_f / divisor)
62
+ t = (string.to_f / divisor)
63
63
  return t if t > EARLIEST_ACCEPTABLE_TIME
64
64
  rescue RangeError
65
65
  # On Ruby versions built with a 32-bit time_t, attempting to
@@ -31,7 +31,8 @@ module NewRelic::Agent::Instrumentation
31
31
  def use_with_newrelic(middleware_class, *args, &block)
32
32
  use_with_tracing(middleware_class) { |wrapped_class| use_without_newrelic(wrapped_class, *args, &block) }
33
33
  end
34
-
34
+ ruby2_keywords(:use_with_newrelic) if respond_to?(:ruby2_keywords, true)
35
+
35
36
  alias_method :use_without_newrelic, :use
36
37
  alias_method :use, :use_with_newrelic
37
38
  end
@@ -54,4 +55,4 @@ module NewRelic::Agent::Instrumentation
54
55
  end
55
56
  end
56
57
  end
57
- end
58
+ end
@@ -31,6 +31,7 @@ module NewRelic::Agent::Instrumentation
31
31
  def use(middleware_class, *args, &blk)
32
32
  use_with_tracing(middleware_class) { |wrapped_class| super(wrapped_class, *args, &blk) }
33
33
  end
34
+ ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)
34
35
  end
35
36
  end
36
- end
37
+ end
@@ -84,52 +84,16 @@ DependencyDetection.defer do
84
84
  end
85
85
  end
86
86
  end
87
- DependencyDetection.defer do
88
- @name = :rails30_view
89
-
90
- depends_on do
91
- defined?(::Rails::VERSION::MAJOR) && defined?(::Rails::VERSION::MINOR) &&
92
- ::Rails::VERSION::MAJOR.to_i == 3 && ::Rails::VERSION::MINOR.to_i == 0
93
- end
94
-
95
- depends_on do
96
- !NewRelic::Agent.config[:disable_view_instrumentation]
97
- end
98
-
99
- executes do
100
- ::NewRelic::Agent.logger.info 'Installing Rails 3.0 view instrumentation'
101
- end
102
-
103
- executes do
104
- ActionView::Template.class_eval do
105
- include NewRelic::Agent::MethodTracer
106
- def render_with_newrelic(*args, &block)
107
- options = if @virtual_path && @virtual_path.starts_with?('/') # file render
108
- {:file => true }
109
- else
110
- {}
111
- end
112
- str = "View/#{NewRelic::Agent::Instrumentation::Rails3::ActionView::NewRelic.template_metric(@identifier, options)}/#{NewRelic::Agent::Instrumentation::Rails3::ActionView::NewRelic.render_type(@identifier)}"
113
- trace_execution_scoped str do
114
- render_without_newrelic(*args, &block)
115
- end
116
- end
117
-
118
- alias_method :render_without_newrelic, :render
119
- alias_method :render, :render_with_newrelic
120
-
121
- end
122
- end
123
- end
124
87
 
88
+ # Though this uses the name :rails31_view, it is used only for rails 3.2 now
89
+ # Rails 3.1 is no longer supported by the agent.
125
90
  DependencyDetection.defer do
126
91
  @name = :rails31_view
127
92
 
128
- # We can't be sure that this will work with future versions of Rails 3.
129
- # Currently enabled for Rails 3.1 and 3.2
93
+ # Enabled for Rails 3.2
130
94
  depends_on do
131
95
  defined?(::Rails::VERSION::MAJOR) && defined?(::Rails::VERSION::MINOR) &&
132
- ::Rails::VERSION::MAJOR.to_i == 3 && ([1,2].member?(::Rails::VERSION::MINOR.to_i))
96
+ ::Rails::VERSION::MAJOR.to_i == 3 && ::Rails::VERSION::MINOR.to_i == 2
133
97
  end
134
98
 
135
99
  depends_on do
@@ -137,7 +101,7 @@ DependencyDetection.defer do
137
101
  end
138
102
 
139
103
  executes do
140
- ::NewRelic::Agent.logger.info 'Installing Rails 3.1/3.2 view instrumentation'
104
+ ::NewRelic::Agent.logger.info 'Installing Rails 3.2 view instrumentation'
141
105
  end
142
106
 
143
107
  executes do
@@ -18,6 +18,10 @@ DependencyDetection.defer do
18
18
  defined?(::Redis) && defined?(::Redis::VERSION)
19
19
  end
20
20
 
21
+ conflicts_with_prepend do
22
+ defined?(::PrometheusExporter)
23
+ end
24
+
21
25
  depends_on do
22
26
  NewRelic::Agent::Datastores::Redis.is_supported_version? &&
23
27
  NewRelic::Agent::Datastores::Redis.safe_from_third_party_gem?
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module Instrumentation
8
+ module Resque
9
+ module Helper
10
+ extend self
11
+
12
+ def resque_fork_per_job?
13
+ ENV["FORK_PER_JOB"] != 'false' && NewRelic::LanguageSupport.can_fork?
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -6,7 +6,7 @@
6
6
  module NewRelic::Agent::Instrumentation
7
7
  module Resque
8
8
  include NewRelic::Agent::Instrumentation::ControllerInstrumentation
9
-
9
+
10
10
  def with_tracing
11
11
  begin
12
12
  perform_action_with_newrelic_trace(
@@ -24,8 +24,10 @@ module NewRelic::Agent::Instrumentation
24
24
  ensure
25
25
  # Stopping the event loop before flushing the pipe.
26
26
  # The goal is to avoid conflict during write.
27
- NewRelic::Agent.agent.stop_event_loop
28
- NewRelic::Agent.agent.flush_pipe_data
27
+ if NewRelic::Agent::Instrumentation::Resque::Helper.resque_fork_per_job?
28
+ NewRelic::Agent.agent.stop_event_loop
29
+ NewRelic::Agent.agent.flush_pipe_data
30
+ end
29
31
  end
30
32
  end
31
33
 
@@ -13,6 +13,11 @@ DependencyDetection.defer do
13
13
  defined?(::Resque::Job) && !NewRelic::Agent.config[:disable_resque]
14
14
  end
15
15
 
16
+ # Airbrake uses method chaining on Resque::Job on versions < 11.0.3
17
+ conflicts_with_prepend do
18
+ defined?(::Airbrake) && defined?(::Airbrake::AIRBRAKE_VERSION) && ::Gem::Version.create(::Airbrake::AIRBRAKE_VERSION) < ::Gem::Version.create('11.0.3')
19
+ end
20
+
16
21
  executes do
17
22
  ::NewRelic::Agent.logger.info 'Installing Resque instrumentation'
18
23
  end
@@ -32,7 +37,7 @@ DependencyDetection.defer do
32
37
  chain_instrument NewRelic::Agent::Instrumentation::Resque::Chain
33
38
  end
34
39
 
35
- if NewRelic::LanguageSupport.can_fork?
40
+ if NewRelic::Agent::Instrumentation::Resque::Helper.resque_fork_per_job?
36
41
  ::Resque.before_first_fork do
37
42
  NewRelic::Agent.manual_start(:dispatcher => :resque,
38
43
  :sync_startup => true,
@@ -40,9 +45,7 @@ DependencyDetection.defer do
40
45
  end
41
46
 
42
47
  ::Resque.before_fork do |job|
43
- if ENV['FORK_PER_JOB'] != 'false'
44
- NewRelic::Agent.register_report_channel(job.object_id)
45
- end
48
+ NewRelic::Agent.register_report_channel(job.object_id)
46
49
  end
47
50
 
48
51
  ::Resque.after_fork do |job|
@@ -51,6 +54,12 @@ DependencyDetection.defer do
51
54
  NewRelic::Agent.after_fork(:report_to_channel => job.object_id,
52
55
  :report_instance_busy => false)
53
56
  end
57
+ else
58
+ ::Resque.before_first_fork do
59
+ NewRelic::Agent.manual_start(:dispatcher => :resque,
60
+ :sync_startup => true,
61
+ :start_channel_listener => false)
62
+ end
54
63
  end
55
64
  end
56
65
  end
@@ -60,7 +60,12 @@ DependencyDetection.defer do
60
60
 
61
61
  class Sidekiq::Extensions::DelayedClass
62
62
  def newrelic_trace_args(msg, queue)
63
- (target, method_name, _args) = YAML.load(msg['args'][0])
63
+ (target, method_name, _args) = if YAML.respond_to?(:unsafe_load)
64
+ YAML.unsafe_load(msg['args'][0])
65
+ else
66
+ YAML.load(msg['args'][0])
67
+ end
68
+
64
69
  {
65
70
  :name => method_name,
66
71
  :class_name => target.name,
@@ -39,9 +39,15 @@ module NewRelic::Agent::Instrumentation
39
39
  yield
40
40
  end
41
41
 
42
+ def install_lock
43
+ @install_lock ||= Mutex.new
44
+ end
45
+
42
46
  def try_to_use(app, clazz)
43
- has_middleware = app.middleware.any? { |info| info[0] == clazz }
44
- app.use(clazz) unless has_middleware
47
+ install_lock.synchronize do
48
+ has_middleware = app.middleware && app.middleware.any? { |info| info && info[0] == clazz }
49
+ app.use(clazz) unless has_middleware
50
+ end
45
51
  end
46
52
 
47
53
  # Capture last route we've seen. Will set for transaction on route_eval
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module Tilt
7
+ module Chain
8
+ def self.instrument!
9
+ ::Tilt::Template.module_eval do
10
+ include NewRelic::Agent::Instrumentation::Tilt
11
+
12
+ def render_with_new_relic(*args, &block)
13
+ render_with_tracing(*args) {
14
+ render_without_newrelic(*args, &block)
15
+ }
16
+ end
17
+
18
+ alias render_without_newrelic render
19
+ alias render render_with_new_relic
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module Instrumentation
8
+ module Tilt
9
+
10
+ def metric_name(klass, file)
11
+ "View/#{klass}/#{file}/Rendering"
12
+ end
13
+
14
+ # Sinatra uses #caller_locations for the file name in Tilt (unlike Rails/Rack)
15
+ # So here we are only grabbing the file name and name of directory it is in
16
+ def create_filename_for_metric(file)
17
+ return file unless defined?(::Sinatra) && defined?(::Sinatra::Base)
18
+ file.split('/')[-2..-1].join('/')
19
+ rescue NoMethodError
20
+ file
21
+ end
22
+
23
+ def render_with_tracing(*args, &block)
24
+ begin
25
+ finishable = Tracer.start_segment(
26
+ name: metric_name(self.class, create_filename_for_metric(self.file))
27
+ )
28
+ begin
29
+ yield
30
+ rescue => error
31
+ NewRelic::Agent.notice_error(error)
32
+ raise
33
+ end
34
+ ensure
35
+ finishable.finish if finishable
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module Tilt::Prepend
7
+ include NewRelic::Agent::Instrumentation::Tilt
8
+
9
+ def render(*args, &block)
10
+ render_with_tracing { super }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ require_relative 'tilt/instrumentation'
6
+ require_relative 'tilt/chain'
7
+ require_relative 'tilt/prepend'
8
+
9
+ DependencyDetection.defer do
10
+ named :tilt
11
+
12
+ depends_on { defined?(::Tilt) }
13
+
14
+ executes do
15
+ ::NewRelic::Agent.logger.info('Installing Tilt instrumentation')
16
+ end
17
+
18
+ executes do
19
+ if use_prepend?
20
+ prepend_instrument ::Tilt::Template, NewRelic::Agent::Instrumentation::Tilt::Prepend
21
+ else
22
+ chain_instrument NewRelic::Agent::Instrumentation::Tilt::Chain
23
+ end
24
+ end
25
+ end
@@ -83,39 +83,44 @@ module NewRelic
83
83
  value.nil? || value.empty?
84
84
  end
85
85
 
86
- def browser_timing_header #THREAD_LOCAL_ACCESS
86
+ def browser_timing_header(nonce=nil) #THREAD_LOCAL_ACCESS
87
87
  return '' unless js_enabled_and_ready? # fast exit
88
88
 
89
89
  state = NewRelic::Agent::Tracer.state
90
90
 
91
91
  return '' unless insert_js?(state) # slower exit
92
92
 
93
- bt_config = browser_timing_config(state)
93
+ bt_config = browser_timing_config(state, nonce)
94
94
  return '' if bt_config.empty?
95
95
 
96
- bt_config + browser_timing_loader
96
+ bt_config + browser_timing_loader(nonce)
97
97
  rescue => e
98
98
  ::NewRelic::Agent.logger.debug "Failure during RUM browser_timing_header construction", e
99
99
  ''
100
100
  end
101
101
 
102
- def browser_timing_loader
103
- html_safe_if_needed("\n<script>#{Agent.config[:js_agent_loader]}</script>")
102
+ def browser_timing_loader(nonce=nil)
103
+ html_safe_if_needed("\n<script type=\"text/javascript\"#{create_nonce(nonce)}>#{Agent.config[:js_agent_loader]}</script>")
104
104
  end
105
105
 
106
- def browser_timing_config(state)
106
+ def browser_timing_config(state, nonce=nil)
107
107
  txn = state.current_transaction
108
108
  return '' if txn.nil?
109
109
 
110
110
  txn.freeze_name_and_execute_if_not_ignored do
111
111
  data = data_for_js_agent(txn)
112
112
  json = ::JSON.dump(data)
113
- return html_safe_if_needed("\n<script>window.NREUM||(NREUM={});NREUM.info=#{json}</script>")
113
+ return html_safe_if_needed("\n<script type=\"text/javascript\"#{create_nonce(nonce)}>window.NREUM||(NREUM={});NREUM.info=#{json}</script>")
114
114
  end
115
115
 
116
116
  ''
117
117
  end
118
118
 
119
+ def create_nonce(nonce=nil)
120
+ return '' unless nonce
121
+ " nonce=\"#{nonce.to_s}\""
122
+ end
123
+
119
124
  BEACON_KEY = "beacon".freeze
120
125
  ERROR_BEACON_KEY = "errorBeacon".freeze
121
126
  LICENSE_KEY_KEY = "licenseKey".freeze
@@ -131,9 +136,9 @@ module NewRelic
131
136
 
132
137
  # NOTE: Internal prototyping may override this, so leave name stable!
133
138
  def data_for_js_agent(transaction)
134
- queue_time_in_seconds = [transaction.queue_time.to_f, 0.0].max
135
- start_time_in_seconds = [transaction.start_time.to_f, 0.0].max
136
- app_time_in_seconds = Time.now.to_f - start_time_in_seconds
139
+ queue_time_in_seconds = [transaction.queue_time, 0.0].max
140
+ start_time_in_seconds = [transaction.start_time, 0.0].max
141
+ app_time_in_seconds = Process.clock_gettime(Process::CLOCK_REALTIME) - start_time_in_seconds
137
142
 
138
143
  queue_time_in_millis = (1000.0 * queue_time_in_seconds).round
139
144
  app_time_in_millis = (1000.0 * app_time_in_seconds).round
@@ -33,6 +33,7 @@ module NewRelic
33
33
  COLON = ':'.freeze
34
34
  COMMA = ','.freeze
35
35
  CLOSING_BRACE = '}'.freeze
36
+ REPLACEMENT_CHAR = '�'
36
37
 
37
38
  def initialize
38
39
  Agent.config.register_callback :app_name do
@@ -86,12 +87,10 @@ module NewRelic
86
87
  message << QUOTE << key << QUOTE << COLON << QUOTE << value << QUOTE
87
88
  end
88
89
 
89
- def escape message
90
- if String === message
91
- message.to_json
92
- else
93
- message.inspect.to_json
94
- end
90
+ def escape(message)
91
+ message = message.to_s unless message.is_a?(String)
92
+ message = message.scrub(REPLACEMENT_CHAR) unless message.valid_encoding?
93
+ message.to_json
95
94
  end
96
95
 
97
96
  def clear_tags!
@@ -110,20 +110,15 @@ module NewRelic
110
110
  #
111
111
  # @api public
112
112
  #
113
- def wrap_message_broker_consume_transaction library: nil,
114
- destination_type: nil,
115
- destination_name: nil,
113
+ def wrap_message_broker_consume_transaction(library:,
114
+ destination_type:,
115
+ destination_name:,
116
116
  headers: nil,
117
117
  routing_key: nil,
118
118
  queue_name: nil,
119
119
  exchange_type: nil,
120
120
  reply_to: nil,
121
- correlation_id: nil
122
-
123
- # ruby 2.0.0 does not support required kwargs
124
- raise ArgumentError, 'missing required argument: library' if library.nil?
125
- raise ArgumentError, 'missing required argument: destination_type' if destination_type.nil?
126
- raise ArgumentError, 'missing required argument: destination_name' if destination_name.nil?
121
+ correlation_id: nil)
127
122
 
128
123
  state = Tracer.state
129
124
  return yield if state.current_transaction
@@ -185,17 +180,14 @@ module NewRelic
185
180
  #
186
181
  # @api public
187
182
  #
188
- def start_amqp_publish_segment(library: nil,
189
- destination_name: nil,
183
+ def start_amqp_publish_segment(library:,
184
+ destination_name:,
190
185
  headers: nil,
191
186
  routing_key: nil,
192
187
  reply_to: nil,
193
188
  correlation_id: nil,
194
189
  exchange_type: nil)
195
190
 
196
- # ruby 2.0.0 does not support required kwargs
197
- raise ArgumentError, 'missing required argument: library' if library.nil?
198
- raise ArgumentError, 'missing required argument: destination_name' if destination_name.nil?
199
191
  raise ArgumentError, 'missing required argument: headers' if headers.nil? && CrossAppTracing.cross_app_enabled?
200
192
 
201
193
  original_headers = headers.nil? ? nil : headers.dup
@@ -247,20 +239,14 @@ module NewRelic
247
239
  #
248
240
  # @api public
249
241
  #
250
- def start_amqp_consume_segment(library: nil,
251
- destination_name: nil,
252
- delivery_info: nil,
253
- message_properties: nil,
242
+ def start_amqp_consume_segment(library:,
243
+ destination_name:,
244
+ delivery_info:,
245
+ message_properties:,
254
246
  exchange_type: nil,
255
247
  queue_name: nil,
256
248
  start_time: nil)
257
249
 
258
- # ruby 2.0.0 does not support required kwargs
259
- raise ArgumentError, 'missing required argument: library' if library.nil?
260
- raise ArgumentError, 'missing required argument: destination_name' if destination_name.nil?
261
- raise ArgumentError, 'missing required argument: delivery_info' if delivery_info.nil?
262
- raise ArgumentError, 'missing required argument: message_properties' if message_properties.nil?
263
-
264
250
  segment = Tracer.start_message_broker_segment(
265
251
  action: :consume,
266
252
  library: library,