richardstyles/laravel-vapor-support

Adds additional support for Laravel vapor applications

v1.0.2 2022-06-23 17:04 UTC

README

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31

Laravel Vapor Support

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A small support package to help large Vapor applications which may occasionally hit "Internal Server Error" due to large request sizes.

This package provides a middleware to monitor the main response:

  • Illuminate\Http\Response
  • Illuminate\Http\JsonResponse
  • Symfony\Component\HttpFoundation\BinaryFileResponse

There is also support for:

  • Symfony\Component\HttpFoundation\StreamedResponse

However, because a streamed response length is generally unknown, by default a 'info' level log is recorded to aid debugging.

This package provides a middleware which can be used to monitor the length of the response body. This allows you to be notified if a page is starting to exceed the Vapor/AWS limits. The default logs included allow for route name (if set), url and size to be logged to help you pinpoint any pages of concern.

Installation

You can install the package via composer:

composer require richardstyles/laravel-vapor-support

You can publish the config file with:

php artisan vendor:publish --tag="laravel-vapor-support-config"

This is the contents of the published config file:

return [
    'warning_size' => env('VAPOR_SUPPORT_RESPONSE_WARNING_SIZE', 5000000),
    'warning_action' => \RichardStyles\LaravelVaporSupport\Actions\ResponseSizeWarning::class,


    'limit_size' => env('VAPOR_SUPPORT_RESPONSE_LIMIT_SIZE', 6000000),
    'limit_action' => \RichardStyles\LaravelVaporSupport\Actions\ResponseSizeLimit::class,

    'stream_action' => \RichardStyles\LaravelVaporSupport\Actions\StreamResponseMonitor::class,
];

Usage

Simply add the middleware to either the Kernal.

app/Http/Kernel.php

protected $middlewareGroups = [
        'web' => [
             ...
            \RichardStyles\LaravelVaporSupport\Http\Middleware\ResponseLimitMonitor::class,
        ],
        ...

Or as a named middleware to use on individual routes.

protected $routeMiddleware = [
        ...
        'response.monitor' => \RichardStyles\LaravelVaporSupport\Http\Middleware\ResponseLimitMonitor::class,
    ];

For monitoring response sizes (in bytes), these can be set in your .ENV, or by modifying the config.

Examples set very low

VAPOR_SUPPORT_RESPONSE_WARNING_SIZE=1000
VAPOR_SUPPORT_RESPONSE_LIMIT_SIZE=5000

Using the example above would cause;

  • The Warning class run when response body exceeds "1000" bytes.
  • The Limit class runs when response body exceeds "5000" bytes.

The config also allows the base classes to be overridden. The Limit and Warning classes must implement RichardStyles\LaravelVaporSupport\Contracts\HandlesResponsesLimit The stream response should implement RichardStyles\LaravelVaporSupport\Contracts\HandlesStreamResponse as with streamed responses you cannot easily know it's length.

The default actions taken by this package is to Log either:

  • Log info on streamed response
  • Log warning when request length exceeds warning_size
  • Log critical when request length exceeds limit_size

By implementing your own classes and setting these within the config vapor-support you can notify/alert however your application requires, such as to Bugsnag or other third party services.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Support

If you are having general issues with this package, feel free to contact me on Twitter.

If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request with a failing test.

If you're using this package, I'd love to hear your thoughts. Thanks!

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.