Skip to content

Commit

Permalink
feat: add JobProcessing and JobProcessed events
Browse files Browse the repository at this point in the history
  • Loading branch information
subzero10 committed Sep 7, 2024
1 parent 804c4d1 commit 257ecd5
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Concerns/MeasuresDuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Honeybadger\HoneybadgerLaravel\Concerns;

trait MeasuresDuration
{
/**
* @var array{string, float}
*/
private array $durations = [];

/**
* Helper to measure the duration between blocks of code that span multiple files.
* If the name already exists in the durations array, it will return the duration in milliseconds.
* If it does not exist, a timer will be started for this name and null will be returned.
*/
public function time(string $name): ?float {
if (array_key_exists($name, $this->durations)) {
$start = $this->durations[$name];
$end = microtime(true);
$duration = ($end - $start) * 1000;
unset($this->durations[$name]);
return $duration;
} else {
$this->durations[$name] = microtime(true);
return null;
}
}

}
35 changes: 35 additions & 0 deletions src/Events/JobProcessed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Honeybadger\HoneybadgerLaravel\Events;

use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
use Illuminate\Queue\Events\JobProcessed as LaravelJobProcessed;

class JobProcessed
{
public string $handles = LaravelJobProcessed::class;

/**
* @param LaravelJobProcessed $event
* @return EventPayload
*/
public function getEventPayload($event): EventPayload
{
$jobClass = get_class($event->job);

$metadata = [
'connectionName' => $event->connectionName,
'queue' => $event->job->queue,
'job' => get_class($event->job),
'duration' => Honeybadger::time($jobClass)
];

return new EventPayload(
'job',
'job.processed',
'Job processed',
$metadata,
);
}

}
35 changes: 35 additions & 0 deletions src/Events/JobProcessing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Honeybadger\HoneybadgerLaravel\Events;

use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
use Illuminate\Queue\Events\JobProcessing as LaravelJobProcessing;

class JobProcessing extends ApplicationEvent
{
public string $handles = LaravelJobProcessing::class;

/**
* @param LaravelJobProcessing $event
* @return EventPayload
*/
public function getEventPayload($event): EventPayload
{
$jobClass = get_class($event->job);

Honeybadger::time($jobClass);

$metadata = [
'connectionName' => $event->connectionName,
'queue' => $event->job->queue,
'job' => $jobClass,
];

return new EventPayload(
'job',
'job.processing',
'Job processing',
$metadata,
);
}
}
1 change: 1 addition & 0 deletions src/Facades/Honeybadger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static float|null time(string $name)
* @method static void event(string|array $eventTypeOrPayload, array $payload = null)
* @method static void checkin(string $key)
* @method static array rawNotification(callable $callable)
Expand Down
5 changes: 5 additions & 0 deletions src/HoneybadgerLaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use Honeybadger\Contracts\Reporter;
use Honeybadger\Exceptions\ServiceException;
use Honeybadger\Honeybadger;
use Honeybadger\HoneybadgerLaravel\Concerns\MeasuresDuration;
use Honeybadger\HoneybadgerLaravel\Exceptions\TestException;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Request;
use Throwable;

class HoneybadgerLaravel extends Honeybadger
{
use MeasuresDuration;

const VERSION = '4.1.1';

const DEFAULT_EVENTS = [
Expand All @@ -22,6 +25,8 @@ class HoneybadgerLaravel extends Honeybadger
Events\CacheHit::class,
Events\CacheMiss::class,
Events\JobQueued::class,
Events\JobProcessing::class,
Events\JobProcessed::class,
Events\MailSending::class,
Events\MailSent::class,
Events\MessageLogged::class,
Expand Down

0 comments on commit 257ecd5

Please sign in to comment.