Skip to content

Commit

Permalink
Add Laravel about command registration (#768)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Bouma <alex@bouma.me>
  • Loading branch information
SebastiaanKloos and stayallive authored Aug 29, 2023
1 parent ba87236 commit bf155b8
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Sentry/Laravel/Console/AboutCommandIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Sentry\Laravel\Console;

use Sentry\Client;
use Sentry\Laravel\Version;
use Sentry\State\HubInterface;

class AboutCommandIntegration
{
public function __invoke(HubInterface $hub): array
{
$client = $hub->getClient();

if ($client === null) {
return [
'Enabled' => '<fg=red;options=bold>NOT CONFIGURED</>',
'Laravel SDK Version' => Version::SDK_VERSION,
'PHP SDK Version' => Client::SDK_VERSION,
];
}

$options = $client->getOptions();

// Note: order is not important since Laravel orders these alphabetically
return [
'Enabled' => $options->getDsn() ? '<fg=green;options=bold>YES</>' : '<fg=red;options=bold>MISSING DSN</>',
'Environment' => $options->getEnvironment() ?: '<fg=yellow;options=bold>NOT SET</>',
'Laravel SDK Version' => Version::SDK_VERSION,
'PHP SDK Version' => Client::SDK_VERSION,
'Release' => $options->getRelease() ?: '<fg=yellow;options=bold>NOT SET</>',
'Sample Rate Errors' => $this->formatSampleRate($options->getSampleRate()),
'Sample Rate Performance Monitoring' => $this->formatSampleRate($options->getTracesSampleRate(), $options->getTracesSampler() !== null),
'Sample Rate Profiling' => $this->formatSampleRate($options->getProfilesSampleRate()),
'Send Default PII' => $options->shouldSendDefaultPii() ? '<fg=green;options=bold>ENABLED</>' : '<fg=yellow;options=bold>DISABLED</>',
];
}

private function formatSampleRate(?float $sampleRate, bool $hasSamplerCallback = false): string
{
if ($hasSamplerCallback) {
return '<fg=green;options=bold>CUSTOM SAMPLER</>';
}

if ($sampleRate === null) {
return '<fg=yellow;options=bold>NOT SET</>';
}

return number_format($sampleRate * 100) . '%';
}
}
19 changes: 19 additions & 0 deletions src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Http\Kernel as HttpKernelInterface;
use Illuminate\Foundation\Application as Laravel;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Http\Request;
use Illuminate\Log\LogManager;
use Illuminate\Support\Facades\Config;
use Laravel\Lumen\Application as Lumen;
use RuntimeException;
use Sentry\Client;
use Sentry\ClientBuilder;
use Sentry\ClientBuilderInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\Integration as SdkIntegration;
use Sentry\Laravel\Console\AboutCommandIntegration;
use Sentry\Laravel\Console\PublishCommand;
use Sentry\Laravel\Console\TestCommand;
use Sentry\Laravel\Features\Feature;
Expand Down Expand Up @@ -91,6 +95,8 @@ public function boot(): void
}

$this->registerArtisanCommands();

$this->registerAboutCommandIntegration();
}
}

Expand Down Expand Up @@ -201,6 +207,19 @@ protected function registerArtisanCommands(): void
]);
}

/**
* Register the `php artisan about` command integration.
*/
protected function registerAboutCommandIntegration(): void
{
// The about command is only available in Laravel 9 and up so we need to check if it's available to us
if (!class_exists(AboutCommand::class)) {
return;
}

AboutCommand::add('Sentry', AboutCommandIntegration::class);
}

/**
* Configure and register the Sentry client with the container.
*/
Expand Down
88 changes: 88 additions & 0 deletions test/Sentry/Console/AboutCommandIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Sentry\Console;

use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Support\Facades\Artisan;
use Sentry\Client;
use Sentry\Laravel\Tests\TestCase;
use Sentry\Laravel\Version;
use Sentry\State\Hub;
use Sentry\State\HubInterface;

class AboutCommandIntegrationTest extends TestCase
{
protected function setUp(): void
{
if (!class_exists(AboutCommand::class)) {
$this->markTestSkipped('The about command is only available in Laravel 9.0+');
}

parent::setUp();
}

public function testAboutCommandContainsExpectedData(): void
{
$this->resetApplicationWithConfig([
'sentry.release' => '1.2.3',
'sentry.environment' => 'testing',
'sentry.traces_sample_rate' => 0.95,
]);

$expectedData = [
'environment' => 'testing',
'release' => '1.2.3',
'sample_rate_errors' => '100%',
'sample_rate_profiling' => 'NOT SET',
'sample_rate_performance_monitoring' => '95%',
'send_default_pii' => 'DISABLED',
'php_sdk_version' => Client::SDK_VERSION,
'laravel_sdk_version' => Version::SDK_VERSION,
];

$actualData = $this->runArtisanAboutAndReturnSentryData();

foreach ($expectedData as $key => $value) {
$this->assertArrayHasKey($key, $actualData);
$this->assertEquals($value, $actualData[$key]);
}
}

public function testAboutCommandContainsExpectedDataWithoutHubClient(): void
{
$this->app->bind(HubInterface::class, static function () {
return new Hub(null);
});

$expectedData = [
'enabled' => 'NOT CONFIGURED',
'php_sdk_version' => Client::SDK_VERSION,
'laravel_sdk_version' => Version::SDK_VERSION,
];

$actualData = $this->runArtisanAboutAndReturnSentryData();

foreach ($expectedData as $key => $value) {
$this->assertArrayHasKey($key, $actualData);
$this->assertEquals($value, $actualData[$key]);
}
}

private function runArtisanAboutAndReturnSentryData(): array
{
$this->withoutMockingConsoleOutput();

$this->artisan(AboutCommand::class, ['--json' => null]);

$output = Artisan::output();

// This might seem like a weird thing to do, but it's necessary to make sure that that the command didn't have any side effects on the container
$this->refreshApplication();

$aboutOutput = json_decode($output, true);

$this->assertArrayHasKey('sentry', $aboutOutput);

return $aboutOutput['sentry'];
}
}

0 comments on commit bf155b8

Please sign in to comment.