maksimer / ms-application-insights-laravel
Microsoft Azure Application Insights for Laravel 5
Requires
- php: >=5.4.2
- guzzlehttp/guzzle: ^6.3
- illuminate/config: ~5.7||^6.0
- illuminate/support: ~5.7||^6.0
- laravel/framework: ~5.7||^6.0
- microsoft/application-insights: ^0.4.4
README
为Microsoft Application Insights提供的简单Laravel实现
安装
更新应用的应用程序 composer.json 文件的 require 部分
"require": { ... "marchie/ms-application-insights-laravel": "^0.2", ... }
服务提供者
将服务提供者添加到应用程序的 config/app.php 文件中的 providers 数组中
'providers' => [ ... Marchie\MSApplicationInsightsLaravel\Providers\MSApplicationInsightsServiceProvider::class, ... ]
外观
将外观添加到应用程序的 config/app.php 文件中的 aliases 数组中
'aliases' => [ ... 'AIClient' => Marchie\MSApplicationInsightsLaravel\Facades\MSApplicationInsightsClientFacade::class, 'AIServer' => Marchie\MSApplicationInsightsLaravel\Facades\MSApplicationInsightsServerFacade::class, ... ]
instrumentation Key
该包将检查应用程序的 .env 文件中的 Instrumentation Key。
将以下内容添加到您的 .env 文件中
...
MS_INSTRUMENTATION_KEY=<your instrumentation key>
...
您可以在 Microsoft Azure Portal 上找到您的 instrumentation key。
导航到
Microsoft Azure > Browse > Application Insights > (Application Name) > Settings > Properties
用法
请求跟踪中间件
要使用请求跟踪监控应用程序的性能,请将中间件添加到您的应用程序中,该中间件位于 app/Http/Kernel.php。它必须在添加 StartSession 中间件之后添加。
protected $middleware = [ ... 'MSApplicationInsightsMiddleware', ... ]
请求将向 Application Insights 发送以下附加属性
- ajax (布尔值):如果请求是 AJAX 请求,则为 true
- ip (字符串):客户端的 IP 地址
- pjax (布尔值):如果请求是 PJAX 请求,则为 true
- secure (布尔值):如果请求通过 HTTPS 发送,则为 true
- route (字符串):如果适用,则为路由名称
- user (整数):如果适用,则为登录用户的 ID
- referer (字符串):如果可用,则为请求的 HTTP_REFERER 值
中间件还用于估计用户在特定页面上的时间。这作为名为 browse_duration 的 trace 事件发送。
异常处理器
要报告应用程序中发生的异常,请使用提供的异常处理器。在应用程序的 app/Handlers/Exception.php 文件中替换以下行
... # Delete this line use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; # Insert this line use Marchie\MSApplicationInsightsLaravel\Handlers\MSApplicationInsightsExceptionHandler as ExceptionHandler; ...
异常处理器将发送与上述相同的附加属性到 Application Insights。
客户端
为了从客户端将页面视图信息注册到 Application Insights,只需将以下代码插入您的 Blade 视图中即可
{!! AIClient::javascript() !!}
注意:Microsoft 建议您将脚本放在页面的 <head> 部分,以便计算客户端页面加载时间的最大值。
自定义
如果您想使用任何底层 ApplicationInsights-PHP 功能,您可以直接从服务器外观调用方法
... AIServer::trackEvent('Test event'); AIServer::flush(); ...
有关可用方法的更多信息,请参阅 ApplicationInsights-PHP 页面。
版本历史
0.2.4
- 当向 Application Insights 提交数据时添加 try/catch 块,以防止 RequestExceptions 在连接有问题时杀死应用程序。
0.2.3
- 纠正了 dingus 错误!
0.2.2
- 向异常添加了更多属性
- 删除了自动刷新关闭函数
0.2.1
- 在异常上刷新队列(否则,如果 Laravel 是守护进程,则队列异常不会报告)
0.2.0
- 服务器端实现
0.1.2
- 纠正了愚蠢的错误!
0.1.1
- 空键不再引发异常(不将JavaScript插入到视图中)
0.1.0
- 仅客户端JavaScript