newrelic_rpm 6.6.0.358 → 6.11.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of newrelic_rpm might be problematic. Click here for more details.

Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +65 -5
  4. data/CHANGELOG.md +222 -0
  5. data/Gemfile +5 -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 +114 -55
  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 +127 -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/datastores/mongo.rb +1 -1
  23. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +2 -2
  24. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +8 -8
  25. data/lib/new_relic/agent/distributed_tracing.rb +155 -6
  26. data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +0 -0
  27. data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +60 -45
  28. data/lib/new_relic/agent/distributed_tracing/distributed_trace_intrinsics.rb +80 -0
  29. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  30. data/lib/new_relic/agent/{distributed_trace_payload.rb → distributed_tracing/distributed_trace_payload.rb} +23 -99
  31. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
  32. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
  33. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +126 -0
  34. data/lib/new_relic/agent/error_collector.rb +33 -16
  35. data/lib/new_relic/agent/error_event_aggregator.rb +7 -5
  36. data/lib/new_relic/agent/external.rb +7 -7
  37. data/lib/new_relic/agent/guid_generator.rb +28 -0
  38. data/lib/new_relic/agent/hostname.rb +7 -1
  39. data/lib/new_relic/agent/http_clients/abstract.rb +82 -0
  40. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +24 -19
  41. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +28 -13
  42. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +17 -21
  43. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +10 -11
  44. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +16 -4
  45. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +4 -6
  46. data/lib/new_relic/agent/http_clients/uri_util.rb +3 -2
  47. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +5 -7
  48. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +4 -0
  49. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +11 -2
  50. data/lib/new_relic/agent/instrumentation/active_record.rb +4 -2
  51. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +7 -2
  52. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +8 -4
  53. data/lib/new_relic/agent/instrumentation/bunny.rb +45 -28
  54. data/lib/new_relic/agent/instrumentation/curb.rb +59 -18
  55. data/lib/new_relic/agent/instrumentation/data_mapper.rb +3 -1
  56. data/lib/new_relic/agent/instrumentation/excon.rb +1 -1
  57. data/lib/new_relic/agent/instrumentation/excon/connection.rb +6 -3
  58. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +2 -1
  59. data/lib/new_relic/agent/instrumentation/grape.rb +5 -10
  60. data/lib/new_relic/agent/instrumentation/http.rb +6 -3
  61. data/lib/new_relic/agent/instrumentation/httpclient.rb +5 -3
  62. data/lib/new_relic/agent/instrumentation/memcache.rb +3 -1
  63. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +6 -2
  64. data/lib/new_relic/agent/instrumentation/mongo.rb +9 -3
  65. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +13 -0
  66. data/lib/new_relic/agent/instrumentation/net.rb +6 -3
  67. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +25 -1
  68. data/lib/new_relic/agent/instrumentation/redis.rb +9 -3
  69. data/lib/new_relic/agent/instrumentation/resque.rb +3 -0
  70. data/lib/new_relic/agent/instrumentation/sidekiq.rb +0 -1
  71. data/lib/new_relic/agent/instrumentation/typhoeus.rb +23 -6
  72. data/lib/new_relic/agent/logging.rb +139 -0
  73. data/lib/new_relic/agent/messaging.rb +5 -73
  74. data/lib/new_relic/agent/method_tracer.rb +3 -2
  75. data/lib/new_relic/agent/method_tracer_helpers.rb +2 -2
  76. data/lib/new_relic/agent/monitors.rb +27 -0
  77. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
  78. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
  79. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +1 -1
  80. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +2 -4
  81. data/lib/new_relic/agent/new_relic_service.rb +7 -5
  82. data/lib/new_relic/agent/noticible_error.rb +22 -0
  83. data/lib/new_relic/agent/span_event_aggregator.rb +1 -0
  84. data/lib/new_relic/agent/span_event_primitive.rb +82 -53
  85. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  86. data/lib/new_relic/agent/supported_versions.rb +2 -2
  87. data/lib/new_relic/agent/tracer.rb +65 -18
  88. data/lib/new_relic/agent/transaction.rb +84 -79
  89. data/lib/new_relic/agent/transaction/abstract_segment.rb +28 -2
  90. data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
  91. data/lib/new_relic/agent/transaction/distributed_tracing.rb +57 -146
  92. data/lib/new_relic/agent/transaction/external_request_segment.rb +29 -36
  93. data/lib/new_relic/agent/transaction/message_broker_segment.rb +3 -11
  94. data/lib/new_relic/agent/transaction/segment.rb +7 -1
  95. data/lib/new_relic/agent/transaction/trace.rb +2 -4
  96. data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
  97. data/lib/new_relic/agent/transaction/trace_node.rb +10 -8
  98. data/lib/new_relic/agent/transaction_error_primitive.rb +10 -15
  99. data/lib/new_relic/agent/transaction_event_primitive.rb +28 -39
  100. data/lib/new_relic/cli/commands/deployments.rb +1 -1
  101. data/lib/new_relic/cli/commands/install.rb +3 -2
  102. data/lib/new_relic/coerce.rb +31 -6
  103. data/lib/new_relic/constants.rb +34 -0
  104. data/lib/new_relic/control/instance_methods.rb +10 -1
  105. data/lib/new_relic/dependency_detection.rb +4 -4
  106. data/lib/new_relic/noticed_error.rb +38 -17
  107. data/lib/new_relic/rack/browser_monitoring.rb +5 -0
  108. data/lib/new_relic/supportability_helper.rb +14 -0
  109. data/lib/new_relic/version.rb +1 -1
  110. data/lib/tasks/multiverse.rb +25 -0
  111. data/lib/tasks/tests.rake +6 -1
  112. data/newrelic_rpm.gemspec +18 -7
  113. data/test/agent_helper.rb +322 -70
  114. metadata +98 -32
  115. data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
  116. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -40
  117. data/lib/new_relic/agent/http_clients/abstract_request.rb +0 -31
  118. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  119. data/lib/tasks/versions.html.erb +0 -28
  120. data/lib/tasks/versions.postface.html +0 -8
  121. data/lib/tasks/versions.preface.html +0 -9
  122. data/lib/tasks/versions.rake +0 -65
  123. data/lib/tasks/versions.txt.erb +0 -14
