Newrelic isn't recognizing my Slim PHP routes
Asked Answered
S

4

7

I have just set up New Relic on my PHP web app. Everything is working great except one thing... All of the transactions show as going through "index.php".

The reason for this because I'm using the Slim framework (there are many alternatives for routing) with URL rewriting so that I can have nice human URLs like "/user/settings" without a folder for every controller and action.

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

Saire answered 25/7, 2013 at 14:14 Comment(0)
D
8

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

Here is an example setting it to the pattern:

$app->hook('slim.before.dispatch', function() use ($app) {
    newrelic_name_transaction($app->router()->getCurrentRoute()->getPattern());
});
Denman answered 3/1, 2014 at 0:38 Comment(0)
S
8

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

A little modification made it work for me (with Slim), and I imagine other PHP routers and frameworks will have roughly the same solution:

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

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

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

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

Saire answered 25/7, 2013 at 14:14 Comment(3)
You might want to have a look at this: "You should absolutely NOT include any request-specific data in the transaction name. So for example, using the actual request URL is extremely bad." newrelic.com/docs/php/…Cello
They say that - but they dont really offer an allternativeTaiga
I think in many cases the request url isn't going to be useful to people. For example, if you have an e-commerce site with 10,000 products, you'd want 'product page' as a single action and not 10,000 individual ones. Otherwise it'd be hard to get much out of the reporting UI.Increasing
D
8

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

Here is an example setting it to the pattern:

$app->hook('slim.before.dispatch', function() use ($app) {
    newrelic_name_transaction($app->router()->getCurrentRoute()->getPattern());
});
Denman answered 3/1, 2014 at 0:38 Comment(0)
S
1

New Relic now has out-of-the-box support for the Slim Framework starting with version 6.7.0.174 of the PHP Agent.

Saith answered 21/9, 2016 at 21:7 Comment(0)
P
0

I updated to the NewRelic agent 6.9.0.182 but transactions are still not named so I put a middleware (since Slim 3 does not support hook anymore) instead 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();
Phebephedra answered 2/2, 2017 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.