Class: AzureApplicationInsights::Channel::SenderBase

Inherits:
Object
  • Object
show all
Defined in:
lib/azure_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.

Parameters:

  • service_endpoint_uri (String)

    the address of the service to send telemetry data to.



20
21
22
23
24
25
# File 'lib/azure_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

#loggerObject

The logger for the sender.



42
43
44
# File 'lib/azure_application_insights/channel/sender_base.rb', line 42

def logger
  @logger
end

#queueQueueBase

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

Returns:

  • (QueueBase)

    the queue instance that this sender is draining.



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

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.

Returns:

  • (Fixnum)

    the maximum number of items in a telemetry batch.



39
40
41
# File 'lib/azure_application_insights/channel/sender_base.rb', line 39

def send_buffer_size
  @send_buffer_size
end

#service_endpoint_uriString

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

Returns:

  • (String)

    the service endpoint URI.



29
30
31
# File 'lib/azure_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.

Parameters:



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/azure_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('azure_application_insights') { "Failed to send data: #{response.message}" }
  end
end