@@ -0,0 +1,126 @@
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
+
5
+ require 'new_relic/coerce'
6
+
7
+ module NewRelic
8
+ module Agent
9
+ class TraceContextPayload
10
+ VERSION = 0
11
+ PARENT_TYPE = 0
12
+ DELIMITER = "-".freeze
13
+ SUPPORTABILITY_PARSE_EXCEPTION = "Supportability/TraceContext/Parse/Exception".freeze
14
+
15
+ TRUE_CHAR = '1'.freeze
16
+ FALSE_CHAR = '0'.freeze
17
+
18
+ PARENT_TYPES = %w(App Browser Mobile).map(&:freeze).freeze
19
+
20
+ class << self
21
+ def create version: VERSION,
22
+ parent_type: PARENT_TYPE,
23
+ parent_account_id: nil,
24
+ parent_app_id: nil,
25
+ id: nil,
26
+ transaction_id: nil,
27
+ sampled: nil,
28
+ priority: nil,
29
+ timestamp: now_ms
30
+
31
+ new version, parent_type, parent_account_id, parent_app_id, id,
32
+ transaction_id, sampled, priority, timestamp
33
+ end
34
+
35
+ include NewRelic::Coerce
36
+
37
+ def from_s payload_string
38
+ attrs = payload_string.split(DELIMITER)
39
+
40
+ payload = create \
41
+ version: int!(attrs[0]),
42
+ parent_type: int!(attrs[1]),
43
+ parent_account_id: attrs[2],
44
+ parent_app_id: attrs[3],
45
+ id: value_or_nil(attrs[4]),
46
+ transaction_id: value_or_nil(attrs[5]),
47
+ sampled: value_or_nil(attrs[6]) ? boolean_int!(attrs[6]) == 1 : nil,
48
+ priority: float!(attrs[7]),
49
+ timestamp: int!(attrs[8])
50
+ handle_invalid_payload message: 'payload missing attributes' unless payload.valid?
51
+ payload
52
+ rescue => e
53
+ handle_invalid_payload error: e
54
+ raise
55
+ end
56
+
57
+ private
58
+
59
+ def now_ms
60
+ (Time.now.to_f * 1000).round
61
+ end
62
+
63
+ def handle_invalid_payload error: nil, message: nil
64
+ NewRelic::Agent.increment_metric SUPPORTABILITY_PARSE_EXCEPTION
65
+ if error
66
+ NewRelic::Agent.logger.warn "Error parsing trace context payload", error
67
+ elsif message
68
+ NewRelic::Agent.logger.warn "Error parsing trace context payload: #{message}"
69
+ end
70
+ end
71
+ end
72
+
73
+ attr_accessor :version,
74
+ :parent_type_id,
75
+ :parent_account_id,
76
+ :parent_app_id,
77
+ :id,
78
+ :transaction_id,
79
+ :sampled,
80
+ :priority,
81
+ :timestamp
82
+
83
+ alias_method :sampled?, :sampled
84
+
85
+ def initialize version, parent_type_id, parent_account_id, parent_app_id,
86
+ id, transaction_id, sampled, priority, timestamp
87
+ @version = version
88
+ @parent_type_id = parent_type_id
89
+ @parent_account_id = parent_account_id
90
+ @parent_app_id = parent_app_id
91
+ @id = id
92
+ @transaction_id = transaction_id
93
+ @sampled = sampled
94
+ @priority = priority
95
+ @timestamp = timestamp
96
+ end
97
+
98
+ def parent_type
99
+ @parent_type_string ||= PARENT_TYPES[@parent_type_id]
100
+ end
101
+
102
+ def valid?
103
+ version \
104
+ && parent_type_id \
105
+ && !parent_account_id.empty? \
106
+ && !parent_app_id.empty? \
107
+ && timestamp
108
+ rescue
109
+ false
110
+ end
111
+
112
+ def to_s
113
+ result = version.to_s
114
+ result << DELIMITER << parent_type_id.to_s
115
+ result << DELIMITER << parent_account_id
116
+ result << DELIMITER << parent_app_id
117
+ result << DELIMITER << (id || NewRelic::EMPTY_STR)
118
+ result << DELIMITER << (transaction_id || NewRelic::EMPTY_STR)
119
+ result << DELIMITER << (sampled ? TRUE_CHAR : FALSE_CHAR)
120
+ result << DELIMITER << sprintf("%.6f", priority)
121
+ result << DELIMITER << timestamp.to_s
122
+ result
123
+ end
124
+ end
125
+ end
126
+ end
@@ -32,7 +32,6 @@ module NewRelic
32
32
  end
