frnkly/laravel-keen 问题修复 & 功能扩展

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

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

frnkly/laravel-keen

Composer 安装命令:

composer require frnkly/laravel-keen

包简介

An efficient Keen.io integration for Laravel

README 文档

README

Defer sending event data to Keen.io until after a request has been fulfilled–automatically and transparently.

Latest Stable Version Build Status Total Downloads License

This package provides a light wrapper around the Keen PHP client, as well as a middleware configured to defer sending event data to Keen until after each request has been fulfilled. That means that as the number of tracked events increase, the impact on request time remains virtually non-existent. For convenience, the middleware can also track every request automatically.

Features

  • Deferred event tracking 🙌
  • Optional automatic tracking of every request
  • Support data enrichment out of the box
  • Extensible middleware
  • Option to ignore specific HTTP status codes (e.g. 100, 302, etc.)
    • Or any request based on custom logic
  • More to come!

Installation

Install the Laravel + Keen package using Composer:

$ composer require frnkly/laravel-keen

This will also install the Keen PHP client as a dependency.

Configuration

Your Keen project ID and keys should be defined in the config/services.php configuration file, but will usually come from the environment configuration:

return [
    // Other 3rd-party Services
    // ...
    
    // Add your Keen information here
    'keen' => [
        'id'     => env('KEEN_PROJECT_ID'),
        'master' => env('KEEN_MASTER_KEY'),
        'write'  => env('KEEN_WRITE_KEY'),
    ],
];

And in your .env file, you might add something like:

KEEN_PROJECT_ID=your-project-id
KEEN_MASTER_KEY=your-master-key
KEEN_WRITE_KEY=your-write-key

Getting started

Register the middleware globally in app/Http/Kernel.php:

protected $middleware = [
    // Other Middleware
    // ...
    
    \Frnkly\LaravelKeen\Middleware::class,
];

Or within a middleware group:

protected $middlewareGroups = [
    'web' => [
        // Other "Web" Middleware
        // ...
        
        \Frnkly\LaravelKeen\Middleware::class,
    ],

    'api' => [
        // Other "API" Middleware
        // ...
        
        \Frnkly\LaravelKeen\Middleware::class,
    ],
];

Thanks to package discovery in Laravel 5.5, the service provider should already be available. It can also be manually registered through the config/app.php configuration file:

'providers' => [
    // Other Service Providers
    // ...

    Frnkly\LaravelKeen\ServiceProvider::class,
]

Automatic tracking of every request

Automatic tracking is enabled by default. You may disable this behaviour through the configuration file:

'keen' => [
    // Other Keen options
    // ...
    
    'track_requests' => false,
],

Data enrichment

To automatically add data enrichment to each request, use the following configuration options:

'keen' => [
    // Other Keen settings
    // ...
    
    'addons' => [
        'ip_to_geo'       => true,  // IP to Geo parser
        'ua_parser'       => true,  // User Agent parser
        'referrer_parser' => true,  // Referrer parser
    ],
],

Each data enrichment object will appear in your Keen stream under the same name as above (i.e. ip_to_geo and ua_parser).

Using your own middleware

The included middleware makes it simple to extend and gain more granular control over the data that gets sent to Keen. You can create your own middleware using Artisan, and then have it extend \Frnkly\LaravelKeen\Middleware:

$ php artisan make:middleware TracksRequests

Inside the new middleware, override the protected method buildRequestEventData:

<?php

namespace App\Http\Middleware;

class TracksRequests extends \Frnkly\LaravelKeen\Middleware
{
    /**
     * Data for "request" event.
     *
     * @param \Illuminate\Http\Request  $request
     * @param \Illuminate\Http\Response $response
     */
    protected function buildRequestEventData($request, $response)
    {
        parent::buildRequestEventData($request, $response);
        
        // Add or overwrite values.
        $host      = 'staging';
        $overwrite = true;
        $this->client->addRequestEventData('host', $host, $overwrite)
        
        // Add parameters to array values.
        ->addRequestEventParams([
            'user' => $request->user()->id
        ])
        ->addRequestEventParams([
            'mime' => $request->getMimeType('html')
        ], 'response')
        
        // Add data enrichment.
        ->addRequestEventData('target_url', 'https://example.com')
        ->enrichRequestEvent([
            'name'  => 'url_parser',
            'output'=> 'url_parser',
            'input' => ['url' => 'target_url']
        ]);
    }
}

Remember to update your app/Http/Kernel.php file to use your own middleware class instead of the pre-configured one.

Skipping requests

Some response codes do not need to be tracked, such as redirects. You can configure a list of response codes to ignore in your middleware:

/**
 * @var array
 */
protected $skipResponseCodes = [
    100,
    101,
    301,
    302,
    307,
    308,
];

You may also override the shouldRun method in the middleware:

/**
 * Determines if the middleware should run or not.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Illuminate\Http\Response $response
 * @return bool
 */
protected function shouldRun($request, $response) : bool
{
    if (! parent::shouldRun($request, $response)) {
        return false;
    }
    
    // Define your own custom logic here.
    // ...

    return true;
}

Type-hinting

The LaravelKeen client can be type-hinted in the constructor of any class that is resolved by the container:

namespace App\Events;

use App\Events\OrderShipped;
use Frnkly\LaravelKeen\Client;

class OrderShipped
{
    private $client;
    
    public function __construct(Client $client)
    {
        // The client can now be accessed by all methods in this class.
        $this->client = $client;
    }
    
    public function handle(OrderShipped $event)
    {
        // By adding a deferred event, we can continue working on the request
        // without waiting on a reply back from Keen.io. The event will be
        // processed once the request is done–automatically and transparently.
        $this->client->addDeferredEvent('order-shipped', [
            // Event data...
        ]);
    }
}

Controllers can also take advantage of method injection:

use Frnkly\LaravelKeen\Client;

class UserController extends Controller
{
    public function store(Client $client)
    {
        $client->addDeferredEvent('new-user', [
            // Event data...
        ]);
    }
}

This project is licensed under the MIT license.

I'd love to hear your comments or questions about this project. If you have an idea on how to improve it, create an issue or get in touch.

frnkly/laravel-keen 适用场景与选型建议

frnkly/laravel-keen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 198 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 10 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 198
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-09