Skip to content

Commit

Permalink
Merge branch 'main' into eqs-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky authored Sep 26, 2024
2 parents 3edf04a + c2c9eb9 commit 3cf7c19
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^10.4",
"symfony/messenger": "^5.4 || ^6.0 || ^7.0"
"symfony/messenger": "^5.4 || ^6.0 || ^7.0",
"symfony/security-bundle": "^5.4 || ^6.0 || ^7.0"
},
"suggest": {
"ext-newrelic": "Install the New Relic PHP agent on your system"
"ext-newrelic": "Install the New Relic PHP agent on your system",
"symfony/security-bundle": "Track users impacted by transaction error"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Check24\NewRelicBundle\EventListener;

use Check24\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;

Expand All @@ -26,12 +27,18 @@
public function __construct(
private array $excludedExceptions,
private NewRelicInteractorInterface $interactor,
private ?Security $security = null,
) {
}

public function __invoke(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
// link users to transactions (anonymously), so that we can monitor # of users impacted by errors in error inbox
// see https://docs.newrelic.com/docs/errors-inbox/error-users-impacted/#attributes
if ($user = $this->security?->getUser()) {
$this->interactor->addCustomParameter('enduser.id', crc32($user->getUserIdentifier()));
}

foreach ($this->excludedExceptions as $excludedException) {
if ($exception instanceof $excludedException) {
Expand Down
7 changes: 7 additions & 0 deletions tests/App/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Check24\NewRelicBundle\Check24NewRelicBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
Expand All @@ -29,6 +30,7 @@ public function registerBundles(): iterable
new FrameworkBundle(),
new MonologBundle(),
new Check24NewRelicBundle(),
new SecurityBundle(),
];
}

Expand All @@ -46,6 +48,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
],
],
],
'router' => ['resource' => 'kernel::loadRoutes', 'type' => 'service', 'utf8' => true],
]);

$container->loadFromExtension('check24_new_relic', [
Expand Down Expand Up @@ -76,6 +79,10 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
],
],
]);
$container->loadFromExtension('security', [
'providers' => ['users_in_memory' => ['memory' => null]],
'firewalls' => ['main' => ['provider' => 'users_in_memory']],
]);
});
}

Expand Down
17 changes: 17 additions & 0 deletions tests/EventListener/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@
use Check24\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Tests\Check24\NewRelicBundle\DummyException;

class ExceptionListenerTest extends TestCase
{
private ExceptionListener $listener;
private NewRelicInteractorInterface&MockObject $interactor;
private Security&MockObject $security;

protected function setUp(): void
{
$this->listener = new ExceptionListener(
[DummyException::class],
$this->interactor = $this->createMock(NewRelicInteractorInterface::class),
$this->security = $this->createMock(Security::class),
);
}

Expand All @@ -51,6 +55,19 @@ public function testItSendErrorToNewRelic(): void
$this->listener->__invoke($event);
}

public function testItAttachesUserId(): void
{
$this->security->method('getUser')->willReturn(new InMemoryUser('[email protected]', null));
$this->interactor->expects(self::once())->method('addCustomParameter')->with('enduser.id', '3260418537');

$this->listener->__invoke(new ExceptionEvent(
$this->createMock(HttpKernelInterface::class),
$this->createMock(Request::class),
HttpKernelInterface::MAIN_REQUEST,
new \RuntimeException(),
));
}

public function testItIgnoreExcludedErrors(): void
{
$event = new ExceptionEvent(
Expand Down
2 changes: 2 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Tests\Check24\NewRelicBundle;

use Check24\NewRelicBundle\EventListener\ExceptionListener;
use Check24\NewRelicBundle\NewRelic\Config;
use Check24\NewRelicBundle\NewRelic\NewRelicInteractor;
use Check24\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
Expand All @@ -38,6 +39,7 @@ public function testKernelIsBootableAndServicesAreUsable(): void
self::assertInstanceOf(TraceUuidFactory::class, $container->get(TraceIdFactoryInterface::class));
self::assertInstanceOf(RouteNameStrategy::class, $container->get(RequestTransactionNameStrategyInterface::class));
self::assertInstanceOf(MessageNameStrategy::class, $container->get(MessengerTransactionNameStrategyInterface::class));
self::assertInstanceOf(ExceptionListener::class, $container->get(ExceptionListener::class));

$config = $container->get(Config::class);

Expand Down

0 comments on commit 3cf7c19

Please sign in to comment.