33
33
  end
34
34
 
35
-
36
35
  def initialize_ignored_errors(ignore_errors)
37
36
  @ignore.clear
38
37
  ignore_errors = ignore_errors.split(",") if ignore_errors.is_a? String
@@ -74,7 +73,7 @@ module NewRelic
74
73
  end
75
74
 
76
75
  def self.ignore_error_filter
77
- @ignore_filter
76
+ defined?(@ignore_filter) ? @ignore_filter : nil
78
77
  end
79
78
 
80
79
  # errors is an array of Exception Class Names
@@ -117,14 +116,22 @@ module NewRelic
117
116
  NewRelic::LanguageSupport.jruby? && exception.respond_to?(:java_class)
118
117
  end
119
118
 
120
- def exception_tagged?(exception)
119
+ def exception_tagged_with?(ivar, exception)
121
120
  return false if exception_is_java_object?(exception)
122
- exception.instance_variable_defined?(EXCEPTION_TAG_IVAR)
121
+ exception.instance_variable_defined?(ivar)
122
+ end
123
+
124
+ def tag_exception_using(ivar, exception)
125
+ return if exception_is_java_object?(exception) || exception.frozen?
126
+ begin
127
+ exception.instance_variable_set(ivar, true)
128
+ rescue => e
129
+ NewRelic::Agent.logger.warn("Failed to tag exception: #{exception}: ", e)
130
+ end
123
131
  end
124
132
 
125
133
  def tag_exception(exception)
126
- return if exception_is_java_object?(exception)
127
- return if exception.frozen?
134
+ return if exception_is_java_object?(exception) || exception.frozen?
128
135
  begin
129
136
  exception.instance_variable_set(EXCEPTION_TAG_IVAR, true)
130
137
  rescue => e
@@ -169,9 +176,9 @@ module NewRelic
169
176
 
170
177
  def skip_notice_error?(exception)
171
178
  disabled? ||
172
- error_is_ignored?(exception) ||
173
- exception.nil? ||
174
- exception_tagged?(exception)
179
+ error_is_ignored?(exception) ||
180
+ exception.nil? ||
181
+ exception_tagged_with?(EXCEPTION_TAG_IVAR, exception)
175
182
  end
176
183
 
177
184
  # calls a method on an object, if it responds to it - used for
