Yii 1.1: New Relic Error Alert with Yii

4 followers

Introduction

New Relic is a SaaS company that provides a native PHP extension to interface with their application performance tracking and metrics engine.

Out of the box with the default New Relic installation on your server, it will be able to track your application performance. However, one thing that's missing will be Error Alerts since Yii's error handling seems to take over the PHP error handling as opposed to extending it.

yii-relic - an existing Yii Ext help integrate New Relic with Yii. If you find this a little heavy, you can easily implement the following using the New Relic PHP API:

1) Create a new class under /protected/components/ErrorHandler.php to extend CErrorHandler

<?php
/**
 * Extending CErrorHandler to integrate with New Relic errors
 * 
 */
class ErrorHandler extends CErrorHandler
{
    /**
     * Handles the exception.
     * @param Exception $exception the exception captured
     */
    protected function handleException($exception)
    {
        if (extension_loaded('newrelic')) {
            newrelic_notice_error( $exception->getMessage(), $exception );
        }
 
        parent::handleException( $exception );
    }
}

2) In your config file, add the following under component:

'errorHandler'=>array(
 
                'class' => 'ErrorHandler',
...
            ),

That's it! This will send your errors to New Relic via their API.

Total 2 comments

#14582 report it
Jai Sundar at 2013/08/24 05:13pm
Great

Nice one.

#14561 report it
pmaselkowski at 2013/08/22 03:18pm
Cool

Nice idea, thanks for sharing. I also looked for mentioned extension, for some additional new relic functionality one can simly use new relic api calls, ie for transaction naming:

public function beforeControllerAction($controller, $action) {
    if (extension_loaded('newrelic')) {
         newrelic_name_transaction($controller->id . '/' . $action->id);
    }
    return parent::beforeControllerAction($controller, $action);
}

Leave a comment

Please to leave your comment.

Write new article