getsentry / sentry-symfony

The official Symfony SDK for Sentry (sentry.io)
https://sentry.io
MIT License
695 stars 171 forks source link

Scope accumulates irrelevant extras in long-running php processes #676

Closed murat11 closed 1 year ago

murat11 commented 1 year ago

Environment

monolog:
    handlers:
        sentry:
            type: service
            id: Sentry\Monolog\Handler

services:
    Sentry\Monolog\Handler:
        arguments:
            $hub: '@Sentry\State\HubInterface'
            $level: !php/const Monolog\Logger::ERROR

Steps to Reproduce

We are running http api using symfony in roadrunner runtime and capturing errors with Sentry using the config like shown above. It worked well enough. With one issue: if during the processing of "current" http-request we add something into the Scope - it stays visible for future request, in particular: extras added during the handling of "previous" http-requests are visible in the data of event captured in "current" request. We did something like this in our code

\Sentry\configureScope(function (Scope $scope) use ($extras, $requestTime): void {
        $key = 'http.'.(new \DateTime())->format('H:i:s(v)').'.request';
        $scope->setExtra($key, [
            'data' => $extras,
            'time' => $requestTime,
        ]);
})

and noticed that extras are accumulated between handling of many requests which leads to inconsistent sentry data and memory leaks in case if $key is unique for each event.

I saw that for SubRequestListener everything is accumulated inside "dedicated" scope which is erased using Hub::popScope on "kernel.finish_request". But this listeners are applied only to non-master requests and in our case they do not do their job because roadrunner handles all requests using MAIN_REQUEST request type (not SUB_REQUEST). You can see it here:

                $sfRequest = $this->httpFoundationFactory->createRequest($request);
                $sfResponse = $this->kernel->handle($sfRequest);

So I'm a bit unsure how to proceed with this issue. I can register my own listeners which will do "pushScope/popScope" for master requests too, but I'm also curious why it's not done in your library or may be I must do anything else to make it work as expected in case with roadrunner

cleptric commented 1 year ago

Thanks for the very detailed issue 🙏

We do not support long-running setups like Open Swoole or Roadrunner in this bundle right now. Registering your own listeners would be the best option for you.

murat11 commented 1 year ago

thanks for response, @cleptric

cleptric commented 1 year ago

I would be curious though if there is something similar to Laravel Octance in Symfony that we could be looking into adding support for.

murat11 commented 1 year ago

I would be curious though if there is something similar to Laravel Octance in Symfony that we could be looking into adding support for.

I believe roadrunner-symfony-nyholm is something like that, isn't it?

cleptric commented 1 year ago

I was looking for something more "official" with a bigger user base 🙂

murat11 commented 1 year ago

I think it covers 99% of Symfony+Roadrunner users