soilcapital/application-insights

本项目扩展了 Application Insights API 表面,以支持 PHP。

0.4.6 2022-05-11 12:17 UTC

This package is not auto-updated.

Last update: 2024-09-26 18:54:34 UTC


README

Build Status Packagist Pre Release

本项目扩展了 Application Insights API 表面以支持 PHP。Application Insights 是一项服务,允许开发者保持其应用程序的可用性、性能和成功。此 PHP 模块将允许您将各种类型的遥测(事件、跟踪、异常等)发送到 Application Insights 服务,在那里它们可以在 Azure Portal 中进行可视化。

状态

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

要求

支持 PHP 版本 >=5.4.2。

要使用 Microsoft Visual Studio 打开项目,您将需要 PHP Tools for Visual Studio。这不是必需的。

安装

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

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

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

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

composer require microsoft/application-insights

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

require_once 'vendor/autoload.php';

用法

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

注意:在发送数据之前,您将需要一个 instrumentation key。有关更多信息,请参阅 获取 Application Insights instrumentation key 部分。

初始化客户端并设置 instrumentation key 以及其他可选配置

$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,您需要设置 Operation Context,引用到请求

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

设置客户端在发送前对数据进行gzip压缩

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

行为准则

本项目已采用微软开源行为准则。更多信息请参阅行为准则常见问题解答或联系opencode@microsoft.com提出任何额外问题或意见。