by Moesif, the API analytics and API monetization platform.
With Moesif Ruby middleware for AWS Lambda, you can automatically log API calls and send them to Moesif for API analytics and monitoring. This middleware allows you to integrate Moesif's API analytics and API monetization features into your Ruby applications with minimal configuration.
If you're new to Moesif, see our Getting Started resources to quickly get up and running.
This middleware expects the Lambda proxy integration type. If you're using AWS Lambda with API Gateway, you are most likely using the proxy integration type.
Before using this middleware, make sure you have the following:
After you log into Moesif Portal, you can get your Moesif Application ID during the onboarding steps. You can always access the Application ID any time by following these steps from Moesif Portal after logging in:
- Select the account icon to bring up the settings menu.
- Select Installation or API Keys.
- Copy your Moesif Application ID from the Collector Application ID field.
Install with Bundler:
bundle install moesif_aws_lambda
See the available configuration options to learn how to configure the middleware for your use case.
def your_original_handler(event:, context:)
# your code
end
moesif_options = {
# application_id is required
"application_id" => 'Your Moesif Application Id',
"debug" => true,
# see list of other options below.
}
# create moesif_middleware object.
$moesif_middleware = Moesif::MoesifAwsMiddleware.new(method(:your_original_handler), moesif_options)
def wrapped_handler(event:, context:)
$moesif_middleware.handle(event: event, context: context);
end
Then set the AWS Lambda handler name to wrapped_handler
instead. For more information, see Define Lambda function handler in Ruby.
For a general troubleshooting guide that can help you solve common problems, see Server Troubleshooting Guide.
Other troubleshooting supports:
The following sections describe the available configuration options for this middleware. You must set these options in a Ruby Hash
object when you create the middleware instance. See the sample Lambda handler function code in example_handler.rb
for better understanding.
Data type |
---|
String |
A string that identifies your application in Moesif.
Data type |
---|
String |
Optional.
Use it to tag requests with the version of your API.
Data type | Return type |
---|---|
Proc
|
String |
Optional, but highly recommended.
A Proc
that takes the event, context, and Lambda result as arguments.
Returns a string that represents the user ID used by your system.
Moesif identifies users automatically. However, due to the differences arising from different frameworks and implementations, set this option to ensure user identification properly.
In the following code snippet, event
and context
are original Lambda handler's input parameters. result
is the return result from your own original Lambda handler.
moesif_options['identify_user'] = Proc.new { |event, context, result|
# Add your custom code that returns a string for user id
'12345'
}
Data type | Return type |
---|---|
Proc
|
String |
Optional.
A Proc
that takes the event, context, and Lambda result as arguments.
Returns a string that represents the company ID for this event. This helps Moesif attribute requests to unique company.
moesif_options['identify_company'] = Proc.new { |event, context, result|
# Add your custom code that returns a string for company id
'67890'
}
Data type | Return type |
---|---|
Proc
|
String |
A Proc
that takes env
, headers
, and body
as arguments.
Returns a string that represents the session token for this event.
Similar to users and companies, Moesif tries to retrieve session tokens automatically. But if it doesn't work for your service, use this option to help identify sessions.
moesif_options['identify_session'] = Proc.new { |event, context, result|
# Add your custom code that returns a string for session/API token
'XXXXXXXXX'
}
Data type | Return type |
---|---|
Proc
|
Hash
|
Optional.
A Proc
that takes env
, headers
, and body
as arguments.
Returns a Hash
that represents a JSON object. This allows you to attach any
metadata to this event.
moesif_options['get_metadata'] = Proc.new { |event, context, result|
# Add your custom code that returns a dictionary
value = {
'datacenter' => 'westus',
'deployment_version' => 'v1.2.3'
}
value
}
Data type | Return type |
---|---|
Proc
|
EventModel
|
Optional.
A Proc that takes an EventModel
as an argument and returns an EventModel
.
This option allows you to modify headers or body of an event before sending the event to Moesif.
moesif_options['mask_data'] = Proc.new { |event_model|
# Add your custom code that returns a event_model after modifying any fields
event_model.response.body.password = nil
event_model
}
For more information and the spec of Moesif's event model, see the source code of Moesif API library for Ruby or Moesif Ruby API documentation
Data type | Return type |
---|---|
Proc
|
Boolean |
Optional.
A Proc
that takes env
, headers
, and body
as arguments.
Returns a boolean. Return true
if you want to skip a particular event.
moesif_options['skip'] = Proc.new { |event, context, result|
# Add your custom code that returns true to skip logging the API call
if event.key?("rawPath")
# Skip probes to health page
event["rawPath"].include? "/health"
else
false
end
}
Data type | Default |
---|---|
Boolean |
false
|
Optional.
If true
, the middleware prints out debug messages. In debug mode, the processing is not done in backend thread.
Data type | Default |
---|---|
Boolean |
true
|
Optional.
If false
, doesn't log request and response body to Moesif.
If you want to update User or Company profile using this SDK, use the following methods:
$moesif_middleware.update_user(user_profile)
$moesif_middleware.update_user_batch(user_profiles)
$moesif_middleware.update_company(company_profile)
$moesif_middleware.update_company_batch(company_profiles)
For information about the structure of profiles, see Moesif Ruby API documentation
See example_handler.rb
that contains an example Lambda handler using this middleware.
For AWS Lambda with Ruby, you have to bundle the gem dependencies into a ZIP file archive. For instructions, see Deploy Ruby Lambda functions with .zip file archives.
In the file where you define your Lambda handler, you may have to specify the location of the dependencies:
load_paths = Dir[
"./vendor/bundle/ruby/2.7.0/gems/**/lib"
]
$LOAD_PATH.unshift(*load_paths)
If you face any issues using this middleware, try the troubheshooting guidelines. For further assistance, reach out to our support team.
Explore other integration options from Moesif: