Giter VIP home page Giter VIP logo

health-checks's Introduction

Health Checks

Build Status

Add health checks to your Laravel applications with this package.

Examples

All passed DB failed Queue failed Url stats

Install

First, use Composer to install this package.

$ composer require phpsafari/health-checks

After the package has been installed, go ahead and add ``PhpSafari\ServiceProvider\HealthCheckServiceProvider::classto yourconfig/app.php` file.

After doing this, you can publish the health.php configuration file into your config folder by running:

$ php artisan vendor:publish --tag=health

Usage

Inside the config/health.php file is where you configure all your available health checks. By default, this package comes packed with a few checks already:

  • CorrectEnvironment will check if your application's environment is set to production
  • DatabaseOnline will check your database connection
  • DatabaseUpToDate will check if your application has any migrations that hasn't been migrated yet
  • DebugModeOff will check if debug mode is off
  • QueueProcessing will check if the queue is running and jobs are getting processed
  • PathIsWritable will check if a provided path is writable
  • LogLevel will check if log level is set to the given value
  • MaxRatioOf500Responses will check if the ratio of 500 response are above a given threshold (The last 60 min)
  • MaxResponseTimeAvg will check if average response time for all request are above a given threshold (The last 60 min)

To run a health check of your application, run:

$ php artisan health:check

You can also use Laravel's scheduler to schedule your health checks:

$schedule->command('health:check')->hourly();

Url based health checks

You can also run a health check by hitting the https://<APP_URL>/_health url in a browser or with a tool like Pingdom.

Note: _This feature can be disabled in the config/health.php file, by setting route.enabled to false.

You can navigate to https://<APP_URL>/_health/stats and get all stats, including avg response time,

Creating your own health checks

In order to create your own health checks, you just need to extend the PhpSafari\Checks\HealthCheck class and implement the run() method.

Example:

class IsFriday extends PhpSafari\Checks\HealthCheck
{
    public function run(): bool
    {
        return Carbon::now()->isFriday();
    }
}

Then add new IsFriday() to the list of checks in config/health.php.

Testing

$ phpunit tests/

Security

If you discover any security related issues, please email :author_email instead of using the issue tracker.

Credits

License

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

health-checks's People

Contributors

petersuhm avatar vistik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

health-checks's Issues

unimplemented method

For queue helth check:

Whoops\Exception\ErrorException : Class PhpSafari\Jobs\CheckQueueIsRunning contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Illuminate\Queue\Jobs\Job::getJobId)

I added this implementation to stop this error:

public function getJobId() { }

Thank you.

String based syntax for health checks in the configuration file

The way that health checks are set up in the config breaks the functionality of php artisan config:cache

The issue is how the following gets cached onto bootstrap/cache/config.php

            'checks' => [
                new DatabaseOnline(),
                new DatabaseUpToDate(),
                new DebugModeOff(),
                new LogLevel('error'),
            ]

The generated cache breaks the site during bootstrap since it requires more dependencies to be present. My work around was to modify the constructor of the CheckList.php to instantiate the classes based on the type.

<?php

namespace PhpSafari\Utils;

use PhpSafari\Checks\HealthCheck;
use Vistik\Collections\TypedCollection;

class CheckList extends TypedCollection
{
  public function __construct($checks)
  {
    $cast = function ($check) {
      if ($check instanceof HealthCheck) return $check;
      if (is_string($check)) return new $check;
      if (is_array($check)) return new $check[0](...array_slice($check, 1));
    };

    parent::__construct(array_map($cast, $checks));
  }

  protected $type = HealthCheck::class;
}

So now in my health.php I can do

'checks' => [
        DatabaseOnline::class,
        [PathIsWritable::class, storage_path()],
        [PathIsWritable::class, storage_path('logs')],
        [PathIsWritable::class, storage_path('framework/sessions')],
        [PathIsWritable::class, storage_path('framework/cache')],
      ],

Lumen integration

It would make a log of sense for this to have Lumen integration to have API health...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.