Class: ApplicationInsights::Channel::AsynchronousSender

Inherits:
SenderBase
  • Object
show all
Defined in:
lib/application_insights/channel/asynchronous_sender.rb

Overview

An asynchronous sender that works in conjunction with the AsynchronousQueue. The sender object will start a worker thread that will pull items from the SenderBase#queue. The thread will be created when the client calls #start and will check for queue items every #send_interval seconds. The worker thread can also be forced to check the queue by setting the ApplicationInsights::Channel::AsynchronousQueue#flush_notification event.

  • If no items are found, the thread will go back to sleep.

  • If items are found, the worker thread will send items to the specified service in batches of SenderBase#send_buffer_size.

If no queue items are found for #send_time seconds, the worker thread will shut down (and #start will need to be called again).

Constant Summary collapse

SERVICE_ENDPOINT_URI =
'https://dc.services.visualstudio.com/v2/track'

Instance Attribute Summary collapse

Attributes inherited from SenderBase

#logger, #queue, #send_buffer_size, #service_endpoint_uri

Instance Method Summary collapse

Methods inherited from SenderBase

#send

Constructor Details

#initialize(service_endpoint_uri = SERVICE_ENDPOINT_URI) ⇒ AsynchronousSender

Initializes a new instance of the class.

Parameters:

  • service_endpoint_uri (String) (defaults to: SERVICE_ENDPOINT_URI)

    the address of the service to send telemetry data to.



24
25
26
27
28
29
30
31
32
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 24

def initialize(service_endpoint_uri = SERVICE_ENDPOINT_URI)
  @send_interval = 1.0
  @send_remaining_time = 0
  @send_time = 3.0
  @lock_work_thread = Mutex.new
  @work_thread = nil
  @start_notification_processed = true
  super service_endpoint_uri
end

Instance Attribute Details

#send_intervalFixnum

The time span in seconds at which the the worker thread will check the SenderBase#queue for items (defaults to: 1.0).

Returns:

  • (Fixnum)

    the interval in seconds.



37
38
39
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 37

def send_interval
  @send_interval
end

#send_timeFixnum

The time span in seconds for which the worker thread will stay alive if no items are found in the SenderBase#queue (defaults to 3.0).

Returns:

  • (Fixnum)

    the interval in seconds.



42
43
44
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 42

def send_time
  @send_time
end

#work_threadThread (readonly)

The worker thread which checks queue items and send data every (#send_interval) seconds or upon flush.

Returns:

  • (Thread)

    the work thread



47
48
49
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 47

def work_thread
  @work_thread
end

Instance Method Details

#startObject

Calling this method will create a worker thread that checks the SenderBase#queue every #send_interval seconds for a total duration of #send_time seconds for new items. If a worker thread has already been created, calling this method does nothing.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/application_insights/channel/asynchronous_sender.rb', line 53

def start
  @start_notification_processed = false
  # Maintain one working thread at one time
  unless @work_thread
    @lock_work_thread.synchronize do
      unless @work_thread
        local_send_interval = [@send_interval, 0.1].max
        @send_remaining_time = [@send_time, local_send_interval].max
        @work_thread = Thread.new { run }
        @work_thread.abort_on_exception = false
      end
    end
  end
end