Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startSession immediately triggers deliverSessions #666

Open
alcohol opened this issue Aug 9, 2023 · 1 comment
Open

startSession immediately triggers deliverSessions #666

alcohol opened this issue Aug 9, 2023 · 1 comment
Labels
backlog We hope to fix this feature/bug in the future

Comments

@alcohol
Copy link

alcohol commented Aug 9, 2023

Perhaps the behaviour I am about to describe is intentional, in which case this would be more of a feature (change) request than a bug report.

We currently use a Symfony EventSubscriber to automatically track sessions; see code:

<?php declare(strict_types=1);

namespace Vendor\Events\EventSubscriber;

use Bugsnag\Client;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class BugsnagSubscriber implements EventSubscriberInterface
{
    public function __construct(
        #[Autowire(value: '%kernel.environment%')]
        private readonly string $kernelEnvironment,
        private readonly Client $bugsnag
    ) {
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        if ('docker' === $this->kernelEnvironment || ! $event->isMainRequest()) {
            return;
        }

        $this->bugsnag->startSession();
    }

    public function onKernelTerminate(TerminateEvent $event): void
    {
        if ('docker' === $this->kernelEnvironment || ! $event->isMainRequest()) {
            return;
        }

        $this->bugsnag->getSessionTracker()->sendSessions();
    }

    /** @return array<string, array{string, int}|array{string, int}[]|string|string[][]> */
    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::REQUEST => ['onKernelRequest', 256],
            KernelEvents::TERMINATE => ['onKernelTerminate', 256],
        ];
    }
}

Recently our application suddenly became much slower on our production environment. Doing some quick profiling revealed that an http call to sessions.bugsnag.com was the root cause of this slowdown (the call took over 6 seconds on average).

Note: this was due to networking issues at digital ocean, not bugsnag itself.

I found this peculiar at first since we call deliverSessions in a kernel.terminate event in Symfony, which in theory means that I would have expected the call to sessions.bugsnag.com to happen after we had already delivered a response to the client.

Doing some quick reviewing of the code however, I learned that simply calling startSession already immediately triggers deliverSessions due to:

startSession calling incrementSessions

$this->incrementSessions($currentTime);

This $lastSent variable defaulting to 0

$lastSent = $this->getLastSent();

Meaning this conditional is always true

if ($deliver && ((time() - $lastSent) > self::$DELIVERY_INTERVAL)) {

Is this expected / intentional behaviour?

@johnkiely1
Copy link
Member

Hi @alcohol. This is something that we are aware of as an area of potential improvement. It is on our backlog to address as soon as priorities allow. We will post here as soon as we have any updates.

@johnkiely1 johnkiely1 added the backlog We hope to fix this feature/bug in the future label Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog We hope to fix this feature/bug in the future
Projects
None yet
Development

No branches or pull requests

2 participants