newrelic_rpm 6.4.0.356 → 6.9.0.363

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +50 -5
  4. data/CHANGELOG.md +218 -0
  5. data/Guardfile +16 -1
  6. data/lib/new_relic/agent.rb +86 -5
  7. data/lib/new_relic/agent/agent.rb +124 -56
  8. data/lib/new_relic/agent/agent_logger.rb +4 -0
  9. data/lib/new_relic/agent/attribute_filter.rb +7 -7
  10. data/lib/new_relic/agent/attributes.rb +150 -0
  11. data/lib/new_relic/agent/autostart.rb +19 -14
  12. data/lib/new_relic/agent/configuration/default_source.rb +133 -14
  13. data/lib/new_relic/agent/configuration/event_harvest_config.rb +45 -0
  14. data/lib/new_relic/agent/configuration/manager.rb +13 -9
  15. data/lib/new_relic/agent/configuration/server_source.rb +34 -10
  16. data/lib/new_relic/agent/configuration/yaml_source.rb +11 -6
  17. data/lib/new_relic/agent/connect/request_builder.rb +8 -15
  18. data/lib/new_relic/agent/connect/response_handler.rb +1 -1
  19. data/lib/new_relic/agent/database.rb +1 -2
  20. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +2 -2
  21. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +8 -8
  22. data/lib/new_relic/agent/distributed_tracing.rb +155 -6
  23. data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +0 -0
  24. data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +60 -45
  25. data/lib/new_relic/agent/distributed_tracing/distributed_trace_intrinsics.rb +80 -0
  26. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  27. data/lib/new_relic/agent/{distributed_trace_payload.rb → distributed_tracing/distributed_trace_payload.rb} +23 -99
  28. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
  29. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
  30. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +126 -0
  31. data/lib/new_relic/agent/error_collector.rb +5 -7
  32. data/lib/new_relic/agent/error_event_aggregator.rb +5 -2
  33. data/lib/new_relic/agent/error_trace_aggregator.rb +1 -0
  34. data/lib/new_relic/agent/event_aggregator.rb +26 -32
  35. data/lib/new_relic/agent/external.rb +7 -7
  36. data/lib/new_relic/agent/guid_generator.rb +28 -0
  37. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +1 -2
  38. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +24 -38
  39. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +2 -2
  40. data/lib/new_relic/agent/instrumentation/bunny.rb +1 -1
  41. data/lib/new_relic/agent/instrumentation/curb.rb +1 -1
  42. data/lib/new_relic/agent/instrumentation/excon.rb +1 -1
  43. data/lib/new_relic/agent/instrumentation/grape.rb +5 -10
  44. data/lib/new_relic/agent/instrumentation/http.rb +1 -1
  45. data/lib/new_relic/agent/instrumentation/httpclient.rb +1 -1
  46. data/lib/new_relic/agent/instrumentation/net.rb +1 -1
  47. data/lib/new_relic/agent/instrumentation/rails_notifications/action_cable.rb +2 -3
  48. data/lib/new_relic/agent/instrumentation/resque.rb +3 -0
  49. data/lib/new_relic/agent/instrumentation/typhoeus.rb +1 -1
  50. data/lib/new_relic/agent/javascript_instrumentor.rb +1 -1
  51. data/lib/new_relic/agent/logging.rb +139 -0
  52. data/lib/new_relic/agent/messaging.rb +5 -73
  53. data/lib/new_relic/agent/method_tracer.rb +3 -2
  54. data/lib/new_relic/agent/method_tracer_helpers.rb +1 -1
  55. data/lib/new_relic/agent/monitors.rb +27 -0
  56. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
  57. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
  58. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +3 -3
  59. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +2 -4
  60. data/lib/new_relic/agent/new_relic_service.rb +7 -5
  61. data/lib/new_relic/agent/priority_sampled_buffer.rb +2 -0
  62. data/lib/new_relic/agent/span_event_aggregator.rb +2 -4
  63. data/lib/new_relic/agent/span_event_primitive.rb +43 -25
  64. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  65. data/lib/new_relic/agent/supported_versions.rb +2 -2
  66. data/lib/new_relic/agent/tracer.rb +35 -3
  67. data/lib/new_relic/agent/transaction.rb +37 -36
  68. data/lib/new_relic/agent/transaction/abstract_segment.rb +2 -2
  69. data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
  70. data/lib/new_relic/agent/transaction/distributed_tracing.rb +57 -146
  71. data/lib/new_relic/agent/transaction/external_request_segment.rb +8 -31
  72. data/lib/new_relic/agent/transaction/message_broker_segment.rb +3 -11
  73. data/lib/new_relic/agent/transaction/segment.rb +7 -1
  74. data/lib/new_relic/agent/transaction/trace.rb +2 -4
  75. data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
  76. data/lib/new_relic/agent/transaction/trace_node.rb +10 -8
  77. data/lib/new_relic/agent/transaction_error_primitive.rb +5 -12
  78. data/lib/new_relic/agent/transaction_event_primitive.rb +28 -39
  79. data/lib/new_relic/agent/transaction_event_recorder.rb +3 -3
  80. data/lib/new_relic/cli/commands/deployments.rb +1 -1
  81. data/lib/new_relic/coerce.rb +31 -6
  82. data/lib/new_relic/constants.rb +34 -0
  83. data/lib/new_relic/control/instance_methods.rb +10 -1
  84. data/lib/new_relic/dependency_detection.rb +4 -4
  85. data/lib/new_relic/noticed_error.rb +10 -8
  86. data/lib/new_relic/rack/browser_monitoring.rb +4 -0
  87. data/lib/new_relic/supportability_helper.rb +14 -0
  88. data/lib/new_relic/version.rb +1 -1
  89. data/lib/tasks/tests.rake +6 -1
  90. data/newrelic_rpm.gemspec +16 -5
  91. data/test/agent_helper.rb +115 -9
  92. data/true +0 -0
  93. metadata +97 -30
  94. data/lib/new_relic/agent/configuration/event_data.rb +0 -39
  95. data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
  96. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -40
  97. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  98. data/lib/tasks/versions.html.erb +0 -28
  99. data/lib/tasks/versions.postface.html +0 -8
  100. data/lib/tasks/versions.preface.html +0 -9
  101. data/lib/tasks/versions.rake +0 -65
  102. data/lib/tasks/versions.txt.erb +0 -14
