Class: ApplicationInsights::Channel::SenderBase
- Inherits:
-
Object
- Object
- ApplicationInsights::Channel::SenderBase
- Defined in:
- lib/application_insights/channel/sender_base.rb
Overview
The base class for all types of senders for use in conjunction with an implementation of QueueBase. The queue will notify the sender that it needs to pick up items. The concrete sender implementation will listen to these notifications and will pull items from the queue using QueueBase#pop getting at most #send_buffer_size items. It will then call #send using the list of items pulled from the queue.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#logger ⇒ Object
The logger for the sender.
-
#queue ⇒ QueueBase
The queue that this sender is draining.
-
#send_buffer_size ⇒ Fixnum
The buffer size for a single batch of telemetry.
-
#service_endpoint_uri ⇒ String
The service endpoint URI where this sender will send data to.
Instance Method Summary collapse
-
#initialize(service_endpoint_uri) ⇒ SenderBase
constructor
Initializes a new instance of the class.
-
#send(data_to_send) ⇒ Object
Immediately sends the data passed in to #service_endpoint_uri.
Constructor Details
#initialize(service_endpoint_uri) ⇒ SenderBase
Initializes a new instance of the class.
20 21 22 23 24 25 |
# File 'lib/application_insights/channel/sender_base.rb', line 20 def initialize(service_endpoint_uri) @service_endpoint_uri = service_endpoint_uri @queue = nil @send_buffer_size = 100 @logger = Logger.new(STDOUT) end |
Instance Attribute Details
#logger ⇒ Object
The logger for the sender.
42 43 44 |
# File 'lib/application_insights/channel/sender_base.rb', line 42 def logger @logger end |
#queue ⇒ QueueBase
The queue that this sender is draining. While ApplicationInsights::Channel::SenderBase doesn’t implement any means of doing so, derivations of this class do.
34 35 36 |
# File 'lib/application_insights/channel/sender_base.rb', line 34 def queue @queue end |
#send_buffer_size ⇒ Fixnum
The buffer size for a single batch of telemetry. This is the maximum number of items in a single service request that this sender is going to send.
39 40 41 |
# File 'lib/application_insights/channel/sender_base.rb', line 39 def send_buffer_size @send_buffer_size end |
#service_endpoint_uri ⇒ String
The service endpoint URI where this sender will send data to.
29 30 31 |
# File 'lib/application_insights/channel/sender_base.rb', line 29 def service_endpoint_uri @service_endpoint_uri end |
Instance Method Details
#send(data_to_send) ⇒ Object
Immediately sends the data passed in to #service_endpoint_uri. If the service request fails, the passed in items are pushed back to the #queue.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/application_insights/channel/sender_base.rb', line 48 def send(data_to_send) uri = URI(@service_endpoint_uri) headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json; charset=utf-8', 'Content-Encoding' => 'gzip' } request = Net::HTTP::Post.new(uri.path, headers) # Use JSON.generate instead of to_json, otherwise it will # default to ActiveSupport::JSON.encode for Rails app json = JSON.generate(data_to_send) compressed_data = compress(json) request.body = compressed_data http = Net::HTTP.new uri.hostname, uri.port if uri.scheme.downcase == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end response = http.request(request) http.finish if http.started? if !response.kind_of? Net::HTTPSuccess @logger.warn('application_insights') { "Failed to send data: #{response.}" } end end |