djstarcom/newrelic

NewRelic Tools for PHP

v2.0.2 2018-06-10 21:55 UTC

This package is auto-updated.

Last update: 2024-09-29 04:31:30 UTC


README

Build Status Code Coverage

New Relic 事务库

使用此库将后台作业或长时间运行的脚本报告给 New Relic APM。

示例

<?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();
}

您必须在服务器上配置并运行代理

配置选项

使用 TransactionConfig 类来个性化您的作业。

  • 您可以使用 transactionName 字段来指定每个事务的名称

    未指定时默认为 index.php

  • 您可以使用 applicationName 字段来指定您的应用程序名称

    未指定时默认为 PHP 应用程序

  • 您可以使用 monitoredMethodName 字段来指定要监控的单个方法

    如果未定义,则每个方法的调用都将被视为一个事务

New Relic Insights 库

使用此库可以轻松地将自定义事件发布到 New Relic Insights。

<?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();

您可以在 Insights https://insights.newrelic.com/accounts/99999/manage/add_data 中找到您的密钥

配置

  • 您的 base_uri 必须以斜杠 / 结尾
  • 您必须将 99999 替换为您自己的账户号

安装

推荐的安装方式是通过 Composer

# Install Composer
curl -sS https://getcomposer.org/installer | php

接下来,运行 Composer 命令以安装最新稳定版本

composer.phar require djstarcom/newrelic