bagaswh/application-insights

此项目扩展了 Application Insights API 面板以支持 PHP。

0.4.6 2023-11-27 07:28 UTC

This package is auto-updated.

Last update: 2024-09-27 09:16:21 UTC


README

此分支是为了添加原始仓库中缺少的一些功能。

此项目扩展了 Application Insights API 面板以支持 PHP。 Application Insights 是一项服务,允许开发者确保其应用可用、性能良好并取得成功。此 PHP 模块允许您将各种类型的遥测数据(事件、跟踪、异常等)发送到 Application Insights 服务,您可以在 Azure 门户中可视化这些数据。

状态

尽管我们过去对此 SDK 有所贡献,但 Microsoft 并不维护或支持此 SDK。请注意,Azure Monitor 仅在您使用 支持的 SDK 时提供支持。我们正在不断评估扩大对其他语言支持的机会,请关注我们的 GitHub 公告 页面,以获取最新的 SDK 新闻。

要求

支持 PHP 版本 >=5.4.2。

为了在 Microsoft Visual Studio 中打开项目,您需要 PHP Tools for Visual Studio。这不是必需的。

安装

我们在 Packagist 上发布了一个包。要使用它,首先,您需要获取 Composer

一旦您设置了项目以使用 Composer,只需将您想要使用的版本的引用添加到您的 composer.json 文件中。

require: "bagaswh/application-insights": "*"

或者,您可以使用 composer 命令自动将包添加到您的 composer.json 文件中。

composer require bagaswh/application-insights

确保您添加了 require 语句以引入库

require_once 'vendor/autoload.php';

使用方法

安装后,您可以将遥测数据发送到 Application Insights。以下是一些示例。

注意:在您发送数据之前,您将需要一个仪器密钥。请参阅 获取 Application Insights 仪器密钥 部分以获取更多信息。

初始化客户端并设置仪器密钥和其他可选配置

$telemetryClient = new \ApplicationInsights\Telemetry_Client();
$context = $telemetryClient->getContext();

// Necessary
$context->setInstrumentationKey('YOUR INSTRUMENTATION KEY');

// Optional
$context->getSessionContext()->setId(session_id());
$context->getUserContext()->setId('YOUR USER ID');
$context->getApplicationContext()->setVer('YOUR VERSION');
$context->getLocationContext()->setIp('YOUR IP');

// Start tracking
$telemetryClient->trackEvent('name of your event');
$telemetryClient->flush();

设置操作上下文

为了正确报告 Application Insights,您需要设置操作上下文,引用请求

$telemetryClient->getContext()->getOperationContext()->setId('XX');
$telemetryClient->getContext()->getOperationContext()->setName('GET Index');

通过事件名称发送简单的事件遥测项

$telemetryClient->trackEvent('name of your event');
$telemetryClient->flush();

通过自定义属性和度量发送事件遥测项

$telemetryClient->trackEvent('name of your event', ['MyCustomProperty' => 42, 'MyCustomProperty2' => 'test'], ['duration', 42]);
$telemetryClient->flush();

在发送到服务之前发送多个遥测项也受支持;API 将将所有内容批处理,直到您调用 flush()

$telemetryClient->trackEvent('name of your event');
$telemetryClient->trackEvent('name of your second event');
$telemetryClient->flush();

通过页面名称和 URL 发送简单的页面视图遥测项

$telemetryClient->trackPageView('myPageView', 'http://www.foo.com');
$telemetryClient->flush();

通过持续时间、自定义属性和度量发送页面视图遥测项

$telemetryClient->trackPageView('myPageView', 'http://www.foo.com', 256, ['InlineProperty' => 'test_value'], ['duration' => 42.0]);
$telemetryClient->flush();

通过度量名称和值发送简单的度量遥测项

$telemetryClient->trackMetric('myMetric', 42.0);
$telemetryClient->flush();

通过点类型、计数、最小值、最大值、标准差和度量发送度量遥测项

$telemetryClient->trackMetric('myMetric', 42.0, \ApplicationInsights\Channel\Contracts\Data_Point_Type::Aggregation, 5, 0, 1, 0.2, ['InlineProperty' => 'test_value']);
$telemetryClient->flush();

通过消息发送简单的消息遥测项

$telemetryClient->trackMessage('myMessage', \ApplicationInsights\Channel\Contracts\Message_Severity_Level::INFORMATION, ['InlineProperty' => 'test_value']);
$telemetryClient->flush();

通过请求名称、URL 和开始时间发送简单的请求遥测项

$telemetryClient->trackRequest('myRequest', 'http://foo.bar', time());
$telemetryClient->flush();

通过持续时间、HTTP 状态代码、请求是否成功、自定义属性和度量发送请求遥测项

$telemetryClient->trackRequest('myRequest', 'http://foo.bar', time(), 3754, 200, true, ['InlineProperty' => 'test_value'], ['duration_inner' => 42.0]);
$telemetryClient->flush();

通过自定义属性和度量发送异常遥测

try
{
    // Do something to throw an exception
}
catch (\Exception $ex)
{
    $telemetryClient->trackException($ex, ['InlineProperty' => 'test_value'], ['duration_inner' => 42.0]);
    $telemetryClient->flush();
}

将客户端设置为在发送之前压缩数据

$telemetryClient->getChannel()->setSendGzipped(true);

注册异常处理程序

class Handle_Exceptions
{
    private $_telemetryClient;

    public function __construct()
    {
        $this->_telemetryClient = new \ApplicationInsights\Telemetry_Client();
        $this->_telemetryClient->getContext()->setInstrumentationKey('YOUR INSTRUMENTATION KEY');

        set_exception_handler(array($this, 'exceptionHandler'));
    }

    function exceptionHandler(\Exception $exception)
    {
        if ($exception != NULL)
        {
            $this->_telemetryClient->trackException($exception);
            $this->_telemetryClient->flush();
        }
    }
}

发送成功的SQL依赖性遥测项

$telemetryClient->trackDependency('Query table', "SQL", 'SELECT * FROM table;', time(), 122, true);
$telemetryClient->flush();

发送失败的HTTP依赖性遥测项

$telemetryClient->trackDependency('method', "HTTP", "http://example.com/api/method", time(), 324, false, 503);
$telemetryClient->flush();

发送其他类型依赖性遥测项

$telemetryClient->trackDependency('Name of operation', "service", 'Arguments', time(), 23, true);
$telemetryClient->flush();

更改操作ID(用于连接操作)

$telemetryClient->trackMetric('interestingMetric', 10);
$telemetryClient->getContext()->getOperationContext()->setId(\ApplicationInsights\Channel\Contracts\Utils::returnGuid())
$telemetryClient->trackMetric('differentOperationMetric', 11);
$telemetryClient->flush();