<commit_before>require 'honeybadger/plugin'
require 'honeybadger/util/lambda'

module Honeybadger
  module Plugins
    module LambdaExtension
      unless self.respond_to?(:hb_lambda)
        # Decorator for Lambda handlers so exceptions can be automatically captured
        #
        # Usage:
        #
        # hb_lambda def my_lambda_handler(event:, context:)
        #   # Handler code
        # end
        #
        # class Lambda
        #   hb_lambda def self.my_lambda_handler(event:, context:)
        #     # Handler code
        #   end
        # end
        def hb_lambda(handler_name)
          original_method = method(handler_name)
          self.define_singleton_method(handler_name) do |event:, context:|
            Honeybadger.context({ aws_request_id: context.aws_request_id }) if context.respond_to?(:aws_request_id)

            original_method.call(event: event, context: context)
          rescue => e
            Honeybadger.notify(e)
            # Bubble the error up to Lambda, but disable other reporting to avoid duplicates in local emulated environments
            # Since the process immediately exits, nothing else is affected
            Honeybadger.config[:'exceptions.notify_at_exit'] = false
            raise
          end
        end
      end
    end


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

      execution do
        config[:sync] = true

        # AWS Lambda handlers may be top-level methods or class methods
        # See https://docs.aws.amazon.com/lambda/latest/dg/ruby-handler.html
        # So we provide a decorator for both cases
        main = TOPLEVEL_BINDING.eval("self")
        main.extend(LambdaExtension)
        Class.include(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<commit_msg>Cleanup<commit_after>require 'honeybadger/plugin'
require 'honeybadger/util/lambda'

module Honeybadger
  module Plugins
    module LambdaExtension
      unless self.respond_to?(:hb_lambda)
        # Decorator for Lambda handlers so exceptions can be automatically captured
        #
        # Usage:
        #
        # hb_lambda def my_lambda_handler(event:, context:)
        #   # Handler code
        # end
        #
        # class Lambda
        #   hb_lambda def self.my_lambda_handler(event:, context:)
        #     # Handler code
        #   end
        # end
        def hb_lambda(handler_name)
          original_method = method(handler_name)
          self.define_singleton_method(handler_name) do |event:, context:|
            Honeybadger.context({ aws_request_id: context.aws_request_id }) if context.respond_to?(:aws_request_id)

            original_method.call(event: event, context: context)
          rescue => e
            Honeybadger.notify(e)
            # Bubble the error up to Lambda, but disable other reporting to avoid duplicates in local emulated environments
            # Since the process immediately exits, nothing else is affected
            Honeybadger.config[:'exceptions.notify_at_exit'] = false
            raise
          end
        end
      end
    end


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

      execution do
        config[:sync] = true

        # AWS Lambda handlers may be top-level methods or class methods
        # See https://docs.aws.amazon.com/lambda/latest/dg/ruby-handler.html
        # So we provide a decorator for both cases
        main = TOPLEVEL_BINDING.eval("self")
        main.extend(LambdaExtension)
        ::Class.include(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
{'fraction_non_alphanumeric': 0.08272058823529412, 'fraction_numerical': 0.0, 'mean_word_length': 2.1890109890109892, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1344221', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1168, 'n_words': 299}