@@ -191,8 +198,18 @@ module NewRelic
191
198
  sense_method(actual_exception, :backtrace) || '<no stack trace>'
192
199
  end
193
200
 
201
+ def notice_segment_error(segment, exception, options={})
202
+ return if skip_notice_error?(exception)
203
+
204
+ segment.set_noticed_error create_noticed_error(exception, options)
205
+ exception
206
+ rescue => e
207
+ ::NewRelic::Agent.logger.warn("Failure when capturing segment error '#{exception}':", e)
208
+ nil
209
+ end
210
+
194
211
  # See NewRelic::Agent.notice_error for options and commentary
195
- def notice_error(exception, options={}) #THREAD_LOCAL_ACCESS
212
+ def notice_error(exception, options={}, span_id=nil)
196
213
  return if skip_notice_error?(exception)
197
214
 
198
215
  tag_exception(exception)
@@ -205,8 +222,10 @@ module NewRelic
205
222
 
206
223
  noticed_error = create_noticed_error(exception, options)
207
224
  error_trace_aggregator.add_to_error_queue(noticed_error)
208
- payload = state.current_transaction ? state.current_transaction.payload : nil
209
- error_event_aggregator.record(noticed_error, payload)
225
+ transaction = state.current_transaction
226
+ payload = transaction ? transaction.payload : nil
227
+ span_id ||= (transaction && transaction.current_segment) ? transaction.current_segment.guid : nil
228
+ error_event_aggregator.record(noticed_error, payload, span_id)
210
229
  exception
211
230
  rescue => e
212
231
  ::NewRelic::Agent.logger.warn("Failure when capturing error '#{exception}':", e)
@@ -227,13 +246,11 @@ module NewRelic
227
246
  truncated_trace
228
247
  end
229
248
 
230
- EMPTY_STRING = ''.freeze
231
-
232
249
  def create_noticed_error(exception, options)
233
- error_metric = options.delete(:metric) || EMPTY_STRING
250
+ error_metric = options.delete(:metric) || NewRelic::EMPTY_STR
234
251
 
235
252
  noticed_error = NewRelic::NoticedError.new(error_metric, exception)
236
- noticed_error.request_uri = options.delete(:uri) || EMPTY_STRING
253
+ noticed_error.request_uri = options.delete(:uri) || NewRelic::EMPTY_STR
237
254
  noticed_error.request_port = options.delete(:port)
238
255
  noticed_error.attributes = options.delete(:attributes)
239
256
 
@@ -2,6 +2,7 @@
2
2
  # encoding: utf-8
3
3
  # This file is distributed under New Relic's license terms.
4
4
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
5
+ # frozen_string_literal: true
5
6
 
6
7
  require 'new_relic/agent/event_aggregator'
7
8
  require 'new_relic/agent/transaction_error_primitive'
@@ -10,6 +11,7 @@ require 'new_relic/agent/priority_sampled_buffer'
10
11
  module NewRelic
11
12
  module Agent
12
13
  class ErrorEventAggregator < EventAggregator
14
+ include NewRelic::Coerce
13
15
 
14
16
  named :ErrorEventAggregator
15
17
  capacity_key :'error_collector.max_event_samples_stored'
@@ -17,14 +19,14 @@ module NewRelic
17
19
  :'error_collector.capture_events'
18
20
  buffer_class PrioritySampledBuffer
19
21
 
20
- def record noticed_error, transaction_payload = nil
22
+ def record noticed_error, transaction_payload = nil, span_id = nil
21
23
  return unless enabled?
22
24
 
23
- priority = (transaction_payload && transaction_payload[:priority]) || rand.round(6)
25
+ priority = float!((transaction_payload && transaction_payload[:priority]) || rand)
24
26
 
25
27
  @lock.synchronize do
26
28
  @buffer.append(priority: priority) do
27
- create_event(noticed_error, transaction_payload)
29
+ create_event(noticed_error, transaction_payload, span_id)
28
30
  end
29
31
  notify_if_full
30
32
  end
@@ -32,8 +34,8 @@ module NewRelic
32
34
 
33
35
  private
34
36
 
35
- def create_event noticed_error, transaction_payload
36
- TransactionErrorPrimitive.create noticed_error, transaction_payload
37
+ def create_event noticed_error, transaction_payload, span_id
38
+ TransactionErrorPrimitive.create noticed_error, transaction_payload, span_id
37
39
  end
38
40
  end
