cakephp-biztech/cake-sentry

CakePHP3的Sentry插件

3.0.0 2020-05-21 07:00 UTC

This package is auto-updated.

Last update: 2024-09-11 12:49:59 UTC


README

CakePHP的Sentry集成。

Latest Stable Version Total Downloads Build Status codecov License

要求

  • PHP 7.1+
  • CakePHP 3.6+
  • Sentry账户

安装

使用composer安装。

composer require cakephp-biztech/cake-sentry

使用方法

设置配置文件。

写入您的Sentry账户信息。

// in `config/app.php`
return [
    'Sentry' => [
        'dsn' => YOUR_SENTRY_DSN_HERE
    ]
];

加载插件。

在Application.php中

public function bootstrap()
{
    parent::bootstrap();

    $this->addPlugin(\Biztech\CakeSentry\Plugin::class);
}

或使用cake命令。

bin/cake plugin load Biztech/CakeSentry --bootstrap

这就完了!🎉

高级使用

忽略噪音异常

您可以过滤掉引起骚动且难以确定要解决的问题的异常(如PageNotFoundException),设置不要在Error.skipLog中记录异常。

示例)

// in `config/app.php`
'Error' => [
    'skipLog' => [
        NotFoundException::class,
        MissingRouteException::class,
        MissingControllerException::class,
    ],
]

参考:CakePHP菜谱 https://book.cakephp.com.cn/3.0/en/development/errors.html#error-exception-configuration

设置选项

所有配置都写入Configure::write('Sentry'),并将传递给Sentry\init()。请参阅Sentry的官方文档关于配置关于php-sdk的配置

此外,如果需要,CakeSentry还提供事件钩子,可以更容易地设置动态值。

示例)

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;

class SentryOptionsContext implements EventListenerInterface
{
    public function implementedEvents()
    {
        return [
            'CakeSentry.Client.afterSetup' => 'setServerContext',
        ];
    }

    public function setServerContext(Event $event)
    {
        /** @var Client $subject */
        $subject = $event->getSubject();
        $options = $subject->getHub()->getClient()->getOptions();

        $options->setEnvironment('test_app');
        $options->setRelease('2.0.0@dev');
    }
}

config/bootstrap.php

EventManager::instance()->on(new SentryOptionsContext());

发送更多上下文

在发送错误到Sentry之前,客户端会触发CakeSentry.Client.beforeCapture事件。您可以使用EventListener和外观sentryConfigureScope()等设置上下文,或者使用$event->getContext()->getHub()来访问和设置上下文。调用Raven_Client的API或返回值,将发送错误上下文。现在,cake-sentry支持通过$event->getSubject()->getRequest()在实现的事件中获取Request实例。另请参阅官方文档中关于上下文的章节

示例)

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
use Sentry\State\Scope;

use function Sentry\configureScope as sentryConfigureScope;

class SentryErrorContext implements EventListenerInterface
{
    public function implementedEvents()
    {
        return [
            'CakeSentry.Client.beforeCapture' => 'setContext',
        ];
    }

    public function setContext(Event $event)
    {
        if (PHP_SAPI !== 'cli') {
            /** @var ServerRequest $request */
            $request = $event->getData('request') ?? ServerRequestFactory::fromGlobals();
            $request->trustProxy = true;

            sentryConfigureScope(function (Scope $scope) use ($request, $event) {
                $scope->setTag('app_version',  $request->getHeaderLine('App-Version') ?: 1.0);
                $exception = $event->getData('exception');
                if ($exception) {
                    assert($exception instanceof \Exception);
                    $scope->setTag('status', $exception->getCode());
                }
                $scope->setUser(['ip_address' => $request->clientIp()]);
                $scope->setExtras([
                    'foo' => 'bar',
                    'request attributes' => $request->getAttributes(),
                ]);
            });
        }
    }
}

config/bootstrap.php

EventManager::instance()->on(new SentryErrorContext());

收集用户反馈

CakeSentry.Client.afterCapture事件中,您可以获取最后一个事件ID。另请参阅官方文档

示例)

class SentryErrorContext implements EventListenerInterface
{
    public function implementedEvents()
    {
        return [
            'CakeSentry.Client.afterCapture' => 'callbackAfterCapture',
        ];
    }

    public function callbackAfterCapture(Event $event)
    {
        $lastEventId = $event->getData('lastEventId');
    }
}

贡献

在GitHub上欢迎pull requests和反馈 :) https://github.com/ishan-biztech/cake-sentry

许可证

该插件以开源形式提供,受MIT许可证条款的约束。