Skip to content

Commit

Permalink
[EasyBugsnag] Session tracking for laravel queue job (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
natepage authored Jul 13, 2021
1 parent 5627375 commit 784cf50
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use EonX\EasyBugsnag\Bridge\Laravel\Session\SessionTrackingConfigurator;
use EonX\EasyBugsnag\Bridge\Laravel\Session\SessionTrackingListener;
use EonX\EasyBugsnag\Bridge\Laravel\Session\SessionTrackingMiddleware;
use EonX\EasyBugsnag\Bridge\Laravel\Session\SessionTrackingQueueListener;
use EonX\EasyBugsnag\ClientFactory;
use EonX\EasyBugsnag\Configurators\AwsEcsFargateConfigurator;
use EonX\EasyBugsnag\Configurators\BasicsConfigurator;
Expand All @@ -19,6 +20,7 @@
use EonX\EasyBugsnag\Session\SessionTracker;
use EonX\EasyBugsnag\Shutdown\ShutdownStrategy;
use Illuminate\Contracts\Container\Container;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -165,7 +167,15 @@ static function (Container $app): SessionTrackingConfigurator {
$this->app->middleware([SessionTrackingMiddleware::class]);
}

$this->app->make('events')->listen(RouteMatched::class, SessionTrackingListener::class);
$events = $this->app->make('events');

$this->app->singleton(SessionTrackingListener::class);
$events->listen(RouteMatched::class, SessionTrackingListener::class);

if (\config('easy-bugsnag.session_tracking.queue_job_count_for_sessions', false)) {
$this->app->singleton(SessionTrackingQueueListener::class);
$events->listen(JobProcessing::class, SessionTrackingQueueListener::class);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace EonX\EasyBugsnag\Bridge\Laravel\Session;

use Bugsnag\Client;
use Illuminate\Queue\Events\JobProcessing;

final class SessionTrackingQueueListener
{
/**
* @var \Bugsnag\Client
*/
private $client;

/**
* @var bool
*/
private $configured = false;

public function __construct(Client $client)
{
$this->client = $client;
}

public function handle(JobProcessing $event): void
{
$sessionTracker = $this->client->getSessionTracker();
$sessionTracker->startSession();

if ($this->configured) {
return;
}

// Make sure sessions are sent when worker stops
\register_shutdown_function(static function () use ($sessionTracker): void {
$sessionTracker->sendSessions();
});

$this->configured = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@
/**
* Delimiter used in Regex to resolve excluded URLs.
*/
'session_tracking_exclude_urls_delimiter' => '#'
'session_tracking_exclude_urls_delimiter' => '#',

/**
* Enable/Disable session tracking for queue jobs.
*/
'queue_job_count_for_sessions' => false,
],

/**
Expand Down

0 comments on commit 784cf50

Please sign in to comment.