@@ -14,9 +14,9 @@ module NewRelic
14
14
  attr_reader :transaction_event_aggregator
15
15
  attr_reader :synthetics_event_aggregator
16
16
 
17
- def initialize
18
- @transaction_event_aggregator = NewRelic::Agent::TransactionEventAggregator.new
19
- @synthetics_event_aggregator = NewRelic::Agent::SyntheticsEventAggregator.new
17
+ def initialize events
18
+ @transaction_event_aggregator = NewRelic::Agent::TransactionEventAggregator.new events
19
+ @synthetics_event_aggregator = NewRelic::Agent::SyntheticsEventAggregator.new events
20
20
  end
21
21
 
22
22
  def record payload
@@ -39,7 +39,7 @@ class NewRelic::Cli::Deployments < NewRelic::Cli::Command
39
39
  control.env = @environment if @environment
40
40
 
41
41
  load_yaml_from_env(control.env)
42
- @appname ||= NewRelic::Agent.config.app_names[0] || control.env || 'development'
42
+ @appname ||= NewRelic::Agent.config[:app_name][0] || control.env || 'development'
43
43
  @license_key ||= NewRelic::Agent.config[:license_key]
44
44
 
45
45
  setup_logging(control.env)
@@ -1,17 +1,20 @@
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
  module NewRelic
6
- # We really don't want to send bad values to the collector, and it doesn't
7
- # accept types like Rational that have occasionally slipped into our data.
8
- #
9
- # These methods are intended to safely coerce things into the form we want,
10
- # to provide documentation of expected types on to_collector_array methods,
11
- # and to log failures if totally invalid data gets into outgoing data
12
7
  module Coerce
