protonemedia/laravel-tracer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

protonemedia/laravel-tracer

最新稳定版本:v0.3.3

Composer 安装命令:

composer require protonemedia/laravel-tracer

包简介

README 文档

README

Do not use in production!

Latest Version on Packagist Build Status Quality Score Total Downloads

A package to log request of authenticated users by bundling and qualifying routes.

Installation

You can install the package via composer:

composer require protonemedia/laravel-tracer

Publish the migration and config file and then run the migration.

php artisan vendor:publish
php artisan migrate

Usage

Add the QualifyRoute middleware to your Route Middleware stack:

class Kernel extends \Illuminate\Foundation\Http\Kernel
{
    protected $routeMiddleware = [
        // ...
        'qualify' => \ProtoneMedia\LaravelTracer\Middleware\QualifyRoute::class,
        // ...
    ];
}

Add the TraceUser middleware to the routes or groups you want to trace, for example in the web.php routes file:

use ProtoneMedia\LaravelTracer\Middleware\TraceUser;

Route::group(['middleware' => [TraceUser::class]], function () {
    Route::get('home', 'HomeController');

    Route::get('settings', 'SettingsController')->name('settings.show');

    Route::get('privacy-policy', 'PrivacyPolicyController')->name('privacyPolicy.show')->middleware('qualify:terms');

    Route::get('profile/notifications', 'NotificationsController')->name('notifications.index')->middleware('qualify:notifications,60');

    Route::group(['middleware' => ['qualify:finance']], function () {
        Route::get('invoices', 'InvoicesController@index')->name('invoices.index');
        Route::get('invoices/{id}', 'InvoicesController@show')->name('invoices.show');
    });

    Route::get('machine/{id}', 'MachineController');
    Route::get('rack/{id}', 'RackController')->middleware('qualify:rack');
    Route::get('server/{id}', 'ServerController')->middleware('qualify:server.{id}');
});

Now every request in this example will be logged to the user_requests table. You can use the ProtoneMedia\LaravelTracer\UserRequest model the retrieve the log entries.

Qualify routes

As you can see there are some example routes added to the group of routes we want to trace. Let's explain how each route will be qualified.

  • A GET request to /home will simply be qualified as home since it has no name and no qualifier.
  • A GET request to /settings will be qualified as settings.show because it has a name but still no qualifier.
  • A GET request to /privacy-policy will be qualified as terms, this has been accomplished with the qualify middleware.

Rate limiting

  • A GET request to /profile/notifications will be qualified as notifications and as you can see, the qualify middleware was given a second parameter. This is the number of seconds between each log of this qualifier. When a user visits this route more than once in 60 seconds, it will be stored as 1 request.

Grouped qualifiers

  • A GET request to both /invoices and /invoices/1 will be qualified as finance.

Parameters

  • A GET request /machine/1 will be qualified as machine/1, it has no name and no qualifier so the path will be used as qualifier.
  • A GET request to both /rack/1 and /rack/2 will be qualified as rack. Though the route has a parameter, the qualifier will be used.
  • A GET request to /server/1 will be qualified as server.1. The parameter in the qualifier will be replaced with the actual value used in the path.

Qualify from the controller

This package adds two macros to Illuminate\Http\Request. The first one is qualifyAs. Just as the QualifyRoute middleware this method takes two parameters. The first parameter is the name and the second paramater (optional) is the number seconds to be used by the Rate Limiter. From your controller you could qualify the route using the Request object or by using the request() helper method. The other macro is qualifiedRoute which is a getter for the QualifiedRoute instance.

use Illuminate\Http\Request;

class TicketsController extends Controller
{
    public function index()
    {
        request()->qualifyAs('tickets', 60);
    }

    public function show(Request $request, $id)
    {
        $request->qualifyAs('tickets');
    }

    public function delete(Request $request, $id)
    {
        $qualifiedRoute = $request->qualifiedRoute();

        $qualifiedRoute->name();
        $qualifiedRoute->secondsBetweenLogs();
    }
}

Config

The config file consists of only two options. The first option is seconds_between_logs which can be used to set a default for the Rate Limiter. The second option is should_trace_user where you can specify a class@method which should return a boolean that specifies wether to trace or not. It takes two parameters: $request and $response:

// config/laravel-tracer.php

return [
    'seconds_between_logs' => 120,

    'should_trace_user' => 'MyClass@shouldTrace',
];
// MyClass.php

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class MyClass
{
    public function shouldTrace(Request $request, Response $response): bool
    {
        return !$request->user()->isAdmin();
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email pascal@protone.media instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

protonemedia/laravel-tracer 适用场景与选型建议

protonemedia/laravel-tracer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.25k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「protonemedia」 「laravel-tracer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 protonemedia/laravel-tracer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 protonemedia/laravel-tracer 我们能提供哪些服务?
定制开发 / 二次开发

基于 protonemedia/laravel-tracer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-17