Skip to content

Commit

Permalink
Merge branch 'feature/metric-adjustments' into feature/metric
Browse files Browse the repository at this point in the history
  • Loading branch information
tuj committed Aug 28, 2024
2 parents 1b79eb4 + 470e49c commit 92bec54
Show file tree
Hide file tree
Showing 26 changed files with 217 additions and 128 deletions.
2 changes: 1 addition & 1 deletion config/packages/itkdev_metrics.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
itkdev_metrics:
# Prefix exported metrics (should be application name)
namespace: BookAarhus
namespace: bookaarhus

# Storage adapter to use
adapter:
Expand Down
13 changes: 7 additions & 6 deletions src/Controller/CreateBookingWebformSubmitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public function __construct(

public function __invoke(Request $request): Response
{
$this->logger->info('CreateBookingWebformSubmitController invoked.');
$this->metric->counter('invoke', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$user = $this->getUser();
if ($user instanceof ApiKeyUser) {
Expand All @@ -41,19 +40,19 @@ public function __invoke(Request $request): Response
$apiKeyUserId = $userId ?? $user?->getUserIdentifier();

if (null === $webformId) {
$this->metric->counter('badRequestError', 'Webform submission data not valid.', $this);
$this->metric->incExceptionTotal(BadRequestException::class);
throw new BadRequestException('data->webform->id should not be null');
}
if (null === $submissionUuid) {
$this->metric->counter('badRequestError', 'Webform submission data not valid.', $this);
$this->metric->incExceptionTotal(BadRequestException::class);
throw new BadRequestException('data->submission->uuid should not be null');
}
if (null === $sender) {
$this->metric->counter('badRequestError', 'Webform submission data not valid.', $this);
$this->metric->incExceptionTotal(BadRequestException::class);
throw new BadRequestException('links->sender should not be null');
}
if (null === $getSubmissionUrl) {
$this->metric->counter('badRequestError', 'Webform submission data not valid.', $this);
$this->metric->incExceptionTotal(BadRequestException::class);
throw new BadRequestException('links->get_submission_url should not be null');
}

Expand All @@ -68,6 +67,8 @@ public function __invoke(Request $request): Response
$apiKeyUserId ?? '',
));

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);

return new Response(null, 201);
}
}
4 changes: 3 additions & 1 deletion src/Controller/GetAllResourcesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(

public function __invoke(Request $request): Response
{
$this->metric->counter('invoke', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$userPermissionHeader = $request->headers->get('Authorization-UserPermission');
$whitelistKey = $request->query->get('whitelistKey');
Expand All @@ -39,6 +39,8 @@ public function __invoke(Request $request): Response
$resources = array_merge($resources, $whitelistedResources);
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);

return new JsonResponse($resources, 200);
}
}
10 changes: 6 additions & 4 deletions src/Controller/GetResourceByEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Serializer\SerializerInterface;

#[AsController]
Expand All @@ -23,17 +23,19 @@ public function __construct(

public function __invoke(Request $request, string $resourceMail): Response
{
$this->metric->counter('invoke', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$resource = $this->aakResourceRepository->findOneByEmail($resourceMail);

if (is_null($resource)) {
$this->metric->counter('resourceNotFound', 'Resource not found', $this);
throw new HttpException(404, 'Resource not found');
$this->metric->incExceptionTotal(NotFoundHttpException::class);
throw new NotFoundHttpException('Resource not found');
}

$data = $this->serializer->serialize($resource, 'json', ['groups' => 'resource']);

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);

return new Response($data, 200);
}
}
8 changes: 5 additions & 3 deletions src/Controller/GetStatusByIdsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function __construct(

public function __invoke(Request $request): JsonResponse
{
$this->metric->counter('invoke', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$exchangeIds = json_decode($request->getContent())->ids;

if (empty($exchangeIds)) {
$this->metric->counter('resourceNotFound', 'Resource not found', $this);
$this->metric->incExceptionTotal(NotFoundHttpException::class);
throw new NotFoundHttpException('Resource not found');
}

Expand All @@ -56,7 +56,7 @@ public function __invoke(Request $request): JsonResponse
$this->userBookingCacheService->changeCacheEntry($id, ['status' => $userBooking->status]);
}
} catch (\Exception) {
$this->metric->counter('ignoredException', null, $this);
$this->metric->incExceptionTotal(\Exception::class);

$statuses[] = [
'exchangeId' => $id,
Expand All @@ -65,6 +65,8 @@ public function __invoke(Request $request): JsonResponse
}
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);

return new JsonResponse($statuses);
}
}
8 changes: 6 additions & 2 deletions src/DataPersister/UserBookingDataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function supports($data, array $context = []): bool

