mariopenterman/application-insights

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

0.4.6 2020-10-06 09:20 UTC

This package is auto-updated.

Last update: 2024-09-06 18:44:47 UTC


README

Build Status Packagist Pre Release

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

状态

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

要求

支持 PHP 版本 >=5.4.2。

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

安装

我们已在 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 发送遥测。以下是一些示例。

注意:在您发送数据之前,您将需要一个仪表化密钥。有关更多信息,请参阅 获取 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(该 ID 将操作链接在一起)

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

行为准则

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