app-insights-php/application-insights

此包已被弃用且不再维护。未建议替代包。

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

0.5.1 2023-10-13 22:01 UTC

README

这是一个由微软归档的项目分支。

Tests

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

状态

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

要求

支持 PHP 版本 >= 5.4.2。

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

安装

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

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

require: "app-insights-php/application-insights": "*"

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

composer require app-insights-php/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();

行为准则

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