Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve breadcrumbs #31

Merged
merged 2 commits into from
Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ return array(

// capture release as git sha
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),

// Capture bindings on SQL queries
'breadcrumbs.sql_bindings' => true,
);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@

// capture release as git sha
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),

// Capture bindings on SQL queries
'breadcrumbs.sql_bindings' => false,
);
3 changes: 2 additions & 1 deletion examples/laravel-4.2/app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
| and give it the controller to call when that URI is requested.
|
*/

function verifyCredentials()
{
Log::info('Verifying credentials');
$user = DB::table('migrations')->where('migration', 'a migration')->first();
throw new Exception('No credentials passed!');
}

Expand Down
2 changes: 1 addition & 1 deletion examples/laravel-5.1/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
|
*/

'key' => env('APP_KEY', 'SomeRandomString'),
'key' => env('APP_KEY', 'xlhF31NeOlibJcoOW9tvZg7TkHcAZI3a'),

'cipher' => 'AES-256-CBC',

Expand Down
5 changes: 4 additions & 1 deletion examples/laravel-5.1/config/sentry.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

return array(
'dsn' => env('SENTRY_DSN'),
'dsn' => env('SENTRY_DSN', 'https://e9ebbd88548a441288393c457ec90441:399aaee02d454e2ca91351f29bdc3a07@sentry.io/3235'),

// capture release as git sha
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),

// Capture bindings on SQL queries
'breadcrumbs.sql_bindings' => true,
);
1 change: 1 addition & 0 deletions examples/laravel-5.2/app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
function verifyCredentials()
{
Log::info('Verifying credentials');
$user = DB::table('users')->where('name', 'John')->first();
throw new Exception('No credentials passed!');
}

Expand Down
2 changes: 1 addition & 1 deletion examples/laravel-5.2/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
|
*/

'key' => env('APP_KEY'),
'key' => env('APP_KEY', 'xlhF31NeOlibJcoOW9tvZg7TkHcAZI3a'),

'cipher' => 'AES-256-CBC',

Expand Down
5 changes: 4 additions & 1 deletion examples/laravel-5.2/config/sentry.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

return array(
'dsn' => env('SENTRY_DSN'),
'dsn' => env('SENTRY_DSN', 'https://e9ebbd88548a441288393c457ec90441:399aaee02d454e2ca91351f29bdc3a07@sentry.io/3235'),

// capture release as git sha
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),

// Capture bindings on SQL queries
'breadcrumbs.sql_bindings' => false,
);
17 changes: 16 additions & 1 deletion examples/lumen-5.2/app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
|
*/

function verifyCredentials()
{
Log::info('Verifying credentials');
$user = DB::table('migrations')->where('migration', 'a migration')->first();
throw new Exception('No credentials passed!');
}


function authenticateUser()
{
Log::info('Authenticating the current user');
verifyCredentials();
}


$app->get('/', function () use ($app) {
Log::info('Rendering a page thats about to error');
throw new Exception('An unhandled exception');
authenticateUser();
});
2 changes: 1 addition & 1 deletion examples/lumen-5.2/bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
realpath(__DIR__.'/../')
);

// $app->withFacades();
$app->withFacades();

// $app->withEloquent();

Expand Down
3 changes: 3 additions & 0 deletions examples/lumen-5.2/config/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

return array(
'dsn' => 'https://e9ebbd88548a441288393c457ec90441:399aaee02d454e2ca91351f29bdc3a07@app.getsentry.com/3235',

// Capture bindings on SQL queries
'breadcrumbs.sql_bindings' => false,
);
75 changes: 75 additions & 0 deletions src/Sentry/SentryLaravel/SentryLaravelEventHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Sentry\SentryLaravel;

use Illuminate\Events\Dispatcher;