39
41
  end
@@ -3,8 +3,8 @@
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
5
  require 'new_relic/agent/transaction/tracing'
6
- require 'new_relic/agent/cross_app_tracing'
7
- require 'new_relic/agent/cross_app_payload'
6
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
7
+ require 'new_relic/agent/distributed_tracing/cross_app_payload'
8
8
 
9
9
  module NewRelic
10
10
  module Agent
@@ -78,7 +78,7 @@ module NewRelic
78
78
  #
79
79
  if txn_info = rmd[NON_HTTP_CAT_TXN_HEADER]
80
80
  payload = CrossAppPayload.new(id, transaction, txn_info)
81
- transaction.cross_app_payload = payload
81
+ transaction.distributed_tracer.cross_app_payload = payload
82
82
 
83
83
  CrossAppTracing.assign_intrinsic_transaction_attributes state
84
84
  end
@@ -112,16 +112,16 @@ module NewRelic
112
112
  NewRelic::Agent.record_api_supportability_metric(:get_response_metadata)
113
113
  return unless CrossAppTracing.cross_app_enabled?
114
114
 
115
- return unless (transaction = Tracer.current_transaction)
116
- return unless (cross_app_payload = transaction.cross_app_payload)
115
+ return unless (txn = Tracer.current_transaction)
116
+ return unless (payload = txn.distributed_tracer.cross_app_payload)
117
117
 
118
118
  # must freeze the name since we're responding with it
119
119
  #
120
- transaction.freeze_name_and_execute_if_not_ignored do
120
+ txn.freeze_name_and_execute_if_not_ignored do
121
121
  # build response payload
122
122
  #
123
123
  rmd = {
124
- NewRelicAppData: cross_app_payload.as_json_array(NON_HTTP_CAT_CONTENT_LENGTH)
124
+ NewRelicAppData: payload.as_json_array(NON_HTTP_CAT_CONTENT_LENGTH)
125
125
  }
126
126
 
127
127
  # obfuscate the generated response metadata JSON
@@ -0,0 +1,28 @@
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
+
5
+ module NewRelic
6
+ module Agent
7
+ module GuidGenerator
8
+
9
+ HEX_DIGITS = (0..15).map{|i| i.to_s(16)}
10
+
11
+ module_function
12
+
13
+ # This method intentionally does not use SecureRandom, because it relies
14
+ # on urandom, which raises an exception in MRI when the interpreter runs
15
+ # out of allocated file descriptors.
16
+ # The guids generated by this method may not be _secure_, but they are
17
+ # random enough for our purposes.
18
+ def generate_guid length = 16
19
+ guid = ''
20
+ length.times do |a|
21
+ guid << HEX_DIGITS[rand(16)]
22
+ end
23
+ guid
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -17,8 +17,14 @@ module NewRelic
17
17
  end
18
18
  end
19
19
 
20
+ # calling hostname with -f on some OS's (NetBSD, FreeBSD, Solaris)
21
+ # produces invalid option error, but doesn't raise exception. Instead,
22
+ # we get back empty string. So, solution here is to check for non-zero
23
+ # exit status and retry the command without the -f flag.
20
24
  def self.get_fqdn
21
- %x[hostname -f].chomp!
25
+ fqdn = %x[hostname -f].chomp!
26
+ fqdn = %x[hostname].chomp! unless $?.exitstatus.zero?
27
+ fqdn
22
28
  rescue => e
23
29
  NewRelic::Agent.logger.debug "Unable to determine fqdn #{e}"
24
30
  nil
