Sha256: 3b5f26fe6e6802a2809c17a2ee423ab37793eb16677dc064a9a46ffb060ed658

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'honeybadger/plugin'
require 'honeybadger/util/lambda'

module Honeybadger
  module Plugins
    module LambdaExtension
      # Wrap Lambda handlers so exceptions can be automatically captured
      #
      # Usage:
      #
      # # Automatically included in the top-level main object
      # hb_wrap_handler :my_handler_1, :my_handler_2
      #
      # def my_handler_1(event:, context:)
      # end
      #
      # class MyLambdaApp
      #   extend ::Honeybadger::Plugins::LambdaExtension
      #
      #   hb_wrap_handler :my_handler_1, :my_handler_2
      #
      #   def self.my_handler_1(event:, context:)
      #   end
      # end
      def hb_wrap_handler(*handler_names)
        mod = Module.new do
          handler_names.each do |handler|
            define_method(handler) do |event:, context:|
              Honeybadger.context(aws_request_id: context.aws_request_id) if context.respond_to?(:aws_request_id)

              super(event: event, context: context)
            rescue => e
              Honeybadger.notify(e)
              raise
            end
          end
        end

        self.singleton_class.prepend(mod)
      end
    end

    # @api private
    Plugin.register :lambda do
      requirement { Util::Lambda.lambda_execution? }

      execution do
        config[:sync] = true
        config[:'exceptions.notify_at_exit'] = false

        main = TOPLEVEL_BINDING.eval("self")
        main.extend(LambdaExtension)

        (config[:before_notify] ||= []) << lambda do |notice|
          data = Util::Lambda.normalized_data

          notice.component = data["function"]
          notice.action = data["handler"]
          notice.details["Lambda Details"] = data

          if (trace_id = Util::Lambda.trace_id)
            notice.context[:lambda_trace_id] = trace_id
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
honeybadger-4.12.0 lib/honeybadger/plugins/lambda.rb