New Relic Libraries
, (*1)
New Relic Transaction Library
Use this library to report background jobs or long running scripts to New Relic APM., (*2)
Examples
<?php
namespace Consumers;
class Email
{
public function sendEmail($recipient, $body, $header)
{
//Send email
}
public function beforePerform()
{
//Before Hook
}
public function perform()
{
//Perform a job
}
public function afterPerform()
{
//After Hook
}
}
namespace Foo\Bar;
use DJStarCOM\NewRelic;
use Consumers;
$consumer = new Consumers\Email();
while (true) {
$transactionConfig = new NewRelic\Config\TransactionConfig();
$transactionConfig->applicationName = 'Background Jobs';
$transactionConfig->transactionName = 'consumer::sendEmail';
$consumerMonitored = new NewRelic\Transaction($consumer, $transactionConfig);
$consumerMonitored->sendEmail('Spock', 'James', 'Tiberius');
$transactionConfig->monitoredMethodName = 'perform';
$consumerMonitored->beforePerform();
$consumerMonitored->perform();
$consumerMonitored->afterPerform();
}
You MUST have an agent configured and running on the server, (*3)
Configuration options
Use TransactionConfig class to personalize your job., (*4)
-
You can use transactionName field to specify the name of each transactions, (*5)
Defaults to index.php if not specified, (*6)
-
You can use applicationName field to specify the name your application, (*7)
Defaults to PHP Application if not specified, (*8)
-
You can use monitoredMethodName field to specify only one method to be monitored, (*9)
If not defined every call to a method will be considered one transaction, (*10)
New Relic Insights Library
Use this library to easily post custom events to New Relic Insights., (*11)
<?php
use DJStarCOM\NewRelic;
use GuzzleHttp\Client;
$client = new Client([
#You need to change it to your account number
'base_uri' => 'https://insights-collector.newrelic.com/v1/accounts/99999/'
]);
$this->newRelicInsights = new NewRelic\Insights($client, 'YOUR_KEY_HERE');
$events = new NewRelic\Entity\Insights\EventCollection();
$event = new NewRelic\Entity\Insights\Event();
$event->eventType = 'Purchase';
$event->account = 3;
$event->amount = 259.54;
$events->add($event);
$event2 = new NewRelic\Entity\Insights\Event();
$event2->eventType = 'Purchase';
$event2->account = 4;
$events->add($event2);
$promise = $this->newRelicInsights->sendEvent($events);
$promise->wait();
You can find your key at Insights https://insights.newrelic.com/accounts/99999/manage/add_data, (*12)
Configuring
- Your
base_uri MUST end with trailing slash /
- You MUST replace
99999 with your account number
Installing
The recommended way to install is through
Composer., (*13)
# Install Composer
curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version:, (*14)
composer.phar require djstarcom/newrelic