如何将所有例外情况的用户添加到Symfony4中的Sentry?

时间:2018-12-14 12:36:12

标签: symfony4 sentry

我已将Sentry添加到我的项目中,但没有为某些事件添加当前登录的用户。

我添加了一个事件订阅者,但是我不确定是否真的需要它。对于像ArgumentCountError这样的例外,一切正常。对于NotFoundHttpException,不是。经过一些调试后,我发现并不是每个异常都调用ExceptionListener-> onKernelRequest。我已经在sentry.yaml中清空了skip_capture,但这没有帮助。 我想念什么。我的猜测是传播将在某处停止。但是哪里?我该如何更改呢?

<?php
// src/EventSubscriber/SentrySubscriber

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Sentry\SentryBundle\SentrySymfonyEvents;
use Symfony\Component\HttpKernel\KernelEvents;

class SentrySubscriber implements EventSubscriberInterface {

    /** @var \Raven_Client */
    protected $client;
    private $username;

    public function __construct(\Raven_Client $client) {
        $this->client = $client;
    }

    public static function getSubscribedEvents() {
        return array(
            SentrySymfonyEvents::PRE_CAPTURE => 'preCapture',
            SentrySymfonyEvents::SET_USER_CONTEXT => 'setUserContext',
        );
    }

    public function preCapture(GetResponseForExceptionEvent $event, $eventName, EventDispatcher $dispatcher) {
// username is empty, because setUserContext wasn't executed
// echo 'preCapture';var_dump($this->username);var_dump($this->client->_user);
        $this->client->user_context(['username' => $this->username]);
    }

    public function setUserContext(\Sentry\SentryBundle\Event\SentryUserContextEvent $event) {
//      echo 'setUserContext';      var_dump($event->getAuthenticationToken()->getUsername());
        $this->username = $event->getAuthenticationToken()->getUsername();
    }

}

我的配置:

// config/services.yaml
services:
    App\EventSubscriber\SentrySubscriber:
        arguments:
          - '@sentry.client'
        tags:
          - { name: kernel.event_subscriber }

// config/packages/sentry.yaml
sentry:
    dsn: '%env(SENTRY_DSN)%'
    options:
        curl_method: async
        release: '%env(RELEASE)%'

    skip_capture: # nothing else here, because everything should be captured

1 个答案:

答案 0 :(得分:0)

默认情况下,Sentry不会发送用户数据,以防止GDPR和类似法规出现问题。

如果要重新启用发送用户数据,请将其添加到sentry.yaml

sentry:
    options:
        send_default_pii: true

官方文档here

检查是在\Sentry\SentryBundle\EventListener\RequestListener::onKernelRequest中实现的。