Skip to content

Commit d8d29b8

Browse files
danhunsakerpleckey
authored andcommitted
Add support for Queued Job transactions (#48)
Queued jobs should also have intelligent naming, and be marked properly as background tasks.
1 parent 4c99149 commit d8d29b8

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/Intouch/LaravelNewrelic/LumenNewrelicServiceProvider.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Intouch\LaravelNewrelic;
44

5+
use Illuminate\Queue\Events\JobProcessed;
56
use Illuminate\Support\ServiceProvider;
67
use Intouch\Newrelic\Newrelic;
78

@@ -21,5 +22,43 @@ function ( $app ) {
2122
return new Newrelic( $app['config']->get( 'newrelic.throw_if_not_installed' ) );
2223
}
2324
);
25+
26+
app('queue')->before(function (JobProcessed $event) {
27+
app('newrelic')->backgroundJob( true );
28+
app('newrelic')->startTransaction( ini_get('newrelic.appname') );
29+
if (app('config')->get( 'newrelic.auto_name_jobs' )) {
30+
app('newrelic')->nameTransaction( $this->getJobName($event) );
31+
}
32+
});
33+
34+
app('queue')->after(function (JobProcessed $event) {
35+
app('newrelic')->endTransaction();
36+
});
37+
}
38+
39+
/**
40+
* Build the job name
41+
*
42+
* @return string
43+
*/
44+
public function getJobName(JobProcessed $event)
45+
{
46+
return str_replace(
47+
[
48+
'{connection}',
49+
'{class}',
50+
'{data}',
51+
'{args}',
52+
'{input}',
53+
],
54+
[
55+
$event->connectionName,
56+
get_class($event->job),
57+
json_encode($event->data),
58+
implode(', ', array_keys($event->data)),
59+
implode(', ', array_values($event->data)),
60+
],
61+
$this->app['config']->get( 'newrelic.job_name_provider' )
62+
);
2463
}
2564
}

src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
namespace Intouch\LaravelNewrelic;
1818

19+
use Illuminate\Queue\Events\JobProcessed;
1920
use Illuminate\Routing\Events\RouteMatched;
2021
use Illuminate\Support\ServiceProvider;
2122
use Intouch\Newrelic\Newrelic;
@@ -42,6 +43,7 @@ public function boot()
4243
$this->publishes( [ $config => config_path( 'newrelic.php' ) ], 'config' );
4344

4445
$this->registerNamedTransactions();
46+
$this->registerQueueTransactions();
4547
}
4648

4749
/**
@@ -83,6 +85,26 @@ protected function registerNamedTransactions()
8385
}
8486
}
8587

88+
/**
89+
* Registers the queue transactions with the NewRelic PHP agent
90+
*/
91+
protected function registerQueueTransactions()
92+
{
93+
$app = $this->app;
94+
95+
$app['queue']->before(function (JobProcessed $event) use ( $app ) {
96+
$app['newrelic']->backgroundJob( true );
97+
$app['newrelic']->startTransaction( ini_get('newrelic.appname') );
98+
if ($app['config']->get( 'newrelic.auto_name_jobs' )) {
99+
$app['newrelic']->nameTransaction( $this->getJobName($event) );
100+
}
101+
});
102+
103+
$app['queue']->after(function (JobProcessed $event) use ( $app ) {
104+
$app['newrelic']->endTransaction();
105+
});
106+
}
107+
86108
/**
87109
* Build the transaction name
88110
*
@@ -109,6 +131,32 @@ public function getTransactionName()
109131
);
110132
}
111133

134+
/**
135+
* Build the job name
136+
*
137+
* @return string
138+
*/
139+
public function getJobName(JobProcessed $event)
140+
{
141+
return str_replace(
142+
[
143+
'{connection}',
144+
'{class}',
145+
'{data}',
146+
'{args}',
147+
'{input}',
148+
],
149+
[
150+
$event->connectionName,
151+
get_class($event->job),
152+
json_encode($event->data),
153+
implode(', ', array_keys($event->data)),
154+
implode(', ', array_values($event->data)),
155+
],
156+
$this->app['config']->get( 'newrelic.job_name_provider' )
157+
);
158+
}
159+
112160
/**
113161
* Get the request method
114162
*

src/config/config.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
*/
1818
'auto_name_transactions' => env('NEWRELIC_AUTO_NAME_TRANSACTION', true),
1919

20+
/*
21+
* Will automatically name queued jobs in NewRelic,
22+
* using the Laravel job class, data, or connection name.
23+
*
24+
* Set this to false to use the NewRelic default naming
25+
* scheme, or to set your own in your application.
26+
*/
27+
'auto_name_jobs' => env('NEWRELIC_AUTO_NAME_JOB', true),
28+
2029
/*
2130
* Define the name used when automatically naming transactions.
2231
* a token string:
@@ -33,6 +42,23 @@
3342
*/
3443
'name_provider' => env('NEWRELIC_NAME_PROVIDER', '{uri} {route}'),
3544

45+
/*
46+
* Define the name used when automatically naming queued jobs.
47+
* a token string:
48+
* a pattern you define yourself, available tokens:
49+
* {connection} = The name of the queue connection
50+
* {class} = The name of the job class
51+
* {data} = JSON string with all data passed to the job
52+
* {args} = List of variable names passed to the job
53+
* {input} = List of values passed to the job
54+
* anything that is not a matched token will remain a string literal
55+
* example:
56+
* Given a job named App\MyJob, with data {"subject":"hello","to":"world"},
57+
* the pattern 'I say {input} when I run {class}' would return:
58+
* 'I say hello, world when I run App\MyJob'
59+
*/
60+
'job_name_provider' => env('NEWRELIC_JOB_NAME_PROVIDER', '{class}'),
61+
3662
/*
3763
* Will cause an exception to be thrown if the NewRelic
3864
* PHP agent is not found / installed

0 commit comments

Comments
 (0)