8
+
13
9
  module_function
14
10
 
11
+ # We really don't want to send bad values to the collector, and it doesn't
12
+ # accept types like Rational that have occasionally slipped into our data.
13
+ #
14
+ # These non-bang methods are intended to safely coerce things into the form we want,
15
+ # to provide documentation of expected types on to_collector_array methods,
16
+ # and to log failures if totally invalid data gets into outgoing data
17
+
15
18
  def int(value, context=nil)
16
19
  Integer(value)
17
20
  rescue => error
@@ -61,6 +64,28 @@ module NewRelic
61
64
  end
62
65
  end
63
66
 
67
+ def int! value
68
+ return nil unless value_or_nil(value)
69
+ Integer(value)
70
+ end
71
+
72
+ # Use when you plan to perform a boolean check using the integer 1
73
+ # for true and the integer 0 for false
74
+ # String values will be converted to 0
75
+ def boolean_int! value
76
+ value.to_i
77
+ end
78
+
79
+ def float! value, precision=NewRelic::PRIORITY_PRECISION
80
+ return nil unless value_or_nil(value)
81
+ value.to_f.round(precision)
82
+ end
83
+
84
+ def value_or_nil value
85
+ return nil if value.nil? || (value.respond_to?(:empty?) && value.empty?)
86
+ value
87
+ end
88
+
64
89
  def log_failure(value, type, context, error)
65
90
  msg = "Unable to convert '#{value}' to #{type}"
66
91
  msg += " in context '#{context}'" if context
@@ -0,0 +1,34 @@
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
+ module NewRelic
7
+ PRIORITY_PRECISION = 6
8
+
9
+ EMPTY_ARRAY = [].freeze
10
+ EMPTY_HASH = {}.freeze
11
+ EMPTY_STR = ""
12
+
13
+ HTTP = "HTTP"
14
+ HTTPS = "HTTPS"
15
+ UNKNOWN = "Unknown"
16
+
17
+ FORMAT_NON_RACK = 0
18
+ FORMAT_RACK = 1
19
+
20
+ NEWRELIC_KEY = "newrelic"
21
+ CANDIDATE_NEWRELIC_KEYS = [
22
+ NEWRELIC_KEY,
23
+ 'NEWRELIC',
24
+ 'NewRelic',
25
+ 'Newrelic'
26
+ ].freeze
27
+
28
+ TRACEPARENT_KEY = "traceparent"
29
+ TRACESTATE_KEY = "tracestate"
30
+
31
+ HTTP_TRACEPARENT_KEY = "HTTP_#{TRACEPARENT_KEY.upcase}"
32
+ HTTP_TRACESTATE_KEY = "HTTP_#{TRACESTATE_KEY.upcase}"
33
+ HTTP_NEWRELIC_KEY = "HTTP_#{NEWRELIC_KEY.upcase}"
34
+ end
@@ -101,7 +101,13 @@ module NewRelic
101
101
  Agent.config.replace_or_add_config(manual)
102
102
 
103
103
  config_file_path = @config_file_override || Agent.config[:config_path]
104
- Agent.config.replace_or_add_config(Agent::Configuration::YamlSource.new(config_file_path, env))
104
+ yaml_source = Agent::Configuration::YamlSource.new(config_file_path, env)
105
+ if yaml_source.failed?
106
+ yaml_source.failures.each do |msg|
107
+ stdout.puts Agent::AgentLogger.format_fatal_error(msg)
108
+ end
109
+ end
110
+ Agent.config.replace_or_add_config(yaml_source)
105
111
 
106
112
  if security_settings_valid? && Agent.config[:high_security]
107
113
  Agent.logger.info("Installing high security configuration based on local configuration")
@@ -172,6 +178,9 @@ module NewRelic
172
178
  '.'
173
179
  end
174
180
 
181
+ def stdout
182
+ STDOUT
183
+ end
175
184
  end
