Module: Telemetry::Logger::ExceptionHandler

Included in:
Telemetry::Logger
Defined in:
lib/telemetry/logger/exception_handler.rb

Instance Method Summary collapse

Instance Method Details

#elastic_apm?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/telemetry/logger/exception_handler.rb', line 20

def elastic_apm?
  @elastic_apm unless @elastic_apm.nil?

  @elastic_apm = Kernel.const_defined? 'ElasticAPM'
end

#exception(exc, level: 'error', backtrace: true, backtrace_limit: 20, **opts) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/telemetry/logger/exception_handler.rb', line 4

def exception(exc, level: 'error', backtrace: true, backtrace_limit: 20, **opts)
  level = 'unknown' unless %w[debug info warn error fatal].include? level.to_s
  Telemetry::Logger.send(level, "#{exc.class}: #{exc.message}")
  Telemetry::Logger.send(level, exc.backtrace[0..backtrace_limit]) if backtrace && !exc.backtrace.nil?

  send_to_apm(exc, level: level)

  raise(exc) if opts[:raise]
end

#open_telemetry?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/telemetry/logger/exception_handler.rb', line 26

def open_telemetry?
  @open_telemetry unless @open_telemetry.nil?

  @open_telemetry = Kernel.const_defined? 'OpenTelemetry'
end

#send_to_apm(exc, **opts) ⇒ Object



14
15
16
17
18
# File 'lib/telemetry/logger/exception_handler.rb', line 14

def send_to_apm(exc, **opts)
  handled = opts[:handled] || %w[info debug].include?(opts[:level])
  ::ElasticAPM.report(exc, handled: handled) if elastic_apm?
  ::OpenTelemetry.handle_error(exception: exc, message: exc.message) if open_telemetry?
end