Skip to content

Commit 868d0f7

Browse files
subzero10stympy
andauthored
feat: events api integration (#125)
* feat: events api integration Refs: #124 * chore: add test * chore: support deprecated breadcrumb classes * chore: move deprecation check in service provider * chore: test for deprecated in all the places * fix: do not replace double quoters Co-authored-by: Benjamin Curtis <[email protected]> --------- Co-authored-by: Benjamin Curtis <[email protected]>
1 parent fe384d9 commit 868d0f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+863
-371
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [4.1.0] - 2024-06-29
10+
### Added
11+
- Events: Honeybadger.event() method to send events to Honeybadger Insights
12+
- Events: Custom logger channel to send logs as events to Honeybadger Insights
13+
- Events: Automatic logging of application events to Honeybadger Insights
14+
15+
### Refactored
16+
- Breadcrumbs: Modified automatic breadcrumbs collection to reusable classes to share with Events integration
17+
918
## [4.0.0] - 2024-03-22
1019
### Added
1120
- Support for Laravel 11

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "^8.1",
25-
"honeybadger-io/honeybadger-php": "^2.18.0",
25+
"honeybadger-io/honeybadger-php": ">=2.19.1",
2626
"sixlive/dotenv-editor": "^1.1|^2.0",
2727
"illuminate/console": "^10.0|^11.0",
2828
"illuminate/support": "^10.0|^11.0",

config/honeybadger.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

3-
use Honeybadger\HoneybadgerLaravel\Breadcrumbs;
3+
use Honeybadger\BulkEventDispatcher;
4+
use Honeybadger\HoneybadgerLaravel\HoneybadgerLaravel;
45

56
return [
67
/**
@@ -77,10 +78,12 @@
7778
/**
7879
* Older PHP functions use the Error class, while modern PHP mostly uses Exception.
7980
* Specify if you'd like Honeybadger to report both types of errors.
81+
* The shutdown handler is required flushing any remaining events that were queued using Honeybadger.event().
8082
*/
8183
'handlers' => [
8284
'exception' => true,
8385
'error' => true,
86+
'shutdown' => true,
8487
],
8588

8689
/**
@@ -111,27 +114,10 @@
111114
'enabled' => true,
112115

113116
/**
114-
* Events which should automatically be recorded by the Honeybadger client.
117+
* Events which should automatically be recorded by the Honeybadger client as breadcrumbs.
115118
* Note that to track redis events, you need to call `Redis::enableEvents()` in your app.
116119
*/
117-
'automatic' => [
118-
Breadcrumbs\DatabaseQueryExecuted::class,
119-
Breadcrumbs\DatabaseTransactionStarted::class,
120-
Breadcrumbs\DatabaseTransactionCommitted::class,
121-
Breadcrumbs\DatabaseTransactionRolledBack::class,
122-
Breadcrumbs\CacheHit::class,
123-
Breadcrumbs\CacheMiss::class,
124-
Breadcrumbs\JobQueued::class,
125-
Breadcrumbs\MailSending::class,
126-
Breadcrumbs\MailSent::class,
127-
Breadcrumbs\MessageLogged::class,
128-
Breadcrumbs\NotificationSending::class,
129-
Breadcrumbs\NotificationSent::class,
130-
Breadcrumbs\NotificationFailed::class,
131-
Breadcrumbs\RedisCommandExecuted::class,
132-
Breadcrumbs\RouteMatched::class,
133-
Breadcrumbs\ViewRendered::class,
134-
],
120+
'automatic' => HoneybadgerLaravel::DEFAULT_EVENTS,
135121
],
136122

137123
/**
@@ -141,4 +127,28 @@
141127
* Learn more about checkins at https://docs.honeybadger.io/api/reporting-check-ins/.
142128
*/
143129
'checkins' => [],
130+
131+
'events' => [
132+
/**
133+
* Enable sending application events to Honeybadger Insights.
134+
* Setting this to false will disable automatic events collection and the event() function.
135+
*/
136+
'enabled' => false,
137+
138+
/**
139+
* The number of events to queue before sending them to Honeybadger.
140+
*/
141+
'bulk_threshold' => BulkEventDispatcher::BULK_THRESHOLD,
142+
143+
/**
144+
* The number of seconds to wait before sending queued events to Honeybadger.
145+
*/
146+
'dispatch_interval_seconds' => BulkEventDispatcher::DISPATCH_INTERVAL_SECONDS,
147+
148+
/**
149+
* Events which should automatically be sent to Honeybadger Insights.
150+
* Note that to track redis events, you need to call `Redis::enableEvents()` in your app.
151+
*/
152+
'automatic' => HoneybadgerLaravel::DEFAULT_EVENTS,
153+
],
144154
];

src/Breadcrumbs/Breadcrumb.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Breadcrumbs/CacheHit.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
namespace Honeybadger\HoneybadgerLaravel\Breadcrumbs;
44

5-
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
6-
use Illuminate\Cache\Events\CacheHit as LaravelCacheHit;
7-
8-
class CacheHit extends Breadcrumb
5+
/**
6+
* @deprecated Use {@link \Honeybadger\HoneybadgerLaravel\Events\CacheHit} instead
7+
*/
8+
class CacheHit extends \Honeybadger\HoneybadgerLaravel\Events\CacheHit
99
{
10-
public $handles = LaravelCacheHit::class;
11-
12-
public function handleEvent(LaravelCacheHit $event)
13-
{
14-
Honeybadger::addBreadcrumb('Cache hit', ['key' => $event->key], 'query');
15-
}
1610
}

src/Breadcrumbs/CacheMiss.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
namespace Honeybadger\HoneybadgerLaravel\Breadcrumbs;
44

