Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ codecov.yml export-ignore
phpstan.neon export-ignore
phpstan-baseline.neon export-ignore
warden.toml export-ignore
AGENTS.md export-ignore
CLAUDE.md export-ignore
CONTRIBUTING.md export-ignore
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,32 @@ placeholders are sent with the interpolated message.
\Sentry\getIsolationScope()->setTag('feature', 'checkout');
```

## 5.17.0

The Sentry SDK team is happy to announce the immediate availability of Sentry Symfony SDK v5.17.0.

### Features

- Resolve the user lazily for logs and metrics when a lazy firewall is used. [(#1143)](https://github.com/getsentry/sentry-symfony/pull/1143)

### Bug fixes

- Fix the session being started on every request to read the user when a lazy firewall is used, which locked the session until the response was sent. The user is now read when an event is captured. [(#1129)](https://github.com/getsentry/sentry-symfony/pull/1129)
- Fix the scope of the parent request being popped when a sub-request fails before its own scope is pushed, e.g. if no route matches. [(#1126)](https://github.com/getsentry/sentry-symfony/pull/1126)
- Fix the transaction being finished in the middle of the request when a sub-request fails before its span is started, e.g. if no route matches. [(#1125)](https://github.com/getsentry/sentry-symfony/pull/1125)

### Misc

- Bump `sentry-php` version to `4.38.0`. [(#1143)](https://github.com/getsentry/sentry-symfony/pull/1143)

## 5.16.1

The Sentry SDK team is happy to announce the immediate availability of Sentry Symfony SDK v5.16.1.

### Misc

- Exclude `AGENTS.md`, `CLAUDE.md` and `CONTRIBUTING.md` from the distributed package. [(#1140)](https://github.com/getsentry/sentry-symfony/pull/1140)

## 5.16.0

The Sentry SDK team is happy to announce the immediate availability of Sentry Symfony SDK v5.16.0.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": "^7.2||^8.0",
"composer-runtime-api": "^2.0",
"guzzlehttp/psr7": "^2.1.1|^3.0",
"sentry/sentry": "^5.0.0-beta1",
"sentry/sentry": "^5.0.0-beta2",
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
Expand Down
7 changes: 1 addition & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,6 @@ parameters:
count: 1
path: src/EventListener/SubRequestListener.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriverConnectionForV4\\:\\:exec\\(\\) should return int\\|numeric\\-string but returns int\\|string\\.$#"
count: 1
path: src/Tracing/Doctrine/DBAL/TracingDriverConnectionForV4.php

-
message: "#^Parameter \\#1 \\$sql of method Doctrine\\\\DBAL\\\\Driver\\\\Connection\\:\\:query\\(\\) expects string, string\\|null given\\.$#"
count: 1
Expand Down Expand Up @@ -257,7 +252,7 @@ parameters:

-
message: "#^Access to undefined constant Symfony\\\\Component\\\\HttpKernel\\\\HttpKernelInterface\\:\\:MASTER_REQUEST\\.$#"
count: 3
count: 4
path: tests/EventListener/LoginListenerTest.php

-
Expand Down
15 changes: 15 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,19 @@ public function getConfigTreeBuilder(): TreeBuilder
->max(1.0)
->info('The sampling factor to apply to profiles. A value of 0 will deny sending any profiles, and a value of 1 will send all profiles. Profiles are sampled in relation to traces_sample_rate')
)
->scalarNode('profiles_sampler')->end()
->append(
(new NullableFloatNodeDefinition('profile_session_sample_rate'))
->min(0.0)
->max(1.0)
->info('The sampling factor to apply to continuous profiling sessions. A value of 0 disables continuous profiling, and a value of 1 profiles every session.')
)
->enumNode('profile_lifecycle')
->values(['manual', 'trace'])
->info('Whether the continuous profiler is started and stopped manually ("manual") or runs while a sampled transaction is active ("trace").')
->end()
->variableNode('log_flush_threshold')->defaultNull()->end()
->variableNode('metric_flush_threshold')->defaultNull()->end()
->booleanNode('attach_stacktrace')->end()
->scalarNode('environment')
->cannotBeEmpty()
Expand Down Expand Up @@ -180,6 +192,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->integerNode('max_breadcrumbs')->min(0)->end()
->variableNode('max_breadth')->end()
->booleanNode('send_client_reports')->end()
->variableNode('before_breadcrumb')->end()
->arrayNode('in_app_exclude')
->scalarPrototype()->end()
Expand All @@ -204,6 +218,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('The maximum execution time for the request+response as a whole. It works only when using the default transport.')
->end()
->booleanNode('http_ssl_verify_peer')->end()
->booleanNode('http_ssl_native_ca')->end()
->booleanNode('http_compression')->end()
->booleanNode('capture_silenced_errors')->end()
->enumNode('max_request_body_size')
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ private function registerConfiguration(ContainerBuilder $container, array $confi
$options['traces_sampler'] = new Reference($options['traces_sampler']);
}

if (\is_array($options) && \is_string($options['profiles_sampler'] ?? null)) {
$options['profiles_sampler'] = new Reference($options['profiles_sampler']);
}

if (isset($options['before_send'])) {
$options['before_send'] = new Reference($options['before_send']);
}
Expand Down
89 changes: 89 additions & 0 deletions src/EventListener/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sentry\SentryBundle\EventListener;

use Sentry\Event;
use Sentry\SentryBundle\Sentry;
use Sentry\State\IsolationScope;
use Sentry\UserDataBag;
Expand Down Expand Up @@ -55,6 +56,43 @@ public function handleKernelRequestEvent(RequestEvent $event): void
return;
}

$request = $event->getRequest();

// with a lazy firewall, reading the token starts the session, which most
// handlers then lock for the rest of the request: when the request didn't
// start it, the token is read only once an event is captured
if ($request->hasPreviousSession() && !$request->getSession()->isStarted()) {
// the scope belongs to the runtime context of the request, which ends with it
$session = $request->getSession();

$this->sentry->configureScope(function (IsolationScope $scope) use ($session): void {
$scope->addEventProcessor(function (Event $event): Event {
try {
return $this->addUserToEvent($event);
} catch (\Throwable $exception) {
return $event;
}
});

$tokenRead = false;

// logs and metrics get the user once something else started the session
$scope->setUserResolver(function () use ($session, &$tokenRead): ?UserDataBag {
// the session is not started anymore once it is saved, but
// reading the token again does not need it
if (!$tokenRead && !$session->isStarted()) {
return null;
}

$tokenRead = true;

return $this->getUserFromToken();
});
});

return;
}

$token = $this->tokenStorage->getToken();

if (null !== $token) {
Expand Down Expand Up @@ -82,6 +120,57 @@ public function handleAuthenticationSuccessEvent(AuthenticationSuccessEvent $eve
$this->updateUserContext($event->getAuthenticationToken());
}

private function addUserToEvent(Event $event): Event
{
$user = $event->getUser();

if (null !== $user && null !== $user->getId()) {
return $event;
}

$tokenUser = $this->getUserFromToken();

if (null === $tokenUser) {
return $event;
}

// the user of the event can be the one of the scope, shared with the next events
$user = null !== $user ? clone $user : new UserDataBag();
$user->setId($tokenUser->getId());

foreach ($tokenUser->getMetadata() as $name => $value) {
$user->setMetadata($name, $value);
}

$event->setUser($user);

return $event;
}

private function getUserFromToken(): ?UserDataBag
{
if (null === $this->tokenStorage || !$this->sentry->getClient()->getOptions()->getDataCollection()->shouldCollectUserInfo()) {
return null;
}

$token = $this->tokenStorage->getToken();

if (null === $token || !$this->isTokenAuthenticated($token)) {
return null;
}

$user = new UserDataBag();
$user->setId($this->getUserIdentifier($token->getUser()));

$impersonatorUser = $this->getImpersonatorUser($token);

if (null !== $impersonatorUser) {
$user->setMetadata('impersonator_username', $impersonatorUser);
}

return $user;
}

private function updateUserContext(TokenInterface $token): void
{
if (!$this->isTokenAuthenticated($token)) {
Expand Down
17 changes: 17 additions & 0 deletions src/EventListener/SubRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sentry\SentryBundle\EventListener;

use Sentry\SentryBundle\Sentry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;

Expand All @@ -21,6 +22,11 @@ final class SubRequestListener
*/
private $sentry;

/**
* @var \SplObjectStorage<Request, true> The sub-requests a scope was pushed for
*/
private $requests;

/**
* Constructor.
*
Expand All @@ -29,6 +35,7 @@ final class SubRequestListener
public function __construct(Sentry $sentry)
{
$this->sentry = $sentry;
$this->requests = new \SplObjectStorage();
}

/**
Expand All @@ -44,6 +51,7 @@ public function handleKernelRequestEvent(RequestEvent $event): void
}

$this->sentry->pushScope();
$this->requests[$event->getRequest()] = true;
}

/**
Expand All @@ -58,6 +66,15 @@ public function handleKernelFinishRequestEvent(FinishRequestEvent $event): void
return;
}

$request = $event->getRequest();

// A sub-request can fail before its scope is pushed, e.g. if no route matches
if (!isset($this->requests[$request])) {
return;
}

unset($this->requests[$request]);

$this->sentry->popScope();
}
}
12 changes: 12 additions & 0 deletions src/EventListener/TracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public function handleKernelRequestEvent(RequestEvent $event): void
$this->sentry->setSpan($this->sentry->startTransaction($context));
}

/**
* The response of a sub-request belongs to the span of the sub-request, not to the transaction.
*/
public function handleKernelResponseEvent(ResponseEvent $event): void
{
if (!$this->isMainRequest($event)) {
return;
}

parent::handleKernelResponseEvent($event);
}

/**
* @param ResponseEvent $event
*/
Expand Down
Loading
Loading