Skip to content

Commit bbaa730

Browse files
committed
Add more options to breadcrumbs configruation. #257
1 parent ba97b57 commit bbaa730

File tree

5 files changed

+216
-1
lines changed

5 files changed

+216
-1
lines changed

config/sentry.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
// 'release' => trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')),
99

1010
'breadcrumbs' => [
11+
// Capture Laravel logs in breadcrumbs
12+
'logs' => true,
13+
14+
// Capture SQL queries in breadcrumbs
15+
'sql_queries' => true,
1116

1217
// Capture bindings on SQL queries logged in breadcrumbs
1318
'sql_bindings' => true,
1419

20+
// Capture queue job information in breadcrumbs
21+
'queue_info' => true,
1522
],
1623

1724
];

src/Sentry/Laravel/EventHandler.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,34 @@ class EventHandler
6363
*/
6464
private $events;
6565

66+
/**
67+
* Indicates if we should we add SQL queries to the breadcrumbs.
68+
*
69+
* @var bool
70+
*/
71+
private $recordSqlQueries;
72+
6673
/**
6774
* Indicates if we should we add query bindings to the breadcrumbs.
6875
*
6976
* @var bool
7077
*/
7178
private $recordSqlBindings;
7279

80+
/**
81+
* Indicates if we should we add Laravel logs to the breadcrumbs.
82+
*
83+
* @var bool
84+
*/
85+
private $recordLaravelLogs;
86+
87+
/**
88+
* Indicates if we should we add queue info to the breadcrumbs.
89+
*
90+
* @var bool
91+
*/
92+
private $recordQueueInfo;
93+
7394
/**
7495
* EventHandler constructor.
7596
*
@@ -78,8 +99,11 @@ class EventHandler
7899
*/
79100
public function __construct(Dispatcher $events, array $config)
80101
{
81-
$this->events = $events;
102+
$this->events = $events;
103+
$this->recordSqlQueries = ($config['breadcrumbs.sql_queries'] ?? $config['breadcrumbs']['sql_queries'] ?? true) === true;
82104
$this->recordSqlBindings = ($config['breadcrumbs.sql_bindings'] ?? $config['breadcrumbs']['sql_bindings'] ?? false) === true;
105+
$this->recordLaravelLogs = ($config['breadcrumbs.logs'] ?? $config['breadcrumbs']['logs'] ?? true) === true;
106+
$this->recordQueueInfo = ($config['breadcrumbs.queue_info'] ?? $config['breadcrumbs']['queue_info'] ?? true) === true;
83107
}
84108

85109
/**
@@ -189,6 +213,10 @@ protected function routeMatchedHandler(RouteMatched $match)
189213
*/
190214
protected function queryHandler($query, $bindings, $time, $connectionName)
191215
{
216+
if (!$this->recordSqlQueries) {
217+
return;
218+
}
219+
192220
$this->addQueryBreadcrumb($query, $bindings, $time, $connectionName);
193221
}
194222

@@ -199,6 +227,10 @@ protected function queryHandler($query, $bindings, $time, $connectionName)
199227
*/
200228
protected function queryExecutedHandler(QueryExecuted $query)
201229
{
230+
if (!$this->recordSqlQueries) {
231+
return;
232+
}
233+
202234
$this->addQueryBreadcrumb($query->sql, $query->bindings, $query->time, $query->connectionName);
203235
}
204236

