Class: ApplicationInsights::Channel::SenderBase

Inherits:
Object
  • Object
show all
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

AsynchronousSender, SynchronousSender

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_endpoint_uri) ⇒ SenderBase

Initializes a new instance of the class.



16
17
18
19
20
# File 'lib/application_insights/channel/sender_base.rb', line 16

def initialize(service_endpoint_uri)
  @service_endpoint_uri = service_endpoint_uri
  @queue = nil
  @send_buffer_size = 100
end

Instance Attribute Details

#queueQueueBase

The queue that this sender is draining. While ApplicationInsights::Channel::SenderBase doesn’t implement any means of doing so, derivations of this class do.



29
30
31
# File 'lib/application_insights/channel/sender_base.rb', line 29

def queue
  @queue
end

#send_buffer_sizeFixnum

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.



34
35
36
# File 'lib/application_insights/channel/sender_base.rb', line 34

def send_buffer_size
  @send_buffer_size
end

#service_endpoint_uriString

The service endpoint URI where this sender will send data to.



24
25
26
# File 'lib/application_insights/channel/sender_base.rb', line 24

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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/application_insights/channel/sender_base.rb', line 39

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)
end