Skip to content

Commit

Permalink
Add ApiCallMonitor for managing ApiCallInterface monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-ducrot committed Mar 27, 2024
1 parent 8989304 commit 8c8bb36
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ services:
- setEntityManager: [ '@Doctrine\ORM\EntityManagerInterface' ]
tags: [ 'controller.service_arguments' ]
# Monitoring
Smart\CoreBundle\Monitoring\ApiCallMonitor:
arguments:
- '@Smart\CoreBundle\Monitoring\ProcessMonitor'
Smart\CoreBundle\Monitoring\ProcessMonitor:
arguments:
- '@Doctrine\ORM\EntityManagerInterface'
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/ProcessInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

interface ProcessInterface
{
public const DATA_EXCEPTION_TRACE = 'exception_trace';

public function isOngoing(): bool;

public function isSuccess(): bool;
Expand Down
34 changes: 34 additions & 0 deletions src/Monitoring/ApiCallMonitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Smart\CoreBundle\Monitoring;

use Smart\CoreBundle\Entity\ApiCallInterface;
use Smart\CoreBundle\Entity\ProcessInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class ApiCallMonitor
{
public function __construct(private readonly ProcessMonitor $processMonitor)
{
}

public function start(ApiCallInterface $apiCall, Request $request): ProcessInterface
{
$this->processMonitor->start($apiCall);
$apiCall->setMethod($request->getMethod());
$apiCall->setRouteUrl($request->getUri());
$apiCall->setType($request->attributes->get('_route'));
$apiCall->setInputData($request->request->all());

return $apiCall;
}

public function end(ApiCallInterface $apiCall, int $statusCode, bool $flush = true): void
{
$apiCall->setStatusCode($statusCode);
$this->processMonitor->end($apiCall, $statusCode >= 200 && $statusCode < 300, $flush);
}
}

0 comments on commit 8c8bb36

Please sign in to comment.