@@ -0,0 +1,82 @@
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
+ module Agent
8
+ module HTTPClients
9
+ MUST_IMPLEMENT_ERROR = "Subclasses of %s must implement a :%s method"
10
+ WHINY_NIL_ERROR = "%s cannot initialize with a nil wrapped_response object."
11
+
12
+ # This class provides a public interface for wrapping HTTP requests. This
13
+ # may be used to create wrappers that are compatible with New Relic's
14
+ # external request API.
15
+ #
16
+ # @api public
17
+ class AbstractRequest
18
+
19
+ def []
20
+ raise NotImplementedError, MUST_IMPLEMENT_ERROR % [self.class, __method__]
21
+ end
22
+
23
+ def []=
24
+ raise NotImplementedError, MUST_IMPLEMENT_ERROR % [self.class, __method__]
25
+ end
26
+
27
+ def type
28
+ raise NotImplementedError, MUST_IMPLEMENT_ERROR % [self.class, __method__]
29
+ end
30
+
31
+ def host_from_header
32
+ raise NotImplementedError, MUST_IMPLEMENT_ERROR % [self.class, __method__]
33
+ end
34
+
35
+ def host
36
+ raise NotImplementedError, MUST_IMPLEMENT_ERROR % [self.class, __method__]
37
+ end
38
+
39
+ def method
40
+ raise NotImplementedError, MUST_IMPLEMENT_ERROR % [self.class, __method__]
41
+ end
42
+ end
43
+
44
+ # This class implements the adaptor pattern and is used internally provide
45
+ # uniform access to the underlying HTTP Client's response object
46
+ # NOTE: response_object should be non-nil!
47
+ class AbstractResponse # :nodoc:
48
+
49
+ def initialize wrapped_response
50
+ if wrapped_response.nil?
51
+ raise ArgumentError, WHINY_NIL_ERROR % self.class
52
+ end
53
+ @wrapped_response = wrapped_response
54
+ end
55
+
56
+ def has_status_code?
57
+ !!status_code
58
+ end
59
+
60
+ # most HTTP client libraries report the HTTP status code as an integer, so
61
+ # we expect status_code to be set only if a non-zero value is present
62
+ def status_code
63
+ @status_code ||= get_status_code
64
+ end
65
+
66
+ private
67
+
68
+ def get_status_code_using method_name
69
+ return unless @wrapped_response.respond_to?(method_name)
70
+ code = @wrapped_response.send(method_name).to_i
71
+ code == 0 ? nil : code
72
+ end
73
+
74
+ # Override this method to memoize a non-zero Integer representation
75
+ # of HTTP status code from the response object
76
+ def get_status_code
77
+ get_status_code_using :code
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -1,19 +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
- require 'new_relic/agent/http_clients/abstract_request'
6
+ require_relative 'abstract'
6
7
 
7
8
  module NewRelic
8
9
  module Agent
9
10
  module HTTPClients
10
11
 
11
12
  class CurbRequest
12
- CURB = 'Curb'.freeze
13
- LHOST = 'host'.freeze
14
- UHOST = 'Host'.freeze
13
+ CURB = 'Curb'
14
+ LHOST = 'host'
15
+ UHOST = 'Host'
15
16
 
16
- def initialize( curlobj )
17
+ def initialize curlobj
17
18
  @curlobj = curlobj
18
19
  end
19
20
 
@@ -34,41 +35,45 @@ module NewRelic
34
35
  end
35
36
 
36
37
  def []( key )
37
- @curlobj.headers[ key ]
38
+ @curlobj.headers[key]
38
39
  end
39
40
 
40
- def []=( key, value )
41
- @curlobj.headers[ key ] = value
41
+ def []=(key, value)
42
+ @curlobj.headers[key] = value
42
43
  end
43
44
 
44
45
  def uri
45
- @uri ||= NewRelic::Agent::HTTPClients::URIUtil.parse_and_normalize_url(@curlobj.url)
46
+ @uri ||= URIUtil.parse_and_normalize_url(@curlobj.url)
46
47
  end
47
48
  end
48
49
 
50
+ class CurbResponse < AbstractResponse
49
51
 
50
- class CurbResponse < AbstractRequest
51
-
52
- def initialize(curlobj)
52
+ def initialize wrapped_response
53
+ super wrapped_response
53
54
  @headers = {}
54
- @curlobj = curlobj
55
55
  end
56
56
 
57
57
  def [](key)
58
- @headers[ key.downcase ]
58
+ @headers[key.downcase]
59
59
  end
60
60
 
61
61
  def to_hash
62
62
  @headers.dup
63
63
  end
64
64
 
65
- def append_header_data( data )
66
- key, value = data.split( /:\s*/, 2 )
67
- @headers[ key.downcase ] = value
68
- @curlobj._nr_header_str ||= ''
69
- @curlobj._nr_header_str << data
65
+ def append_header_data data
66
+ key, value = data.split(/:\s*/, 2)
67
+ @headers[key.downcase] = value
68
+ @wrapped_response._nr_header_str ||= String.new
69
+ @wrapped_response._nr_header_str << data
70
70
  end
71
71
 
72
+ private
73
+
74
+ def get_status_code
75
+ get_status_code_using :response_code
76
+ end
72
77
  end
73
78
 
74
79
  end