Skip to content

Commit 245783b

Browse files
committed
Merge pull request #7 from danwall/transaction-names
update automatic transaction naming
2 parents 7bb2666 + 80e4aad commit 245783b

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ Finally, publish the default configuration (it will end up in `app/config/packag
3232

3333
* type: bool
3434
* default: true
35-
* this will automatically name all transactions by their route name
36-
* ex: Route::get('foo/{id}/bar/{name}', ...) will be named: 'get foo/{id}/bar/{name}'
37-
* the uri parameters will not be replaced, it will be the string literal
35+
* this will automatically name all transactions with the following precedence
36+
1. Route name (e.g. "home")
37+
2. Controller and method (e.g. "HomeController@showWelcome")
38+
3. HTTP verb + path (e.g. "GET /")
39+
40+
=> **name_provider**
41+
42+
* type: closure or null
43+
* default: null
44+
* if a closure is provided, this allows for full customization of the transaction name with access to \Illuminate\Http\Request, \Illuminate\Http\Response and \Illuminate\Foundation\Application as parameters for convenience.
3845

3946
=> **throw_if_not_installed**
4047

src/Intouch/LaravelNewrelic/LaravelNewrelicServiceProvider.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,38 @@ function ( $request, $response ) use ( $app )
7878
{
7979
if ( true == $app['config']['laravel-newrelic::auto_name_transactions'] )
8080
{
81-
/** @var \Illuminate\Routing\Router $router */
82-
$router = $app['router'];
8381
/** @var \Intouch\Newrelic\Newrelic $newrelic */
8482
$newrelic = $app['newrelic'];
8583

86-
$newrelic->nameTransaction( $router->currentRouteName() );
84+
$newrelic->nameTransaction( $this->getTransactionName( $request, $response, $app ) );
8785
}
8886
}
8987
);
9088
}
89+
90+
/**
91+
* Build the transaction name
92+
*
93+
* @param \Illuminate\Http\Request $request
94+
* @param \Illuminate\Http\Response $response
95+
* @param \Illuminate\Foundation\Application $app
96+
* @return string
97+
*/
98+
protected function getTransactionName( $request, $response, $app )
99+
{
100+
$nameProvider = $app['config']['laravel-newrelic::name_provider'];
101+
102+
if ( is_callable( $nameProvider ) ) {
103+
$name = $nameProvider( $request, $response, $app );
104+
} else {
105+
/** @var \Illuminate\Routing\Router $router */
106+
$router = $app['router'];
107+
108+
$name = $router->currentRouteName()
109+
?: $router->currentRouteAction()
110+
?: $request->getMethod() . ' ' . $request->getPathInfo();
111+
}
112+
113+
return $name;
114+
}
91115
}

src/config/config.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,25 @@
1010

1111
/*
1212
* Will automatically name transactions in NewRelic,
13-
* using the Laravel route name
13+
* using the Laravel route name, action or request
1414
*/
1515
'auto_name_transactions' => true,
1616

17+
/*
18+
* Define the name provider used when automatically naming transactions.
19+
* Accepts either a closure or null to use the package default.
20+
*/
21+
'name_provider' => null,
22+
23+
// 'name_provider' => function ($request, $response, $app) {
24+
// return $app['router']->currentRouteAction()
25+
// ?: $request->getMethod() . ' ' . $request->getPathInfo();
26+
// },
27+
1728
/*
1829
* Will cause an exception to be thrown if the NewRelic
1930
* PHP agent is not found / installed
2031
*/
2132
'throw_if_not_installed' => false,
2233

23-
);
34+
);

0 commit comments

Comments
 (0)