176
185
  include InstanceMethods
177
186
  end
@@ -93,8 +93,8 @@ module DependencyDetection
93
93
  end
94
94
  end
95
95
 
96
- def depends_on
97
- @dependencies << Proc.new
96
+ def depends_on &block
97
+ @dependencies << block if block_given?
98
98
  end
99
99
 
100
100
  def allowed_by_config?
@@ -114,8 +114,8 @@ module DependencyDetection
114
114
  self.name = new_name
115
115
  end
116
116
 
117
- def executes
118
- @executes << Proc.new
117
+ def executes &block
118
+ @executes << block if block_given?
119
119
  end
120
120
  end
121
121
  end
@@ -39,7 +39,7 @@ class NewRelic::NoticedError
39
39
 
40
40
  # replace error message if enabled
41
41
  if NewRelic::Agent.config[:'strip_exception_messages.enabled'] &&
42
- !self.class.passes_message_whitelist(exception.class)
42
+ !self.class.passes_message_allowlist(exception.class)
43
43
  @message = STRIPPED_EXCEPTION_REPLACEMENT_MESSAGE
44
44
  end
45
45
 
@@ -57,8 +57,12 @@ class NewRelic::NoticedError
57
57
  end
58
58
  end
59
59
 
60
- def self.passes_message_whitelist(exception_class)
61
- NewRelic::Agent.config[:'strip_exception_messages.whitelist'].any? do |klass|
60
+ def self.passes_message_allowlist(exception_class)
61
+ # For backwards compatibility until we remove :'strip_exception_messages.whitelist' config option
62
+
63
+ allowed = NewRelic::Agent.config[:'strip_exception_messages.allowed_classes'].concat(NewRelic::Agent.config[:'strip_exception_messages.whitelist'])
64
+
65
+ allowed.any? do |klass|
62
66
  exception_class <= klass
63
67
  end
64
68
  end
@@ -77,8 +81,6 @@ class NewRelic::NoticedError
77
81
  AGENT_ATTRIBUTES = "agentAttributes".freeze
78
82
  INTRINSIC_ATTRIBUTES = "intrinsics".freeze
79
83
 
80
- EMPTY_HASH = {}.freeze
81
-
82
84
  DESTINATION = NewRelic::Agent::AttributeFilter::DST_ERROR_COLLECTOR
83
85
 
84
86
  # Note that we process attributes lazily and store the result. This is because
@@ -87,7 +89,7 @@ class NewRelic::NoticedError
87
89
  def processed_attributes
88
90
  @processed_attributes ||= begin
89
91
  attributes = base_parameters
90
- merged_attributes = NewRelic::Agent::Transaction::Attributes.new(NewRelic::Agent.instance.attribute_filter)
92
+ merged_attributes = NewRelic::Agent::Attributes.new(NewRelic::Agent.instance.attribute_filter)
91
93
  append_attributes(attributes, USER_ATTRIBUTES, merged_custom_attributes(merged_attributes))
92
94
  append_attributes(attributes, AGENT_ATTRIBUTES, build_agent_attributes(merged_attributes))
93
95
  append_attributes(attributes, INTRINSIC_ATTRIBUTES, build_intrinsic_attributes)
@@ -133,7 +135,7 @@ class NewRelic::NoticedError
133
135
  agent_attributes = if @attributes
134
136
  @attributes.agent_attributes_for(DESTINATION)
135
137
  else
136
- EMPTY_HASH
138
+ NewRelic::EMPTY_HASH
137
139
  end
138
140
 
139
141
  # It's possible to override the request_uri from the transaction attributes
@@ -151,7 +153,7 @@ class NewRelic::NoticedError
151
153
  if @attributes
152
154
  @attributes.intrinsic_attributes_for(DESTINATION)
153
155
  else
154
- EMPTY_HASH
156
+ NewRelic::EMPTY_HASH
155
157
  end
156
158
  end
157
159
 
@@ -21,6 +21,7 @@ module NewRelic::Rack
21
21
 
