Module: ApplicationInsights::UnhandledException

Defined in:
lib/application_insights/unhandled_exception.rb

Class Method Summary collapse

Class Method Details

.collect(instrumentation_key) ⇒ Object

Auto collects unhandled exception and send to the Application Insights service.

Examples:

require 'application_insights'
ApplicationInsights::UnhandledException.collect('<YOUR INSTRUMENTATION KEY GOES HERE>')
raise Exception, 'Boom!'

Parameters:

  • instrumentation_key (string)

    used to identify which Application Insights application this data is for.



19
20
21
22
23
24
# File 'lib/application_insights/unhandled_exception.rb', line 19

def self.collect(instrumentation_key)
  at_exit do
    # Avoid sending exception more than once if this method got invoked multiple times
    send(instrumentation_key) unless @sender
  end
end

.send(instrumentation_key, telemetry_sender = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Send the last raised exception to the Application Insights service if telemetry_sender is not customized.

Parameters:

  • instrumentation_key (string)

    used to identify which Application Insights application this data is for.

  • telemetry_sender (SenderBase) (defaults to: nil)

    used to send the last raised exception.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/application_insights/unhandled_exception.rb', line 32

def self.send(instrumentation_key, telemetry_sender = nil)
  if $! && !$!.is_a?(SystemExit) && !$!.is_a?(SignalException)
    if telemetry_sender
      @sender = telemetry_sender
    elsif !@sender
      # Use a synchronized sender to guarantee the data would be sent out once flush
      @sender = Channel::SynchronousSender.new
    end

    queue = Channel::SynchronousQueue.new @sender
    channel = Channel::TelemetryChannel.new nil, queue
    client = TelemetryClient.new instrumentation_key, channel
    client.track_exception($!, handled_at: 'Unhandled')
    client.flush
  end
end