Skip to content

Commit

Permalink
1834: Fixed method counts
Browse files Browse the repository at this point in the history
  • Loading branch information
tuj committed Aug 28, 2024
1 parent 7022ea7 commit 470e49c
Show file tree
Hide file tree
Showing 24 changed files with 154 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/Controller/CreateBookingWebformSubmitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(

public function __invoke(Request $request): Response
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$user = $this->getUser();
if ($user instanceof ApiKeyUser) {
Expand Down Expand Up @@ -67,7 +67,7 @@ public function __invoke(Request $request): Response
$apiKeyUserId ?? '',
));

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

return new Response(null, 201);
}
Expand Down
4 changes: 2 additions & 2 deletions 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->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

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

return new JsonResponse($resources, 200);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/GetResourceByEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(

public function __invoke(Request $request, string $resourceMail): Response
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

Expand All @@ -34,7 +34,7 @@ public function __invoke(Request $request, string $resourceMail): Response

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

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

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

public function __invoke(Request $request): JsonResponse
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

Expand Down Expand Up @@ -65,7 +65,7 @@ public function __invoke(Request $request): JsonResponse
}
}

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

return new JsonResponse($statuses);
}
Expand Down
8 changes: 5 additions & 3 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->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$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->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

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

return $data;
} catch (MicrosoftGraphCommunicationException|UserBookingException $e) {
Expand Down
4 changes: 2 additions & 2 deletions 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->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::COMPLETE);
$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
4 changes: 2 additions & 2 deletions 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->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

Expand All @@ -36,6 +36,6 @@ public function getCollection(string $resourceClass, string $operationName = nul
yield $location;
}

$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::COMPLETE);
$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
4 changes: 2 additions & 2 deletions 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->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

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

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

return new TraversablePaginator($it, $page, $responseData['pageSize'], $responseData['total']);
}
Expand Down
4 changes: 2 additions & 2 deletions 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->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

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

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

public function __invoke(WorkerMessageFailedEvent $event): void
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$envelope = $event->getEnvelope();
$message = $envelope->getMessage();
Expand Down Expand Up @@ -64,5 +64,7 @@ public function __invoke(WorkerMessageFailedEvent $event): void
$resource
);
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
4 changes: 2 additions & 2 deletions src/MessageHandler/AddBookingToCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
*/
public function __invoke(AddBookingToCacheMessage $message): void
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

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

Expand Down Expand Up @@ -65,6 +65,6 @@ public function __invoke(AddBookingToCacheMessage $message): void
throw new RecoverableMessageHandlingException(sprintf('Booking id could not be retrieved for booking with iCalUID: %s', $message->getICalUID()));
}

$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::COMPLETE);
$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
8 changes: 5 additions & 3 deletions src/MessageHandler/CreateBookingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
*/
public function __invoke(CreateBookingMessage $message): void
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$booking = $message->getBooking();

Expand Down Expand Up @@ -123,13 +123,13 @@ public function __invoke(CreateBookingMessage $message): void
$response['iCalUId'],
));
} else {
$this->metric->incFunctionTotal($this, __FUNCTION__, 'icaluid_not_found');
$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->incFunctionTotal($this, __FUNCTION__, 'booking_conflict_detected');
$this->metric->incMethodTotal(__METHOD__, 'booking_conflict_detected');

$this->bus->dispatch(new SendBookingNotificationMessage(
$booking,
Expand All @@ -143,5 +143,7 @@ public function __invoke(CreateBookingMessage $message): void

throw $exception;
}

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

$this->userBookingCacheService->deleteCacheEntry($message->getExchangeId());
$this->metric->totalIncByOne('cache_entry_deleted', 'Cache entry has been deleted.', $this, ['complete']);

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
4 changes: 3 additions & 1 deletion src/MessageHandler/SendBookingNotificationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
*/
public function __invoke(SendBookingNotificationMessage $message): void
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

try {
$this->logger->info('SendBookingNotificationHandler.');
Expand All @@ -54,5 +54,7 @@ public function __invoke(SendBookingNotificationMessage $message): void

throw $e;
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
4 changes: 3 additions & 1 deletion src/MessageHandler/SendUserBookingNotificationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
*/
public function __invoke(SendUserBookingNotificationMessage $message): void
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

try {
$this->logger->info('SendBookingNotificationHandler.');
Expand All @@ -54,5 +54,7 @@ public function __invoke(SendUserBookingNotificationMessage $message): void

throw $e;
}

$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
4 changes: 2 additions & 2 deletions src/MessageHandler/UpdateBookingInCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public function __construct(
*/
public function __invoke(UpdateBookingInCacheMessage $message): void
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

$this->userBookingCacheService->changeCacheEntry($message->getExchangeId(), $message->getChanges());

$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::COMPLETE);
$this->metric->incMethodTotal(__METHOD__, Metric::COMPLETE);
}
}
6 changes: 4 additions & 2 deletions src/MessageHandler/WebformSubmitHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public function __construct(

public function __invoke(WebformSubmitMessage $message): void
{
$this->metric->incFunctionTotal($this, __FUNCTION__, Metric::INVOKE);
$this->metric->incMethodTotal(__METHOD__, Metric::INVOKE);

try {
$dataSubmission = $this->webformService->getData($message);
} catch (WebformSubmissionRetrievalException $e) {
if (403 == $e->getCode()) {
$this->metric->incFunctionTotal($this, __FUNCTION__, 'forbidden');
$this->metric->incMethodTotal(__METHOD__, 'forbidden');
}

// TODO: Handle other request actions as a retryable exception.
Expand Down Expand Up @@ -110,6 +110,8 @@ public function __invoke(WebformSubmitMessage $message): void

throw new UnrecoverableMessageHandlingException($e->getMessage());
}

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

/**
Expand Down
13 changes: 7 additions & 6 deletions src/Service/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ public function totalIncByOne(string $name, ?string $description = null, $invoki
$this->metricsService->counter($prefix.$name.'_total', $description ?? '', 1, $labels);
}

public function incFunctionTotal($invokingInstance, string $functionName, string $action): void
public function incMethodTotal(string $method, string $action): void
{
$classShortName = (new \ReflectionClass($invokingInstance))->getShortName();
$className = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $classShortName));
$functionName = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $functionName));
$metrixMethod = preg_replace('/^.*\\\s*/', '', $method);
$metrixMethod = str_replace('::', '__', $metrixMethod);
$metrixMethod = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $metrixMethod));

$this->metricsService->counter('function_total', 'Function totals.', 1, [$className.'__'.$functionName => $action]);
$this->metricsService->counter('method_'.$metrixMethod.'_total', 'Method '.$method.' totals.', 1, ['action' => $action]);
}

public function incExceptionTotal($exceptionClassName): void
{
$exceptionClassName = preg_replace('/^.*\\\s*/', '', $exceptionClassName);
$exceptionClassName = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $exceptionClassName));
$this->metricsService->counter('exception', 'Exception totals.', 1, [self::EXCEPTION => $exceptionClassName]);
$this->metricsService->counter('exception_total', 'Exception totals.', 1, [self::EXCEPTION => $exceptionClassName]);
}
}
Loading

0 comments on commit 470e49c

Please sign in to comment.