Newrelic does not recognize my thin PHP routes

I just created a new relic in my PHP web application. Everything works fine except for one ... All transactions show that they go through "index.php".

The reason for this is because I use the Slim framework (there are many alternatives for routing ) with URL rewriting so that I have nice human URLs like "/ user / settings" without a folder for each controller and actions.

But that still leaves me with index.php as the name for every new transaction New Relic.

+7
php frameworks url-rewriting newrelic slim
source share
4 answers

You can use the hook to set the transaction name to the name or pattern of the router.

Here is an example setting it in a template:

 $app->hook('slim.before.dispatch', function() use ($app) { newrelic_name_transaction($app->router()->getCurrentRoute()->getPattern()); }); 
+8
source share

It took me some searches, however I was able to find the answer ( available here ) related to CodeIgniter.

A small modification made it work for me (with Slim), and I believe that other PHP routers and frameworks will have about the same solution:

 if (extension_loaded ('newrelic')) { newrelic_name_transaction($_SERVER['REQUEST_URI']); } 

Edit: to avoid including any GET parameters, use this in the second line:

 newrelic_name_transaction(current(explode('?', $_SERVER['REQUEST_URI']))) 

Note. Emerson's answer, where he recommends using a route pattern, is much better than using a literal URL if you are using Slim.

+8
source share

The new Relic now has built-in support for the Slim Framework, starting with version 6.7.0.174 of the PHP agent.

+1
source share

I updated the NewRelic agent 6.9.0.182, but the transactions are still not named, so I put the middleware (since Slim 3 no longer supports hook) and it works better:

 $app = new \Slim\App(['settings' => [ // to be able access to route within middleware 'determineRouteBeforeAppMiddleware' => true, ]]); // middleware to send the correct route to NewRelic $app->add(function ($request, $response, $next) { if (extension_loaded('newrelic') && $request->getAttribute('route')) { newrelic_name_transaction($request->getAttribute('route')->getPattern()); } return $next($request, $response); }); // loads some routes $app->run(); 
0
source share

All Articles