22
22
  CONTENT_TYPE = 'Content-Type'.freeze
23
23
  CONTENT_DISPOSITION = 'Content-Disposition'.freeze
24
+ CONTENT_LENGTH = 'Content-Length'.freeze
24
25
  ATTACHMENT = 'attachment'.freeze
25
26
  TEXT_HTML = 'text/html'.freeze
26
27
 
@@ -35,6 +36,9 @@ module NewRelic::Rack
35
36
  js_to_inject = NewRelic::Agent.browser_timing_header
36
37
  if (js_to_inject != "") && should_instrument?(env, status, headers)
37
38
  response_string = autoinstrument_source(response, headers, js_to_inject)
39
+ if headers.key?(CONTENT_LENGTH)
40
+ headers[CONTENT_LENGTH] = response_string.size.to_s
41
+ end
38
42
 
39
43
  env[ALREADY_INSTRUMENTED_KEY] = true
40
44
  if response_string
@@ -11,6 +11,8 @@ module NewRelic
11
11
  # transaction, just to eke out a bit less performance hit
12
12
  #
13
13
  API_SUPPORTABILITY_METRICS = [
14
+ :accept_distributed_trace_headers,
15
+ :create_distributed_trace_headers,
14
16
  :add_custom_attributes,
15
17
  :add_instrumentation,
16
18
  :add_method_tracer,
@@ -64,5 +66,17 @@ module NewRelic
64
66
  NewRelic::Agent.logger.debug "API supportability metric not found for :#{method_name}"
65
67
  end
66
68
  end
69
+
70
+ def valid_api_argument_class? arg, name, klass
71
+ return true if arg.is_a?(klass)
72
+
73
+ caller_location = caller_locations.first.label
74
+
75
+ message = "Bad argument passed to ##{caller_location}. " \
76
+ "Expected #{klass} for `#{name}` but got #{headers.class}"
77
+
78
+ NewRelic::Agent.logger.warn message
79
+ nil
80
+ end
67
81
  end
68
82
  end
@@ -11,7 +11,7 @@ module NewRelic
11
11
  end
12
12
 
13
13
  MAJOR = 6
14
- MINOR = 4
14
+ MINOR = 9
15
15
  TINY = 0
16
16
 
17
17
  begin
@@ -26,9 +26,14 @@ if defined? Rake::TestTask
26
26
  agent_home = File.expand_path(File.join(File.dirname(__FILE__),'..','..'))
27
27
 
28
28
  Rake::TestTask.new(:newrelic) do |t|
29
+
30
+ file_pattern = ENV["file"]
31
+ file_pattern = file_pattern.split(",").map{|f| "#{agent_home}/#{f}".gsub("//", "/")} if file_pattern
32
+ file_pattern ||= "#{agent_home}/test/new_relic/**/*_test.rb"
33
+
29
34
  t.libs << "#{agent_home}/test"
30
35
  t.libs << "#{agent_home}/lib"
31
- t.pattern = "#{agent_home}/test/new_relic/**/*_test.rb"
36
+ t.pattern = file_pattern
32
37
  t.verbose = true
33
38
  end
34
39
 
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.version = NewRelic::VERSION::STRING
11
11
  s.required_ruby_version = '>= 2.0.0'
12
12
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
13
- s.authors = [ "Matthew Wear", "Chris Pine", "Rachel Klein", "Justin Foote" ]
13
+ s.authors = [ "Chris Pine", "Rachel Klein", "Tanna McClure", "Michael Lang" ]
14
14
  s.date = Time.now.strftime('%Y-%m-%d')
15
15
  s.licenses = ['New Relic']
16
16
  s.description = <<-EOS
@@ -31,6 +31,13 @@ EOS
31
31
  "newrelic.yml"
32
32
  ]
33
33
 
