Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record the queue context #183

Merged
merged 1 commit into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/BugsnagServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace Bugsnag\BugsnagLaravel;

use Bugsnag\Breadcrumbs\Breadcrumb;
use Bugsnag\BugsnagLaravel\Queue\Tracker;
use Bugsnag\BugsnagLaravel\Request\LaravelResolver;
use Bugsnag\Callbacks\CustomUser;
use Bugsnag\Client;
use Bugsnag\Configuration;
use Bugsnag\Report;
use Exception;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Application as LumenApplication;
Expand Down Expand Up @@ -93,6 +96,28 @@ protected function setupQueue(QueueManager $queue)
$queue->looping(function () {
$this->app->bugsnag->flush();
$this->app->bugsnag->clearBreadcrumbs();
$this->app->make(Tracker::class)->clear();
});

if (!class_exists(JobProcessing::class)) {
return;
}

$queue->before(function (JobProcessing $event) {
$this->app->bugsnag->setAppType('Queue');

$job = [
'name' => $event->job->getName(),
'queue' => $event->job->getQueue(),
'attempts' => $event->job->attempts(),
'connection' => $event->connectionName,
];

if (method_exists($event->job, 'resolveName')) {
$job['resolved'] = $event->job->resolveName();
}

$this->app->make(Tracker::class)->set($job);
});
}

Expand All @@ -110,6 +135,18 @@ public function register()

if (!isset($config['callbacks']) || $config['callbacks']) {
$client->registerDefaultCallbacks();

$client->registerCallback(function (Report $report) use ($app) {
$tracker = $app->make(Tracker::class);

if ($context = $tracker->context()) {
$report->setContext($context);
}

if ($job = $tracker->get()) {
$report->setMetaData(['job' => $job]);
}
});
}

if (!isset($config['user']) || $config['user']) {
Expand Down Expand Up @@ -142,6 +179,10 @@ public function register()
return $client;
});

$this->app->singleton('bugsnag.tracker', function () {
return new Tracker();
});

$this->app->singleton('bugsnag.logger', function (Container $app) {
return new LaravelLogger($app['bugsnag']);
});
Expand All @@ -151,6 +192,7 @@ public function register()
});

$this->app->alias('bugsnag', Client::class);
$this->app->alias('bugsnag.tracker', Tracker::class);
$this->app->alias('bugsnag.logger', LaravelLogger::class);
$this->app->alias('bugsnag.multi', MultiLogger::class);
}
Expand Down Expand Up @@ -184,6 +226,6 @@ protected function getGuzzle(array $config)
*/
public function provides()
{
return ['bugsnag', 'bugsnag.logger', 'bugsnag.multi'];
return ['bugsnag', 'bugsnag.tracker', 'bugsnag.logger', 'bugsnag.multi'];
}
}
57 changes: 57 additions & 0 deletions src/Queue/Tracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Bugsnag\BugsnagLaravel\Queue;

class Tracker
{
/**
* The current job information.
*
* @var array|null
*/
protected $job;

/**
* Get the current context.
*
* @return string|null
*/
public function context()
{
if (isset($this->job['resolved'])) {
return $this->job['resolved'];
}
}

/**
* Get the current job information.
*
* @return array|null
*/
public function get()
{
return $this->job;
}

/**
* Set the current job information.
*
* @param array $job
*
* @return void
*/
public function set(array $job)
{
$this->job = $job;
}

/**
* Clear the current job information.
*
* @return void
*/
public function clear()
{
$this->job = null;
}
}
6 changes: 6 additions & 0 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Bugsnag\BugsnagLaravel\LaravelLogger;
use Bugsnag\BugsnagLaravel\MultiLogger;
use Bugsnag\BugsnagLaravel\Queue\Tracker;
use Bugsnag\Client;
use GrahamCampbell\TestBenchCore\ServiceProviderTrait;

Expand All @@ -16,6 +17,11 @@ public function testClientIsInjectable()
$this->assertIsInjectable(Client::class);
}

public function testJobTrackerIsInjectable()
{
$this->assertIsInjectable(Tracker::class);
}

public function testMultiLoggerIsInjectable()
{
$this->assertIsInjectable(MultiLogger::class);
Expand Down