Skip to content

Commit

Permalink
Add ApiCallMonitor for ApiCallInterface monitoring + ProcessInterface…
Browse files Browse the repository at this point in the history
…::addExceptionTraceData
  • Loading branch information
mathieu-ducrot committed Apr 2, 2024
1 parent 0b8dc24 commit 3e3fc5b
Show file tree
Hide file tree
Showing 4 changed files with 47 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 @@ -53,4 +53,6 @@ public function getData(): ?array;
public function setData(?array $data): static;

public function addData(string $key, mixed $value): void;

public function addExceptionTraceData(\Exception $e): void;
}
8 changes: 8 additions & 0 deletions src/Entity/ProcessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,12 @@ public function addData(string $key, mixed $value): void
{
$this->data[$key] = $value;
}

/**
* Use this if you wish to store the exception trace on the internal data of the process
*/
public function addExceptionTraceData(\Exception $e): void
{
$this->data['exception_trace'] = $e->getTrace();
}
}
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): ApiCallInterface
{
$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 3e3fc5b

Please sign in to comment.