// Event handling inspired by the ``laravel-debugbar`` project:
// https://github.com/barryvdh/laravel-debugbar
class SentryLaravelEventHandler
{
public function __construct(\Raven_Client $client, array $config)
{
$this->client = $client;
$this->sqlBindings = (
isset($config['breadcrumbs.sql_bindings'])
? $config['breadcrumbs.sql_bindings']
: true
);
}

public function subscribe(Dispatcher $events)
{
$this->events = $events;
$events->listen('*', [$this, 'onWildcardEvent']);
}

public function onWildcardEvent()
{
$name = $this->events->firing();
$args = func_get_args();
$data = null;
$level = 'info';
if ($name === 'Illuminate\Database\Events\QueryExecuted') {
$name = 'sql.query';
$message = $args[0]->sql;
$data = array(
'connectionName' => $args[0]->connectionName,
);
if ($this->sqlBindings) {
$bindings = $args[0]->bindings;
if (!empty($bindings)) {
$data['bindings'] = $bindings;
}
}
} elseif ($name === 'illuminate.query') {
// $args = array(sql, bindings, ...)
$name = 'sql.query';
$message = $args[0];
$data = array(
'connectionName' => $args[3],
);
if ($this->sqlBindings) {
$bindings = $args[1];
if (!empty($bindings)) {
$data['bindings'] = $bindings;
}
}
} elseif ($name === 'illuminate.log') {
$name = 'log.' . $args[0];
$level = $args[0];
$message = $args[1];
if (!empty($args[2])) {
$data = array('params' => $args[2]);
}
} else {
return;
}
$this->client->breadcrumbs->record(array(
'message' => $message,
'category' => $name,
'data' => $data,
'level' => $level,
));
}
}
22 changes: 14 additions & 8 deletions src/Sentry/SentryLaravel/SentryLaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SentryLaravelServiceProvider extends ServiceProvider
*/
public function boot()
{
parent::boot();

$app = $this->app;

// Laravel 4.x compatibility
Expand All @@ -35,23 +37,21 @@ public function boot()
$app['sentry']->captureException($e);
});

$this->bindLogger($app['sentry']);
$this->bindEvents($app);
} else {
// the default configuration file
$this->publishes(array(
__DIR__ . '/config.php' => config_path('sentry.php'),
), 'config');

$this->bindLogger(app('sentry'));
$this->bindEvents($app);
}
}

public function bindLogger($client)
protected function bindEvents($app)
{
$handler = new \Raven_Breadcrumbs_MonologHandler($client);

$logger = Log::getMonolog();
$logger->pushHandler($handler);
$handler = new SentryLaravelEventHandler($app['sentry'], $app['sentry.config']);
$handler->subscribe($app->events);
}

/**
Expand All @@ -61,7 +61,7 @@ public function bindLogger($client)
*/
public function register()
{
$this->app->singleton('sentry', function ($app) {
$this->app->singleton('sentry.config', function ($app) {
// sentry::config is Laravel 4.x
$user_config = $app['config']['sentry'] ?: $app['config']['sentry::config'];

Expand All @@ -70,6 +70,12 @@ public function register()
$user_config = [];
}

return $user_config;
});

$this->app->singleton('sentry', function ($app) {
$user_config = $app['sentry.config'];

$client = SentryLaravel::getClient(array_merge(array(
'environment' => $app->environment(),
'prefixes' => array(base_path()),
Expand Down
15 changes: 14 additions & 1 deletion src/Sentry/SentryLaravel/SentryLumenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class SentryLumenServiceProvider extends ServiceProvider
public function boot()
{
$this->app->configure('sentry');
$this->bindEvents($this->app);
}

protected function bindEvents($app)
{
$handler = new SentryLaravelEventHandler($app['sentry'], $app['sentry.config']);
$handler->subscribe($app->events);
}

/**
Expand All @@ -30,14 +37,20 @@ public function boot()
*/
public function register()
{
$this->app->singleton('sentry', function ($app) {
$this->app->singleton('sentry.config', function ($app) {
$user_config = $app['config']['sentry'];

// Make sure we don't crash when we did not publish the config file
if (is_null($user_config)) {
$user_config = [];
}

return $user_config;
});

$this->app->singleton('sentry', function ($app) {
$user_config = $app['sentry.config'];

$client = SentryLaravel::getClient(array_merge(array(
'environment' => $app->environment(),
'prefixes' => array(base_path()),
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry/SentryLaravel/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@

// capture release as git sha
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),

// Capture bindings on SQL queries
'breadcrumbs.sql_bindings' => true,
);