Class: LambdaLoadout::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_loadout/middleware.rb

Overview

Decorator-style wrapper for Lambda handlers

Provides a simple DSL for wrapping Lambda handlers with observability.

Examples:

Basic usage

LambdaLoadout::Handler.configure do |config|
  config.service = "payment"
  config.namespace = "MyApp"
end

def lambda_handler(event:, context:)
  LambdaLoadout::Handler.call(event, context) do |logger, metrics|
    logger.info("Processing event")
    metrics.add_metric(name: "EventProcessed", unit: "Count", value: 1)

    { statusCode: 200 }
  end
end

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.capture_cold_startObject

Returns the value of attribute capture_cold_start.



86
87
88
# File 'lib/lambda_loadout/middleware.rb', line 86

def capture_cold_start
  @capture_cold_start
end

.log_levelObject

Returns the value of attribute log_level.



86
87
88
# File 'lib/lambda_loadout/middleware.rb', line 86

def log_level
  @log_level
end

.namespaceObject

Returns the value of attribute namespace.



86
87
88
# File 'lib/lambda_loadout/middleware.rb', line 86

def namespace
  @namespace
end

.serviceObject

Returns the value of attribute service.



86
87
88
# File 'lib/lambda_loadout/middleware.rb', line 86

def service
  @service
end

Class Method Details

.call(event, context, capture_cold_start: @capture_cold_start || true) {|logger, metrics| ... } ⇒ Object

Call handler with observability

Parameters:

  • event (Hash)

    Lambda event

  • context (Object)

    Lambda context

  • capture_cold_start (Boolean) (defaults to: @capture_cold_start || true)

    Override cold start capture

Yields:

Returns:

  • (Object)

    Result of block execution



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lambda_loadout/middleware.rb', line 103

def call(event, context, capture_cold_start: @capture_cold_start || true, &block)
  logger = LambdaLoadout::Logger.new(
    service: @service,
    level: @log_level || :info
  )

  metrics = LambdaLoadout::Metrics.new(
    namespace: @namespace || 'LambdaLoadout',
    service: @service
  )

  logger.inject_lambda_context(context)
  logger.info('Lambda invocation started', event_keys: event.keys)

  begin
    result = block.call(logger, metrics)
    metrics.add_cold_start_metric(context) if capture_cold_start
    logger.info('Lambda invocation completed successfully')
    result
  rescue StandardError => e
    logger.exception(e, message: 'Lambda invocation failed')
    metrics.add_metric(name: 'LambdaError', unit: 'Count', value: 1)
    metrics.add_dimension(name: 'error_type', value: e.class.name)
    raise
  ensure
    begin
      metrics.flush
    rescue StandardError
      nil
    end
  end
end

.configure {|self| ... } ⇒ void

This method returns an undefined value.

Configure global handler defaults

Yields:

  • (self)

    Configuration block



92
93
94
# File 'lib/lambda_loadout/middleware.rb', line 92

def configure
  yield self
end