34
+ s.metadata = {
35
+ 'bug_tracker_uri' => 'https://support.newrelic.com/',
36
+ 'changelog_uri' => 'https://github.com/newrelic/rpm/blob/master/CHANGELOG.md',
37
+ 'documentation_uri' => 'https://docs.newrelic.com/docs/agents/ruby-agent',
38
+ 'source_code_uri' => 'https://github.com/newrelic/rpm'
39
+ }
40
+
34
41
  file_list = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/(?!agent_helper.rb)}) }
35
42
  build_file_path = 'lib/new_relic/build.rb'
36
43
  file_list << build_file_path if File.exist?(build_file_path)
@@ -41,12 +48,16 @@ EOS
41
48
  s.rubygems_version = Gem::VERSION
42
49
  s.summary = "New Relic Ruby Agent"
43
50
 
44
- s.add_development_dependency 'rake', '10.1.0'
51
+ s.add_development_dependency 'rake', '12.3.3'
52
+ s.add_development_dependency 'rb-inotify', '0.9.10' # locked to support < Ruby 2.3 (and listen 3.0.8)
53
+ s.add_development_dependency 'listen', '3.0.8' # locked to support < Ruby 2.3
45
54
  s.add_development_dependency 'minitest', '~> 4.7.5'
46
- s.add_development_dependency 'mocha', '~> 0.13.0'
55
+ s.add_development_dependency 'mocha', '~> 1.9.0'
47
56
  s.add_development_dependency 'yard'
48
- s.add_development_dependency 'pry-nav', '~> 0.2.4'
57
+ s.add_development_dependency 'pry-nav', '~> 0.3.0'
49
58
  s.add_development_dependency 'pry-stack_explorer', '~> 0.4.9'
59
+ s.add_development_dependency 'guard', '~> 2.16.0'
60
+ s.add_development_dependency 'guard-minitest', '~> 2.4.0'
50
61
  s.add_development_dependency 'hometown', '~> 0.2.5'
51
- s.add_development_dependency 'bundler', '< 2.0'
62
+ s.add_development_dependency 'bundler'
52
63
  end
@@ -212,7 +212,7 @@ end
212
212
  #
213
213
  # If you want to *allow* unexpected metrics matching certain patterns, use
214
214
  # the :ignore_filter option. This will allow you to specify a Regex that
215
- # whitelists broad swathes of metric territory (e.g. 'Supportability/').
215
+ # allowlists broad swathes of metric territory (e.g. 'Supportability/').
216
216
  #
217
217
  def assert_metrics_recorded_exclusive(expected, options={})
218
218
  expected = _normalize_metric_expectations(expected)
@@ -345,6 +345,26 @@ def in_transaction(*args, &blk)
345
345
  txn
346
346
  end
347
347
 
348
+ # Temporarily disables default transformer so tests with invalid inputs can be tried
349
+ def with_disabled_defaults_transformer key
350
+ begin
351
+ transformer = NewRelic::Agent::Configuration::DEFAULTS[key][:transform]
352
+ NewRelic::Agent::Configuration::DEFAULTS[key][:transform] = nil
353
+ yield
354
+ ensure
355
+ NewRelic::Agent::Configuration::DEFAULTS[key][:transform] = transformer
356
+ end
357
+ end
358
+
359
+ # Convenience wrapper to stand up a transaction and provide a segment within
360
+ # that transaction to work with. The same arguements as provided to in_transaction
361
+ # may be supplied.
362
+ def with_segment *args, &blk
363
+ in_transaction(*args) do |txn|
364
+ yield txn.current_segment, txn
365
+ end
366
+ end
367
+
348
368
  def stub_transaction_guid(guid)
349
369
  NewRelic::Agent::Transaction.tl_current.instance_variable_set(:@guid, guid)
350
370
  end
@@ -452,6 +472,13 @@ def with_config(config_hash, at_start=true)
452
472
  end
453
473
  end
454
474
 
475
+ def with_server_source config_hash, at_start=true
476
+ with_config config_hash, at_start do
477
+ NewRelic::Agent.config.notify_server_source_added
478
+ yield
479
+ end
480
+ end
481
+
455
482
  def with_config_low_priority(config_hash)
