neeckeloo/newrelic

NewRelic模块用于Laminas

v3.0.0 2020-06-27 15:17 UTC

README

NewRelic模块为New Relic监控系统提供一个面向对象的PHP包装器。

Build Status Latest Stable Version Total Downloads

简介

NewRelic模块提供了一个日志记录器和New Relic PHP API的包装器。

当前路由用于设置每个事务的名称。此外,如果启用,模块允许记录异常。

需求

  • PHP ^7.2
  • Laminas

安装

NewRelic模块仅通过Composer官方支持安装。有关Composer文档,请参阅 getcomposer.org

您可以从命令行安装模块

$ composer require neeckeloo/newrelic

或者,您也可以在您的 composer.json 文件中手动添加依赖项

{
    "require": {
        "neeckeloo/newrelic": "^2.5"
    }
}

通过在您的 application.config.php 文件中添加 NewRelic 键来启用模块。通过将 newrelic.global.php.dist 文件复制到您的 config/autoload 文件夹中来自定义模块。

默认配置

return [
    'newrelic' => [
        // Sets the newrelic app name.  Note that this will discard metrics
        // collected before the name is set.  If empty then your php.ini
        // configuration will take precedence. You can set the value by
        // environment variable, or by overriding in a local config.
        'application_name' => getenv('NEW_RELIC_APP_NAME') ?: null,

        // May be null and will only be set if application name is also given.
        // You can set the value by environment variable, or by overriding in 
        // a local config.
        'license' => getenv('NEW_RELIC_LICENSE_KEY ') ?: null,

        // If false then neither change the auto_instrument or manually
        // instrument the real user monitoring.
        'browser_timing_enabled' => false,

        // When true tell the newrelic extension to insert Real User Monitoring
        // scripts automatically.
        'browser_timing_auto_instrument' => true,

        // When true, a logger with the newrelic writer will be called for
        // dispatch error events.
        'exceptions_logging_enabled' => false,

        // Defines ignored transactions
        'ignored_transactions' => [],

        // Defines background job transactions
        'background_jobs' => [],
    ],
];

用法

定义事务名称

默认情况下,模块使用 NewRelic\Listener\RequestListener 自动使用匹配的路由名称指定事务名称。

事务名称提供者

事务名称从配置中定义的提供者(默认为 NewRelic\TransactionNameProvider\RouteNameProvider)检索。

use NewRelic\TransactionNameProvider\RouteNameProvider;

return [
    'newrelic' => [
        'transaction_name_provider' => RouteNameProvider::class,
    ],
];

该包包含一些提供者

  • RouteNameProvider
  • HttpRequestUrlProvider
  • NullProvider

手动指定事务名称

您也可以通过将 NullProvider 定义为事务名称提供者并使用客户端的 nameTransaction 方法来自定义事务名称。

忽略事务

NewRelic API允许忽略一些事务。此配置定义了一些将被忽略的事务路由和控制器的规则。

忽略路由

return [
    'newrelic' => [
        'ignored_transactions' => [
            'routes' => [
                'admin*',
                'user/login',
            ],
        ],
    ],
];

这些规则忽略所有管理员路由和 "user/login" 路由。

忽略控制器

return [
    'newrelic' => [
        'ignored_transactions' => [
            'controllers' => [
                'FooController',
                'BarController',
                'BazController',
            ],
        ],
    ],
];

您还可以忽略指定控制器的一些操作。

return [
    'newrelic' => [
        'ignored_transactions' => [
            'controllers' => [
                ['FooController', ['foo', 'bar']],
                ['BarController', ['baz']],
            ],
        ],
    ],
];

手动忽略事务

您可以通过调用NewRelic客户端的 ignoreTransaction() 方法手动忽略事务。

$client = $container->get('NewRelic\Client');
$client->ignoreTransaction();

定义后台作业

后台作业的配置与忽略事务相同,但使用以下键 background_jobs

return [
    'newrelic' => [
        'background_jobs' => [],
    ],
];

手动定义后台作业

您可以通过调用NewRelic客户端的 backgroundJob() 方法手动将事务定义为后台作业。

$client = $container->get('NewRelic\Client');
$client->backgroundJob(true);

忽略apdex指标

您可以使用键 ignored_apdex 忽略类似于事务指标之类的apdex指标。

return [
    'newrelic' => [
        'ignored_apdex' => [],
    ],
];

手动忽略apdex指标

您可以通过调用NewRelic客户端的 ignoreApdex() 方法手动忽略apdex指标。

$client = $container->get('NewRelic\Client');
$client->ignoreApdex();

添加自定义指标

$client = $container->get('NewRelic\Client');
$client->addCustomMetric('salesprice', $price);