Skip to content

Instantly share code, notes, and snippets.

@alariva
Last active December 18, 2018 14:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alariva/aa2c31aa37568981071ebf33ed93f3f0 to your computer and use it in GitHub Desktop.
Save alariva/aa2c31aa37568981071ebf33ed93f3f0 to your computer and use it in GitHub Desktop.
Check Rollbar service with beyondcode/laravel-self-diagnosis
<?php
// File: app/SelfDiagnosis/Checks/CanReportToRollbar.php
namespace App\SelfDiagnosis\Checks;
use BeyondCode\SelfDiagnosis\Checks\Check;
use Dotenv\Dotenv;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
use Rollbar\RollbarLogger;
/************************************************
* Ariel Vallese <alariva@gmail.com>
* 12-12-2018 https://alariva.com
*
* Perform a test-ping to Rollbar Service for
* application post-deploy self diagnosis.
* v1.2
************************************************/
class CanReportToRollbar implements Check
{
/**
* Suggested corrective actions.
*
* @var string
*/
protected $suggests;
/**
* The name of the check.
*
* @param array $config
*
* @return string
*/
public function name(array $config): string
{
return 'Can report log messages to Rollbar service.';
}
/**
* Perform the actual verification of this check.
*
* @param array $config
*
* @return bool
*/
public function check(array $config): bool
{
try {
$rollbar = app()->make(\Rollbar\RollbarLogger::class);
$response = $rollbar->log('info', 'SELF-DIAGNOSIS PING CHECK: OK');
$success = $response->wasSuccessful();
if (!$success) {
$status = $response->getStatus();
$this->suggests = "Rollbar service responded with an error status code:[{$status}]";
}
return $success;
} catch (\Illuminate\Contracts\Container\BindingResolutionException $e) {
$this->suggests = 'Verify your config access token and log level are set and correct.';
return false;
} catch (\Exception $e) {
$this->suggests = 'Verify your config, package and API versions and connectivity.';
return false;
}
}
/**
* The error message to display in case the check does not pass.
*
* @param array $config
*
* @return string
*/
public function message(array $config): string
{
return 'Could not report a log message to Rollbar service. '.$this->suggests;
}
}
// File: app/config/logging.php
// ...
'rollbar' => [
'access_token' => env('ROLLBAR_TOKEN'), // Keep on all versions
'token' => env('ROLLBAR_TOKEN'), // Keep if using Rollbar <= 3.x, otherwise remove (*)
'level' => env('ROLLBAR_LEVEL'),
],
// ...
// (*) SEE
// https://github.com/rollbar/rollbar-php-laravel/issues/64
// File: app/config/self-diagnosis.php
// ...
/*
* Environment specific checks that will only be performed for the corresponding environment.
*/
'environment_checks' => [
'development' => [
// ...
],
'production' => [
// ...
\App\SelfDiagnosis\Checks\CanReportToRollbar::class, // Add this line
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment