roenschg/php-newrelic

Wrapper for the NewRelic Agent API that allows to send big amounts of custom events reliably.

v2.1.1 2021-06-17 15:34 UTC

This package is auto-updated.

Last update: 2024-10-17 22:53:49 UTC


README

This package provides a simple and clean wrapper around the newrelic APM agent API and also enables you to send large amounts of custom metrics into newrelic without dropping any data.

Installation

composer require groensch/php-newrelic

Basic Usage

You have one class that acts as a wrapper for every function that the PHP agent api defines.

$newRelic = new Groensch\NewRelic\Handler(new Groensch\NewRelic\CustomEventHandler\PHPAgent());

$newRelic->nameTransaction('MyCustomTransactionName');
$newRelic->recordCustomEvent('eventName', ['count' => 20]);

Crossed fingers (Instead of using extension_loaded('newrelic'))

The CrossedFingers handler allows you to use the same methods on environments without a NewRelic extension installed without throwing errors. So instead of doing this if on every line where you want to send events to:

if (extension_loaded('newrelic')) { // Ensure PHP agent is available
    newrelic_name_transaction("/Product/view/");
}
$newRelic = new Groensch\NewRelic\Handler(
    new \Groensch\NewRelic\CustomEventHandler\CrossedFingers(),
    new \Groensch\NewRelic\TransactionHandler\CrossedFingers()
);

$newRelic->nameTransaction('MyCustomTransactionName');
$newRelic->recordCustomEvent('eventName', ['count' => 20]);

If the extension does not exist, nothing will be done.

HTTP Bulk sending (Send huge amounts of custom events reliable)

If you need to make sure that you send all the data to NewRelic even if you reach the limits of the agent you can use the CustomEventHandler AutoBulkHttp.

Please note that this means you application will be slowed down by the amount of requests you send to NewRelic because of the logged custom events.

The following code shows you how to setup the NewRelicHandler to send the CustomEvents over the API and automaticly pack requests together in bulks.

use Groensch\NewRelic\Handler as NewRelicHandler;
use Groensch\NewRelic\HttpInsertApi as NewRelicHttpInsertApi;
use Groensch\NewRelic\CustomEventHandler\AutoBulkHttp as NewRelicCustomEventBulkHttpHandler;
    
// Configure NewRelic handler
$newRelicHttpApi = new NewRelicHttpInsertApi(NEW_RELIC_API_ACCOUNT_ID, NEW_RELIC_API_INSERT_KEY);
$newRelicCustomEventHandler = new NewRelicCustomEventBulkHttpHandler($newRelicHttpApi);
$newRelic = new NewRelicHandler($newRelicCustomEventHandler);
    
// Record custom event
$newRelic->recordCustomEvent('test1', ['recorded' => (string) time()]);
$newRelic->recordCustomEvent('test2', ['recorded' => (string) time()]);

The NewRelicHttpInsertApi is responsible to connect to the InsightAPI and the NewRelicCustomEventBulkHttpHandler is responsible to pack custom events together in bulk and is aware of the API limitations.

If the package is big enough it will flush the buffer and send the request to the insights api. The Buffer will be cleared during shutdown of you application as well.

If the data you pass to the method ecordCustomEvent is invalid or to big, you will get an Exception.

List of methods

For further information about the methods please see the NewRelic documentation

  • recordCustomEvent(...)
  • addCustomParameter(...)
  • backgroundJob(...)
  • captureParams(...)
  • customMetric(...)
  • disableAutorum(...)
  • endOfTransaction(...)
  • endTransaction(...)
  • ignoreApdex(...)
  • ignoreTransaction(...)
  • nameTransaction(...)
  • noticeError(...)
  • recordDatastoreSegment(...)
  • setAppname(...)
  • setUserAttributes(...)
  • startTransaction(...)
  • getTransactionHandler(...)
  • setTransactionHandler(...)

Running Unit-Tests

docker-compose run php composer install
docker-compose run php vendor/bin/phpunit

Running Code-Sniffer

docker-compose run php composer install
docker-compose run php vendor/bin/phpcs --config-set installed_paths vendor/escapestudios/symfony2-coding-standard
docker-compose run php vendor/bin/phpcs