marchie / ms-application-insights-laravel
Microsoft Azure Application Insights for Laravel 5
Requires
- php: >=5.4.0
- illuminate/config: ~5.0
- illuminate/support: ~5.0
- laravel/framework: ~5.0
- microsoft/application-insights: ^0.3.0
This package is not auto-updated.
Last update: 2024-09-14 17:37:22 UTC
README
一个简单的Laravel实现,用于Microsoft Application Insights
安装
更新应用程序的require
部分 composer.json 文件
"require": { ... "marchie/ms-application-insights-laravel": "^0.2", ... }
仪器密钥
该包将检查您的应用程序的 .env 文件以获取您的 仪器密钥。
将以下内容添加到您的 .env 文件中
...
MS_INSTRUMENTATION_KEY=<your instrumentation key>
...
您可以在Microsoft Azure Portal上找到您的仪器密钥。
导航到
Microsoft Azure > Browse > Application Insights > (Application Name) > Settings > Properties
Laravel版本没有自动发现(< 5.5)时
服务提供者
将服务提供者添加到应用程序的 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, ... ]
用法
请求跟踪中间件
要使用请求跟踪监控您的应用程序性能,请将中间件添加到应用程序中,在 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在连接到Application Insights时出现问题时杀死应用程序。
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