Skip to content

Commit fa1a0e1

Browse files
committed
Improve breadcrumbs
- Capture illuminate query events - Swap out Monolog handler for illuminate.log
1 parent 3052f44 commit fa1a0e1

File tree

13 files changed

+141
-12
lines changed

13 files changed

+141
-12
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ return array(
116116

117117
// capture release as git sha
118118
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),
119+
120+
// Capture bindings on SQL queries
121+
'breadcrumbs.sql_bindings' => true,
119122
);
120123
```
121124

examples/laravel-4.2/app/config/packages/sentry/sentry-laravel/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55

66
// capture release as git sha
77
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),
8+
9+
// Capture bindings on SQL queries
10+
'breadcrumbs.sql_bindings' => false,
811
);

examples/laravel-4.2/app/routes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
|
88
| Here is where you can register all of the routes for an application.
99
| It's a breeze. Simply tell Laravel the URIs it should respond to
10-
| and give it the Closure to execute when that URI is requested.
10+
| and give it the controller to call when that URI is requested.
1111
|
1212
*/
1313

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

examples/laravel-5.1/config/sentry.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55

66
// capture release as git sha
77
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),
8+
9+
// Capture bindings on SQL queries
10+
'breadcrumbs.sql_bindings' => true,
811
);

examples/laravel-5.2/app/Http/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
function verifyCredentials()
1515
{
1616
Log::info('Verifying credentials');
17+
$user = DB::table('users')->where('name', 'John')->first();
1718
throw new Exception('No credentials passed!');
1819
}
1920

examples/laravel-5.2/config/sentry.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55

66
// capture release as git sha
77
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),
8+
9+
// Capture bindings on SQL queries
10+
'breadcrumbs.sql_bindings' => false,
811
);

examples/lumen-5.2/app/Http/routes.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,22 @@
1111
|
1212
*/
1313

14+
function verifyCredentials()
15+
{
16+
Log::info('Verifying credentials');
17+
$user = DB::table('migrations')->where('migration', 'a migration')->first();
18+
throw new Exception('No credentials passed!');
19+
}
20+
21+
22+
function authenticateUser()
23+
{
24+
Log::info('Authenticating the current user');
25+
verifyCredentials();
26+
}
27+
28+
1429
$app->get('/', function () use ($app) {
1530
Log::info('Rendering a page thats about to error');
16-
throw new Exception('An unhandled exception');
31+
authenticateUser();
1732
});

examples/lumen-5.2/bootstrap/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
realpath(__DIR__.'/../')
2424
);
2525

26-
// $app->withFacades();
26+
$app->withFacades();
2727

2828
// $app->withEloquent();
2929

examples/lumen-5.2/config/sentry.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
return array(
44
'dsn' => 'https://e9ebbd88548a441288393c457ec90441:[email protected]/3235',
5+
6+
// Capture bindings on SQL queries
7+
'breadcrumbs.sql_bindings' => false,
58
);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Sentry\SentryLaravel;
4+
5+
use Illuminate\Events\Dispatcher;
6+
7+
// Event handling inspired by the ``laravel-debugbar`` project:
8+
// https://github.com/barryvdh/laravel-debugbar
9+
class SentryLaravelEventHandler
10+
{
11+
public function __construct(\Raven_Client $client, array $config)
12+
{
13+
$this->client = $client;
14+
$this->sqlBindings = (
15+
isset($config['breadcrumbs.sql_bindings'])
16+
? $config['breadcrumbs.sql_bindings']
17+
: true
18+
);
19+
}
20+
21+
public function subscribe(Dispatcher $events)
22+
{
23+
$this->events = $events;
24+
$events->listen('*', [$this, 'onWildcardEvent']);
25+
}
26+
27+
public function onWildcardEvent()
28+
{
29+
$name = $this->events->firing();
30+
$args = func_get_args();
31+
$data = null;
32+
$level = 'info';
33+
if ($name === 'Illuminate\Database\Events\QueryExecuted') {
34+
$name = 'sql.query';
35+
$message = $args[0]->sql;
36+
$data = array(
37+
'connectionName' => $args[0]->connectionName,
38+
);
39+
if ($this->sqlBindings) {
40+
$bindings = $args[0]->bindings;
41+
if (!empty($bindings)) {
42+
$data['bindings'] = $bindings;
43+
}
44+
}
45+
} elseif ($name === 'illuminate.query') {
46+
// $args = array(sql, bindings, ...)
47+
$name = 'sql.query';
48+
$message = $args[0];
49+
$data = array(
50+
'connectionName' => $args[3],
51+
);
52+
if ($this->sqlBindings) {
53+
$bindings = $args[1];
54+
if (!empty($bindings)) {
55+
$data['bindings'] = $bindings;
56+
}
57+
}
58+
} elseif ($name === 'illuminate.log') {
59+
$name = 'log.' . $args[0];
60+
$level = $args[0];
61+
$message = $args[1];
62+
if (!empty($args[2])) {
63+
$data = array('params' => $args[2]);
64+
}
65+
} else {
66+
return;
67+
}
68+
$this->client->breadcrumbs->record(array(
69+
'message' => $message,
70+
'category' => $name,
71+
'data' => $data,
72+
'level' => $level,
73+
));
74+
}
75+
}

0 commit comments

Comments
 (0)