@@ -240,6 +272,10 @@ private function addQueryBreadcrumb($query, $bindings, $time, $connectionName)
240272
*/
241273
protected function logHandler($level, $message, $context)
242274
{
275+
if (!$this->recordLaravelLogs) {
276+
return;
277+
}
278+
243279
Integration::addBreadcrumb(new Breadcrumb(
244280
$level,
245281
Breadcrumb::TYPE_USER,
@@ -256,6 +292,10 @@ protected function logHandler($level, $message, $context)
256292
*/
257293
protected function messageLoggedHandler(MessageLogged $logEntry)
258294
{
295+
if (!$this->recordLaravelLogs) {
296+
return;
297+
}
298+
259299
Integration::addBreadcrumb(new Breadcrumb(
260300
$logEntry->level,
261301
Breadcrumb::TYPE_USER,
@@ -289,6 +329,10 @@ protected function queueJobProcessingHandler(JobProcessing $event)
289329
// When a job starts, we want to push a new scope
290330
Integration::getCurrentHub()->pushScope();
291331

332+
if (!$this->recordQueueInfo) {
333+
return;
334+
}
335+
292336
$job = [
293337
'job' => $event->job->getName(),
294338
'queue' => $event->job->getQueue(),
@@ -322,6 +366,10 @@ protected function commandStartingHandler(CommandStarting $event)
322366
$scope->setTag('command', $event->command);
323367
});
324368

369+
if (!$this->recordQueueInfo) {
370+
return;
371+
}
372+
325373
Integration::addBreadcrumb(new Breadcrumb(
326374
Breadcrumb::LEVEL_INFO,
327375
Breadcrumb::TYPE_USER,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
4+
namespace Sentry\Laravel\Tests;
5+
6+
class LaravelLogsInBreadcrumbsTest extends SentryLaravelTestCase
7+
{
8+
public function testLaravelLogsAreRecordedWhenEnabled()
9+
{
10+
$this->resetApplicationWithConfig([
11+
'sentry.breadcrumbs.logs' => true,
12+
]);
13+
14+
$this->assertTrue($this->app['config']->get('sentry.breadcrumbs.logs'));
15+
16+
$this->dispatchLaravelEvent('illuminate.log', [
17+
$level = 'debug',
18+
$message = 'test message',
19+
$context = ['1'],
20+
]);
21+
22+
$breadcrumbs = $this->getCurrentBreadcrumbs();
23+
24+
/** @var \Sentry\Breadcrumb $lastBreadcrumb */
25+
$lastBreadcrumb = end($breadcrumbs);
26+
27+
$this->assertEquals($level, $lastBreadcrumb->getLevel());
28+
$this->assertEquals($message, $lastBreadcrumb->getMessage());
29+
$this->assertEquals($context, $lastBreadcrumb->getMetadata()['params']);
30+
}
31+
32+
public function testLaravelLogsAreRecordedWhenDisabled()
33+
{
34+
$this->resetApplicationWithConfig([
35+
'sentry.breadcrumbs.logs' => false,
36+
]);
37+
38+
$this->assertFalse($this->app['config']->get('sentry.breadcrumbs.logs'));
39+
40+
$this->dispatchLaravelEvent('illuminate.log', [
41+
$level = 'debug',
42+
$message = 'test message',
43+
$context = ['1'],
44+
]);
45+
46+
$breadcrumbs = $this->getCurrentBreadcrumbs();
47+
$this->assertEmpty($breadcrumbs);
48+
}
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
4+
namespace Sentry\Laravel\Tests;
5+
6+
use Illuminate\Console\Events\CommandStarting;
7+
use Symfony\Component\Console\Input\ArrayInput;
8+
use Symfony\Component\Console\Output\BufferedOutput;
9+
10+
class QueueInfoInBreadcrumbsTest extends SentryLaravelTestCase
11+
{
12+
public function testQueueInfoAreRecordedWhenEnabled()
13+
{
14+
if ($this->shouldSkip()) {
15+
$this->markTestSkipped('Laravel version too low.');
16+
}
17+
$this->resetApplicationWithConfig([
18+
'sentry.breadcrumbs.queue_info' => true,
19+
]);
20+
21+
$this->assertTrue($this->app['config']->get('sentry.breadcrumbs.queue_info'));
22+
23+
$this->dispatchCommandStartEvent();
24+
25+
$breadcrumbs = $this->getCurrentBreadcrumbs();
26+
27+
/** @var \Sentry\Breadcrumb $lastBreadcrumb */
28+
$lastBreadcrumb = end($breadcrumbs);
29+
30+
$this->assertEquals('Invoked Artisan command: test:command', $lastBreadcrumb->getMessage());
31+
$this->assertEquals('--foo=bar', $lastBreadcrumb->getMetadata()['input']);
32+
}
33+
34+
public function testQueueInfoAreRecordedWhenDisabled()
35+
{
36+
if ($this->shouldSkip()) {
37+
$this->markTestSkipped('Laravel version too low.');
38+
}
39+
$this->resetApplicationWithConfig([
40+
'sentry.breadcrumbs.queue_info' => false,
41+
]);
42+
43+
$this->assertFalse($this->app['config']->get('sentry.breadcrumbs.queue_info'));
44+
$this->dispatchCommandStartEvent();
45+
46+
$breadcrumbs = $this->getCurrentBreadcrumbs();
47+
$this->assertEmpty($breadcrumbs);
48+
}
49+
50+
private function dispatchCommandStartEvent()
51+
{
52+
$dispatcher = $this->app['events'];
53+
$method = method_exists($dispatcher, 'dispatch') ? 'dispatch' : 'fire';
54+
$this->app['events']->$method(CommandStarting::class, new CommandStarting($command = 'test:command', $input = new ArrayInput(['--foo' => 'bar']), new BufferedOutput()));
55+
}
56+
57+
private function shouldSkip()
58+
{
59+
return !class_exists(CommandStarting::class);
60+
}
61+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
4+
namespace Sentry\Laravel\Tests;
5+
6+
7+
class SqlQueriesInBreadcrumbsTest extends SentryLaravelTestCase
8+
{
9+
public function testSqlQueriesAreRecordedWhenEnabled()
10+
{
11+
$this->resetApplicationWithConfig([
12+
'sentry.breadcrumbs.sql_queries' => true,
13+
]);
14+
15+
$this->assertTrue($this->app['config']->get('sentry.breadcrumbs.sql_queries'));
16+
17+
$this->dispatchLaravelEvent('illuminate.query', [
18+
$query = 'SELECT * FROM breadcrumbs WHERE bindings = ?;',
19+
$bindings = ['1'],
20+
10,
21+
'test',
22+
]);
23+
24+
$breadcrumbs = $this->getCurrentBreadcrumbs();
25+
26+
/** @var \Sentry\Breadcrumb $lastBreadcrumb */
27+
$lastBreadcrumb = end($breadcrumbs);
28+
29+
$this->assertEquals($query, $lastBreadcrumb->getMessage());
30+
}
31+
32+
public function testSqlQueriesAreRecordedWhenDisabled()
33+
{
34+
$this->resetApplicationWithConfig([
35+
'sentry.breadcrumbs.sql_queries' => false,
36+
]);
37+
38+
$this->assertFalse($this->app['config']->get('sentry.breadcrumbs.sql_queries'));
39+
40+
$this->dispatchLaravelEvent('illuminate.query', [
41+
$query = 'SELECT * FROM breadcrumbs WHERE bindings <> ?;',
42+
$bindings = ['1'],
43+
10,
44+
'test',
45+
]);
46+
47+
$breadcrumbs = $this->getCurrentBreadcrumbs();
48+
$this->assertEmpty($breadcrumbs);
49+
}
50+
}

0 commit comments

Comments
 (0)