定制 shifudeen/elastic-apm-laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

shifudeen/elastic-apm-laravel

Composer 安装命令:

composer require shifudeen/elastic-apm-laravel

包简介

Laravel APM agent for Elastic v2 intake API

README 文档

README

CircleCI Latest Stable Version License

Elastic APM agent for v2 intake API. Compatible with Laravel 5.5+.

Transactions list Transaction detail

Installation

Require this package with composer:

composer require shifudeen/elastic-apm-laravel

Add the ServiceProvider class to the providers array in config/app.php:

'providers' => [
    // ... more providers
    \AG\ElasticApmLaravel\ServiceProvider::class,
],

From here, we will take care of everything based on your configuration. The agent and the middleware will be registered, and transactions will be sent to Elastic.

Agent configuration

The following environment variables are supported in the default configuration:

Variable Description
APM_ACTIVE true or false defaults to true. If false, the agent will collect, but not send, transaction data; span collection will also be disabled.
APM_ACTIVE_CLI true or false defaults to true. If false, the agent will not collect or send transaction or span data for non-HTTP requests but HTTP requests will still follow APM_ACTIVE. When APM_ACTIVE is false, this will have no effect.
APM_APPNAME Name of the app as it will appear in APM. Invalid special characters will be replaced with a hyphen.
APM_APPVERSION Version of the app as it will appear in APM.
APM_SERVERURL URL to the APM intake service.
APM_SECRETTOKEN Secret token, if required.
APM_USEROUTEURI true or false defaults to true. The default behavior is to record the URL as defined in your routes configuration. Set to false to record the requested URL, but keep in mind that this can result in excessive unique entries in APM.
APM_IGNORE_PATTERNS Ignore specific routes or jobs by transaction name. Should be a regular expression, and will match multiple patterns via pipe | in the regex. Note that 4 backslashes should be used to match a single backslash. Example: "/\/health-check|^OPTIONS |Foo\\\\Bar\\\\Job/"
APM_QUERYLOG true or false defaults to 'true'. Set to false to completely disable query logging, or to auto if you would like to use the threshold feature.
APM_THRESHOLD Query threshold in milliseconds, defaults to 200. If a query takes longer then 200ms, we enable the query log. Make sure you set APM_QUERYLOG=auto.
APM_BACKTRACEDEPTH Defaults to 25. Depth of backtrace in query span.
APM_MAXTRACEITEMS Defaults to 1000. Max number of child items displayed when viewing trace details.

You may also publish the elastic-apm-laravel.php configuration file to change additional settings:

php artisan vendor:publish --tag=config

Once published, open the config/elastic-apm-laravel.php file and review the various settings.

Collectors

The default collectors typically listen on events to measure portions of the request such as framework loading, database queries, or jobs.

The SpanCollector in particular allows you to measure any section of your own code via the ApmCollector Facade:

use AG\ElasticApmLaravel\Facades\ApmCollector;

ApmCollector::startMeasure('my-custom-span', 'custom', 'measure', 'My custom span');

// do something amazing

ApmCollector::stopMeasure('my-custom-span');

To record an additional span around your job execution, you may include the provided job middleware (Laravel 6+ only https://laravel.com/docs/6.x/queues#job-middleware):

public function middleware()
{
    return [
        app(\AG\ElasticApmLaravel\Jobs\Middleware\RecordTransaction::class),
    ];
}

Add a collector for other events

You can add extra collector(s) to listen to your own application events or Laravel events like Illuminate\Mail\Events\MessageSending for example. We created a base collector that already includes functionality to measure events, that you can extend from:

// app/Collectors/MailMessageCollector.php

namespace YourApp\Collectors;

use AG\ElasticApmLaravel\Contracts\DataCollector;
use AG\ElasticApmLaravel\Collectors\EventDataCollector;

use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;

class MailMessageCollector extends EventDataCollector implements DataCollector
{
    public function getName(): string
    {
        return 'mail-message-collector';
    }

    protected function registerEventListeners(): void
    {
        $this->app->events->listen(MessageSending::class, function (\Swift_Message $message) {
            $this->startMeasure(
                'mail #' . $message->getId(),
                'mail.delivery',
            );
        });

        $this->app->events->listen(MessageSent::class, function (\Swift_Message $message) {
            $this->stopMeasure('mail #' . $message->getId());
        });
    }
}

Don't forget to register your collector when the application starts:

// app/Providers/AppServiceProvider.php

use AG\ElasticApmLaravel\Facades\ApmCollector;

use YourApp\Collectors\MailMessageCollector;

public function boot()
{
    // ...
    ApmCollector::addCollector(MailMessageCollector::class);
}

Development

Get Composer. Follow the instructions defined on the official Composer page, or if you are using homebrew, just run:

brew install composer

Install project dependencies:

composer install

Run the unit test suite:

php vendor/bin/codecept run unit

Please adhere to PSR-2 and Symfony coding standard. Run the following commands before pushing your code:

php ./vendor/bin/php-cs-fixer fix --config .php_cs

shifudeen/elastic-apm-laravel 适用场景与选型建议

shifudeen/elastic-apm-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 04 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-23