456
483
  with_config(config_hash, false) do
457
484
  yield
@@ -605,17 +632,87 @@ ensure
605
632
  NewRelic::Agent.logger = orig_logger
606
633
  end
607
634
 
608
- def with_environment(env)
609
- old_env = {}
610
- env.each do |key, val|
611
- old_env[key] = ENV[key]
612
- ENV[key] = val.to_s
635
+ # The EnvUpdater was introduced due to random fails in JRuby environment
636
+ # whereby attempting to set ENV[key] = some_value randomly failed.
637
+ # It is conjectured that this is thread related, but may also be
638
+ # a core bug in the JVM implementation of Ruby. Root cause was not
639
+ # discovered, but it was found that a combination of retrying and using
640
+ # mutex lock around the update operation was the only consistently working
641
+ # solution as the error continued to surface without the mutex and
642
+ # retry alone wasn't enough, either.
643
+ #
644
+ # JRUBY: oraclejdk8 + jruby-9.2.6.0
645
+ #
646
+ # NOTE: Singleton pattern to ensure one mutex lock for all threads
647
+ class EnvUpdater
648
+ MAX_RETRIES = 5
649
+
650
+ def initialize
651
+ @mutex = Mutex.new
613
652
  end
614
- begin
653
+
654
+ # Will attempt the given block up to MAX_RETRIES before
655
+ # surfacing the exception down the chain.
656
+ def with_retry retry_limit=MAX_RETRIES
657
+ retries ||= 0
658
+ sleep(retries)
615
659
  yield
616
- ensure
617
- old_env.each { |key, old_val| ENV[key] = old_val }
660
+ rescue
661
+ (retries += 1) < retry_limit ? retry : raise
662
+ end
663
+
664
+ # Locks and updates the ENV
665
+ def safe_update env
666
+ with_retry do
667
+ @mutex.synchronize do
668
+ env.each{ |key, val| ENV[key] = val.to_s }
669
+ end
670
+ end
671
+ end
672
+
673
+ # Locks and restores the ENV
674
+ def safe_restore old_env
675
+ with_retry do
676
+ @mutex.synchronize do
677
+ old_env.each{ |key, val| val ? ENV[key] = val : ENV.delete(key) }
678
+ end
679
+ end
618
680
  end
681
+
682
+ # Singleton pattern implemented via @@instance
683
+ def self.instance
684
+ @@instance ||= EnvUpdater.new
685
+ end
686
+
687
+ def self.safe_update env
688
+ instance.safe_update env
689
+ end
690
+
691
+ def self.safe_restore old_env
692
+ instance.safe_restore old_env
693
+ end
694
+
695
+ # Effectively saves current ENV settings for given env's key/values,
696
+ # runs given block, then restores ENV to original state before returning.
697
+ def self.inject env, &block
698
+ old_env = {}
699
+ env.each{ |key, val| old_env[key] = ENV[key] }
700
+ begin
701
+ safe_update(env)
702
+ yield
703
+ ensure
704
+ safe_restore(old_env)
705
+ end
706
+ end
707
+
708
+ # must call instance here to ensure only one @mutex for all threads.
709
+ instance
710
+ end
711
+
712
+ # Changes ENV settings to given and runs given block and restores ENV
713
+ # to original values before returning.
714
+ def with_environment(env, &block)
715
+ EnvUpdater.inject(env) { yield }
619
716
  end
620
717
 
621
718
  def with_argv(argv)
@@ -709,3 +806,12 @@ end
709
806
  def attributes_for(sample, type)
710
807
  sample.attributes.instance_variable_get("@#{type}_attributes")
711
808
  end
809
+
810
+ def uncache_trusted_account_key
811
+ NewRelic::Agent::Transaction::TraceContext::AccountHelpers.instance_variable_set :@trace_state_entry_key, nil
812
+ end
813
+
814
+ def reset_buffers_and_caches
815
+ NewRelic::Agent.drop_buffered_data
816
+ uncache_trusted_account_key
817
+ end