5-
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
6-
use Illuminate\Cache\Events\CacheMissed;
7-
8-
class CacheMiss extends Breadcrumb
5+
/**
6+
* @deprecated Use {@link \Honeybadger\HoneybadgerLaravel\Events\CacheMiss} instead
7+
*/
8+
class CacheMiss extends \Honeybadger\HoneybadgerLaravel\Events\CacheMiss
99
{
10-
public $handles = CacheMissed::class;
11-
12-
public function handleEvent(CacheMissed $event)
13-
{
14-
Honeybadger::addBreadcrumb('Cache miss', ['key' => $event->key], 'query');
15-
}
1610
}

src/Breadcrumbs/DatabaseQueryExecuted.php

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,9 @@
22

33
namespace Honeybadger\HoneybadgerLaravel\Breadcrumbs;
44

5-
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
6-
use Illuminate\Database\Connection;
7-
use Illuminate\Database\Events\QueryExecuted;
8-
9-
class DatabaseQueryExecuted extends Breadcrumb
5+
/**
6+
* @deprecated Use {@link \Honeybadger\HoneybadgerLaravel\Events\DatabaseQueryExecuted} instead
7+
*/
8+
class DatabaseQueryExecuted extends \Honeybadger\HoneybadgerLaravel\Events\DatabaseQueryExecuted
109
{
11-
public $handles = QueryExecuted::class;
12-
13-
public function handleEvent(QueryExecuted $event)
14-
{
15-
$metadata = [
16-
'connectionName' => $event->connectionName,
17-
'sql' => $this->sanitize($event->sql, $event->connection),
18-
'duration' => number_format($event->time, 2, '.', '').'ms',
19-
];
20-
21-
Honeybadger::addBreadcrumb('Database query executed', $metadata, 'query');
22-
}
23-
24-
/**
25-
* Even though Laravel gives us the sanitized query, let's err on the side of caution by removing any quoted data.
26-
*/
27-
public function sanitize(string $sql, Connection $connection): string
28-
{
29-
$escapedQuotes = '#/(\\"|\\\')/#';
30-
$numericData = '#\b\d+\b#';
31-
$singleQuotedData = "#'(?:[^']|'')*'#";
32-
$newlines = '#\n#';
33-
$doubleQuotedData = '#"(?:[^"]|"")*"#';
34-
35-
$sql = preg_replace($escapedQuotes, '', $sql);
36-
$sql = preg_replace([$numericData, $singleQuotedData, $newlines], '?', $sql);
37-
38-
$doubleQuoters = ['pgsql', 'sqlite', 'postgis'];
39-
if (in_array($connection->getConfig('driver'), $doubleQuoters)) {
40-
$sql = preg_replace($doubleQuotedData, '?', $sql);
41-
}
42-
43-
return $sql;
44-
}
4510
}

src/Breadcrumbs/DatabaseTransactionCommitted.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22

33
namespace Honeybadger\HoneybadgerLaravel\Breadcrumbs;
44

5-
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
6-
use Illuminate\Database\Events\TransactionCommitted;
7-
8-
class DatabaseTransactionCommitted extends Breadcrumb
5+
/**
6+
* @deprecated Use {@link \Honeybadger\HoneybadgerLaravel\Events\DatabaseTransactionCommitted} instead
7+
*/
8+
class DatabaseTransactionCommitted extends \Honeybadger\HoneybadgerLaravel\Events\DatabaseTransactionCommitted
99
{
10-
public $handles = TransactionCommitted::class;
11-
12-
public function handleEvent(TransactionCommitted $event)
13-
{
14-
Honeybadger::addBreadcrumb('Database transaction committed', [
15-
'connectionName' => $event->connectionName,
16-
], 'query');
17-
}
1810
}

src/Breadcrumbs/DatabaseTransactionRolledBack.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22

33
namespace Honeybadger\HoneybadgerLaravel\Breadcrumbs;
44

5-
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
6-
use Illuminate\Database\Events\TransactionRolledBack;
7-
8-
class DatabaseTransactionRolledBack extends Breadcrumb
5+
/**
6+
* @deprecated Use {@link \Honeybadger\HoneybadgerLaravel\Events\DatabaseTransactionRolledBack} instead
7+
*/
8+
class DatabaseTransactionRolledBack extends \Honeybadger\HoneybadgerLaravel\Events\DatabaseTransactionRolledBack
99
{
10-
public $handles = TransactionRolledBack::class;
11-
12-
public function handleEvent(TransactionRolledBack $event)
13-
{
14-
Honeybadger::addBreadcrumb('Database transaction rolled back', [
15-
'connectionName' => $event->connectionName,
16-
], 'query');
17-
}
1810
}

src/Breadcrumbs/DatabaseTransactionStarted.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22

33
namespace Honeybadger\HoneybadgerLaravel\Breadcrumbs;
44

5-
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
6-
use Illuminate\Database\Events\TransactionBeginning;
7-
8-
class DatabaseTransactionStarted extends Breadcrumb
5+
/**
6+
* @deprecated Use {@link \Honeybadger\HoneybadgerLaravel\Events\DatabaseTransactionStarted} instead
7+
*/
8+
class DatabaseTransactionStarted extends \Honeybadger\HoneybadgerLaravel\Events\DatabaseTransactionStarted
99
{
10-
public $handles = TransactionBeginning::class;
11-
12-
public function handleEvent(TransactionBeginning $event)
13-
{
14-
Honeybadger::addBreadcrumb('Database transaction started', [
15-
'connectionName' => $event->connectionName,
16-
], 'query');
17-
}
1810
}

0 commit comments

Comments
 (0)