Skip to content

Commit

Permalink
Log every time a session is created
Browse files Browse the repository at this point in the history
Prior to this change there we weren't able to keep track of sessions that got lost.

This change allows us to see every time a session is created and distinguish them by their correlation id.
  • Loading branch information
mharte-ib committed Sep 12, 2024
1 parent 384732b commit 234e0bf
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Service/SessionCorrelationIdService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Copyright 2024 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types = 1);

namespace Surfnet\Tiqr\Service;

use Symfony\Component\HttpFoundation\RequestStack;

final readonly class SessionCorrelationIdService
{
private const SALT = 'Mr6LpJYtuWRDdVR2_7VgTChFhzQ';

public function __construct(
private RequestStack $requestStack
) {
}

public function generateCorrelationId(): ?string
{
$sessionCookie = $this->requestStack->getMainRequest()?->cookies->get('PHPSESSID');

if ($sessionCookie === null) {
return null;
}

return hash('sha256', self::SALT . substr($sessionCookie, 10));
}
}
70 changes: 70 additions & 0 deletions src/Session/LoggingSessionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types = 1);

/**
* Copyright 2024 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\Tiqr\Session;

use Psr\Log\LoggerInterface;
use Surfnet\Tiqr\Service\SessionCorrelationIdService;
use Surfnet\Tiqr\WithContextLogger;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionFactory;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageFactoryInterface;

/**
* This class serves as a decorated version of Symfony's SessionFactory class.
* Every time a session is created, we'll add a log entry that can be identified by the correlation id.
*
* For this logging function to work we need a stateless logger, therefore we inject the default logger by using the variable name $monoLogger.
* The variable name $logger has been bound to the Surfnet GSSP logger in the Symfony container, so we'll need to use another variable name.
* The Surfnet GSSP Logger isn't stateless as it's dependent on the current session, which has not been created when the logging occurs.
*
* @see SessionFactory::createSession()
*/
#[AsDecorator('session.factory')]
final class LoggingSessionFactory extends SessionFactory
{
private LoggerInterface $logger;

public function __construct(
RequestStack $requestStack,
#[Autowire(service: 'session.storage.factory')]
SessionStorageFactoryInterface $storageFactory,
LoggerInterface $monologLogger,
SessionCorrelationIdService $sessionCorrelationIdService,
?callable $usageReporter = null,
) {
$this->logger = WithContextLogger::from(
$monologLogger,
['correlationId' => $sessionCorrelationIdService->generateCorrelationId() ?? ''],
);

parent::__construct($requestStack, $storageFactory, $usageReporter);
}

public function createSession(): SessionInterface
{
$this->logger->info('Created new session.');

return parent::createSession();
}
}

0 comments on commit 234e0bf

Please sign in to comment.