public function remove($data, array $context = []): void
{
$this->metric->counter('remove', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

try {
if ($data instanceof UserBooking) {
Expand All @@ -57,11 +57,13 @@ public function remove($data, array $context = []): void
} catch (MicrosoftGraphCommunicationException|UserBookingException $e) {
throw new HttpException($e->getCode(), 'Booking could not be deleted.');
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}

public function persist($data, array $context = []): mixed
{
$this->metric->counter('persist', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

try {
if ($data instanceof UserBooking) {
Expand All @@ -85,6 +87,8 @@ public function persist($data, array $context = []): mixed
));
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);

return $data;
} catch (MicrosoftGraphCommunicationException|UserBookingException $e) {
throw new HttpException($e->getCode(), 'Booking could not be updated.');
Expand Down
4 changes: 3 additions & 1 deletion src/DataProvider/BusyIntervalCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function supports(string $resourceClass, string $operationName = null, ar
*/
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
{
$this->metric->counter('getCollection', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

if (!isset($context['filters'])) {
throw new BadRequestHttpException('Required filters not set.');
Expand Down Expand Up @@ -69,5 +69,7 @@ public function getCollection(string $resourceClass, string $operationName = nul
yield $busyInterval;
}
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
4 changes: 3 additions & 1 deletion src/DataProvider/LocationCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function supports(string $resourceClass, string $operationName = null, ar

public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
{
$this->metric->counter('getCollection', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$whitelistKey = $context['filters']['whitelistKey'] ?? null;

Expand All @@ -35,5 +35,7 @@ public function getCollection(string $resourceClass, string $operationName = nul

yield $location;
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
4 changes: 3 additions & 1 deletion src/DataProvider/UserBookingCollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function supports(string $resourceClass, string $operationName = null, ar
*/
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
{
$this->metric->counter('getCollection', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$request = $this->requestStack->getCurrentRequest();

Expand Down Expand Up @@ -83,6 +83,8 @@ public function getCollection(string $resourceClass, string $operationName = nul
$obj = new \ArrayObject($userBookings);
$it = $obj->getIterator();

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);

return new TraversablePaginator($it, $page, $responseData['pageSize'], $responseData['total']);
}
}
4 changes: 3 additions & 1 deletion src/DataProvider/UserBookingItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function supports(string $resourceClass, string $operationName = null, ar
*/
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): UserBooking|null
{
$this->metric->counter('getItem', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

if (!isset($id) || !is_string($id)) {
throw new BadRequestHttpException('Required booking id is not set');
Expand All @@ -45,6 +45,8 @@ public function getItem(string $resourceClass, $id, string $operationName = null
throw new AccessDeniedHttpException('Access denied');
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);

return $userBooking;
}
}
10 changes: 3 additions & 7 deletions src/EventListener/FailedMessageEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ public function __construct(

public function __invoke(WorkerMessageFailedEvent $event): void
{
$this->metric->counter('invoke', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$envelope = $event->getEnvelope();
$message = $envelope->getMessage();

if ($message instanceof WebformSubmitMessage) {
$this->metric->counter('webformSubmitMessageError', 'Failed to extract data from webform.', $this);

$webformId = $message->getWebformId();
$throwable = $event->getThrowable();

Expand All @@ -44,8 +42,6 @@ public function __invoke(WorkerMessageFailedEvent $event): void
null
);
} elseif ($message instanceof CreateBookingMessage) {
$this->metric->counter('createBookingMessageError', 'Failed to create the booking in Exchange.', $this);

$booking = $message->getBooking();
$resource = $this->AAKResourceRepository->findOneByEmail($booking->getResourceEmail());

Expand All @@ -58,8 +54,6 @@ public function __invoke(WorkerMessageFailedEvent $event): void
$resource
);
} elseif ($message instanceof SendBookingNotificationMessage) {
$this->metric->counter('sendBookingNotificationError', 'Failed to send notification message to user.', $this);

$booking = $message->getBooking();
$resource = $this->AAKResourceRepository->findOneByEmail($booking->getResourceEmail());

Expand All @@ -70,5 +64,7 @@ public function __invoke(WorkerMessageFailedEvent $event): void
$resource
);
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
12 changes: 5 additions & 7 deletions src/MessageHandler/AddBookingToCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class AddBookingToCacheHandler
public function __construct(
private readonly BookingServiceInterface $bookingService,
private readonly UserBookingCacheServiceInterface $userBookingCacheService,
private readonly LoggerInterface $logger,
private readonly AAKResourceRepository $resourceRepository,
private readonly Metric $metric,
) {
Expand All @@ -33,8 +32,7 @@ public function __construct(
*/
public function __invoke(AddBookingToCacheMessage $message): void
{
$this->logger->info('AddBookingToCacheHandler invoked.');
$this->metric->counter('invoke', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$id = $this->bookingService->getBookingIdFromICalUid($message->getICalUID()) ?? null;

Expand All @@ -61,12 +59,12 @@ public function __invoke(AddBookingToCacheMessage $message): void
'resourceMail' => $booking->getResourceEmail(),
'resourceDisplayName' => $resourceDisplayName,
]);

$this->metric->counter('cacheEntryAdded', 'Cache entry added.', $this);
} else {
$this->metric->counter('recoverableErrorBookingIdNotFound', 'Booking id could not be retrieved for booking with iCalUID.', $this);
$this->metric->counter('generalRecoverableMessageHandlingException');
$this->metric->incExceptionTotal(RecoverableMessageHandlingException::class);

throw new RecoverableMessageHandlingException(sprintf('Booking id could not be retrieved for booking with iCalUID: %s', $message->getICalUID()));
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
19 changes: 8 additions & 11 deletions src/MessageHandler/CreateBookingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ public function __construct(
*/
public function __invoke(CreateBookingMessage $message): void
{
$this->logger->info('CreateBookingHandler invoked.');
$this->metric->counter('invoke', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$booking = $message->getBooking();

if (!$this->security->isGranted(BookingVoter::CREATE, $booking)) {
$this->logger->error('User does not have permission to create bookings for the given resource.');
$this->metric->incExceptionTotal(UnrecoverableMessageHandlingException::class);

$this->metric->counter('generalUnrecoverableMessageHandlingException');
$this->metric->counter('forbidden', 'User does not have permission to create bookings for the given resource.', $this);
throw new UnrecoverableMessageHandlingException('User does not have permission to create bookings for the given resource.', 403);
}

Expand All @@ -60,9 +58,8 @@ public function __invoke(CreateBookingMessage $message): void

if (null == $resource) {
$this->logger->error("Resource $email not found.");
$this->metric->incExceptionTotal(UnrecoverableMessageHandlingException::class);

$this->metric->counter('generalUnrecoverableMessageHandlingException');
$this->metric->counter('resourceNotFound', 'Resource not found.', $this);
throw new UnrecoverableMessageHandlingException("Resource $email not found.", 404);
}

Expand Down Expand Up @@ -126,14 +123,13 @@ public function __invoke(CreateBookingMessage $message): void
$response['iCalUId'],
));
} else {
$this->metric->counter('icaluidNotFound', 'Booking iCalUID could not be retrieved for booking with subject.', $this);
$this->logger->error(sprintf('Booking iCalUID could not be retrieved for booking with subject: %s', $booking->getSubject()));
$this->metric->incMethodTotal(__METHOD__, 'icaluid_not_found');
}
} catch (BookingCreateConflictException $exception) {
// If it is a BookingCreateConflictException the booking should be rejected.
$this->logger->notice(sprintf('Booking conflict detected: %d %s', $exception->getCode(), $exception->getMessage()));
$this->metric->counter('generalBookingCreateConflictException');
$this->metric->counter('bookingConflictDetected', 'Booking conflict detected.', $this);
$this->metric->incMethodTotal(__METHOD__, 'booking_conflict_detected');

$this->bus->dispatch(new SendBookingNotificationMessage(
$booking,
Expand All @@ -143,10 +139,11 @@ public function __invoke(CreateBookingMessage $message): void
// Other exceptions should logged, then re-thrown for the message to be re-queued.
$this->logger->error(sprintf('CreateBookingHandler exception: %d %s', $exception->getCode(), $exception->getMessage()));

$this->metric->counter('generalException');
$this->metric->counter('unexpectedException', null, $this);
$this->metric->incExceptionTotal(\Exception::class);

throw $exception;
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
6 changes: 4 additions & 2 deletions src/MessageHandler/RemoveBookingFromCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public function __construct(
*/
public function __invoke(RemoveBookingFromCacheMessage $message): void
{
$this->metric->counter('invoke', null, $this);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$this->userBookingCacheService->deleteCacheEntry($message->getExchangeId());
$this->metric->counter('cacheEntryDeleted', 'Cache entry has been deleted.', $this);

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
Loading

0 comments on commit 92bec54

Please sign in to comment.