alamote / application-insights
本项目扩展了 Application Insights API 接口,以支持 PHP。
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.5
Requires (Dev)
- evert/phpdoc-md: ~0.0.7
- phpunit/phpunit: ~4.8.36
This package is auto-updated.
Last update: 2024-09-13 20:48:34 UTC
README
本项目扩展了 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,引用到 Request
$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与我们联系,提出任何其他问题或意见。