5 Commits

Author SHA1 Message Date
6e5f929dc3 feat: retries of api requests in case of failure 2024-01-18 21:08:11 +01:00
7eac908bc4 feat: add retry client trait for api requests
feat: add retry client trait for api requests
2024-01-18 21:08:11 +01:00
d7ff806129 feat: add nubium/this-should-never-happen-exception 2024-01-18 20:50:37 +01:00
13bdd096be feat: add sentry
feat: add sentry
2024-01-18 20:50:37 +01:00
192cfb9b73 feat: add support for OTEL logs
feat: add support for OTEL logs
2024-01-18 18:59:31 +01:00
20 changed files with 1572 additions and 51 deletions

4
.env
View File

@@ -18,3 +18,7 @@
APP_ENV=dev
APP_SECRET=f27f6cb11fb594fcb5ab4591ec0aac77
###< symfony/framework-bundle ###
###> sentry/sentry-symfony ###
SENTRY_DSN=
###< sentry/sentry-symfony ###

View File

@@ -3,3 +3,4 @@ TODO:
- [ ] Chybí speciální slovník nebo vypnutí slovníku pro testy
- [ ] V reálný aplikaci bych použil Mockery, nicméně tady mě to přijde zbytečný
- [ ] Nastavení cache ideálně v memcached/redis etc.
- [ ] Vylepsit OTEL logs

View File

@@ -10,14 +10,17 @@
"ext-iconv": "*",
"grpc/grpc": "^1.57",
"guzzlehttp/promises": "*",
"nubium/this-should-never-happen-exception": "^1.0",
"nyholm/psr7": "*",
"open-telemetry/exporter-otlp": "^1.0",
"open-telemetry/opentelemetry-auto-symfony": "^1.0@beta",
"open-telemetry/opentelemetry-logger-monolog": "^1.0",
"open-telemetry/opentelemetry-propagation-server-timing": "^0.0.1",
"open-telemetry/opentelemetry-propagation-traceresponse": "^0.0.2",
"open-telemetry/sdk": "^1.0",
"open-telemetry/transport-grpc": "^1.0",
"php-http/httplug": "*",
"sentry/sentry-symfony": "^4.13",
"symfony/asset": "7.0.*",
"symfony/asset-mapper": "7.0.*",
"symfony/cache": "7.0.*",

1233
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,4 +6,5 @@ return [
Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Sentry\SentryBundle\SentryBundle::class => ['prod' => true],
];

View File

@@ -5,6 +5,10 @@ monolog:
when@dev:
monolog:
handlers:
otel: # Tohle bych v realny aplikaci nepouzil na devu
type: service
id: App\Bridge\Monolog\Handler\SymfonyOtelHandler
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
@@ -40,6 +44,9 @@ when@test:
when@prod:
monolog:
handlers:
otel:
type: service
id: App\Bridge\Monolog\Handler\SymfonyOtelHandler
main:
type: fingers_crossed
action_level: error

View File

@@ -0,0 +1,26 @@
when@prod:
sentry:
dsn: '%env(SENTRY_DSN)%'
# this hooks into critical paths of the framework (and vendors) to perform
# automatic instrumentation (there might be some performance penalty)
# https://docs.sentry.io/platforms/php/guides/symfony/performance/instrumentation/automatic-instrumentation/
tracing:
enabled: false
# If you are using Monolog, you also need this additional configuration to log the errors correctly:
# https://docs.sentry.io/platforms/php/guides/symfony/#monolog-integration
# register_error_listener: false
# register_error_handler: false
# monolog:
# handlers:
# sentry:
# type: sentry
# level: !php/const Monolog\Logger::ERROR
# hub_id: Sentry\State\HubInterface
# Uncomment these lines to register a log message processor that resolves PSR-3 placeholders
# https://docs.sentry.io/platforms/php/guides/symfony/#monolog-integration
# services:
# Monolog\Processor\PsrLogMessageProcessor:
# tags: { name: monolog.processor, handler: sentry }

View File

@@ -39,12 +39,20 @@ services:
arguments:
$username: '%app.usetreno.username%'
$password: '%app.usetreno.password%'
$retryCount: 2
$retryWaitSeconds: 0.5
App\Service\Remote\UsetrenoQRCodeProvider:
arguments:
$retryCount: 2
$retryWaitSeconds: 0.5
App\Service\CachedQRCodeGenerator:
arguments:
$innerGenerator: '@App\Service\Remote\UsetrenoQRCodeProvider'
$cacheDuration: 'PT60S'
App\Service\CurrencyListerInterface: '@App\Service\StaticCurrencyLister'
App\Service\QRCodeQROptionsProviderInterface: '@App\Service\QRCodeQROptionsDefaultProvider'
App\Service\QRCodeGeneratorInterface: '@App\Service\CachedQRCodeGenerator'

View File

@@ -11,22 +11,40 @@ processors:
actions:
- action: insert
key: loki.attribute.labels
value: log.file, log.line, log.module_path, http.request_id, http.uri
value: context, code.filepath, code.namespace, code.function, code.lineno, http.request.method, http.request.body.size, url.full, url.scheme, url.path, http.route, http.response.status_code
- action: insert
from_attribute: log.file
key: log.file
from_attribute: context
key: context
- action: insert
from_attribute: log.line
key: log.line
from_attribute: code.namespace
key: code.namespace
- action: insert
from_attribute: log.module_path
key: log.module_path
from_attribute: code.function
key: code.function
- action: insert
from_attribute: http.request_id
key: http.request_id
from_attribute: code.lineno
key: code.lineno
- action: insert
from_attribute: http.uri
key: http.uri
from_attribute: http.request.method
key: http.request.method
- action: insert
from_attribute: http.request.body.size
key: http.request.body.size
- action: insert
from_attribute: http.response.status_code
key: http.response.status_code
- action: insert
from_attribute: url.full
key: url.full
- action: insert
from_attribute: url.scheme
key: url.scheme
- action: insert
from_attribute: url.path
key: url.path
- action: insert
from_attribute: http.route
key: http.route
- action: insert
key: loki.format
value: raw

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Bridge\Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\FormattableHandlerTrait;
use Monolog\LogRecord;
use OpenTelemetry\API\Globals;
use OpenTelemetry\Contrib\Logs\Monolog\Handler;
use Psr\Log\LogLevel;
use Symfony\Bridge\Monolog\Formatter\VarDumperFormatter;
final class SymfonyOtelHandler extends AbstractHandler
{
protected readonly Handler $innerHandler;
public function __construct() {
parent::__construct();
$this->innerHandler = new Handler(
Globals::loggerProvider(),
LogLevel::INFO, //or `Logger::INFO`, or `Level::Info` depending on monolog version
true,
);
}
public function handle(LogRecord $record): bool
{
return $this->innerHandler->handle($record);
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Attribute\Route;
/**
* Used for testing error handling (sentry...)
*/
class ExceptionController extends AbstractController
{
#[Route("give-me-error-please/exception")]
public function makeException(): void {
throw new \InvalidArgumentException("There is exception");
}
}

View File

@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace App\Entity\Remote\Usetreno;

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace App\Service\Remote\Exception;
class UsetrenoQRCodeRemoteServerErrorException extends UsetrenoQRCodeException
{
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Service\Remote;
use Exception;
use Psr\Log\LoggerInterface;
trait RetryingFailClientTrait
{
/**
* Lepsi zopakovat request kvuli drobnemu vypadku site nebo sluzby (u nedestruktivni operace)
* nez hodit klientovi rovnou 500
*
* @param int $count
* @param float $sleep
* @param array<string> $catchableExceptions
* @param LoggerInterface $logger
* @param callable $callback
* @return mixed
* @throws Exception
*/
protected function retryingFailRequest(
int $count,
float $sleep,
array $catchableExceptions,
LoggerInterface $logger,
callable $callback
): mixed {
for ($i = 0; ; $i++) {
try {
return $callback();
} catch (Exception $e) {
foreach ($catchableExceptions as $exceptionClass) {
if ($e instanceof $exceptionClass) {
$logger->error("transport: fail request retrying... got catchable exception", [
'exception' => $e,
'try' => $i
]);
usleep((int) ($sleep * 1_000_000));
if ($i == $count) {
throw $e;
}
continue 2;
}
}
throw $e;
}
}
// phpstan fail
return null;
}
}

View File

@@ -5,7 +5,9 @@ namespace App\Service\Remote;
use App\Entity\Remote\Usetreno\AuthRequest;
use App\Service\Remote\Exception\AuthorizeException;
use Nubium\Exception\ThisShouldNeverHappenException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@@ -16,12 +18,16 @@ use Symfony\Contracts\HttpClient\ResponseStreamInterface;
class UsetrenoHttpClient implements HttpClientInterface
{
use RetryingFailClientTrait;
const AUTHORIZE_API = "https://topapi.top-test.cz/chameleon/api/v1/token";
protected ?string $authorizationToken = null;
public function __construct(protected HttpClientInterface $innerClient, protected readonly LoggerInterface $logger, protected readonly string $username,
protected readonly string $password) { }
protected readonly string $password,
protected readonly float $retryWaitSeconds,
protected readonly int $retryCount) { }
/**
* @param string $method
@@ -88,26 +94,35 @@ class UsetrenoHttpClient implements HttpClientInterface
"AUTHORIZE_API" => static::AUTHORIZE_API
]);
$rq = $this->innerClient->request(
"POST",
static::AUTHORIZE_API,
[
'json' => new AuthRequest($this->username, $this->password),
]
);
$responseData = $this->retryingFailRequest($this->retryCount, $this->retryWaitSeconds,
[AuthorizeException::class, TransportException::class], $this->logger, function() {
$rq = $this->innerClient->request(
"POST",
static::AUTHORIZE_API,
[
'json' => new AuthRequest($this->username, $this->password),
]
);
$statusCode = $rq->getStatusCode();
$responseData = $rq->getContent(false);
$statusCode = $rq->getStatusCode();
$responseData = $rq->getContent(false);
if ($statusCode != 200) {
$this->logger->error("authorization request status code is not ok", [
"AUTHORIZE_API" => static::AUTHORIZE_API,
"statusCode" => $statusCode,
"content" => $responseData,
"username" => $this->username
]);
if ($statusCode != 200) {
$this->logger->error("authorization request status code is not ok", [
"AUTHORIZE_API" => static::AUTHORIZE_API,
"statusCode" => $statusCode,
"content" => $responseData,
"username" => $this->username
]);
throw new AuthorizeException("Return code is not 200 OK (got: code: $statusCode)");
throw new AuthorizeException("Return code is not 200 OK (got: code: $statusCode)");
}
return $responseData;
});
if (!is_string($responseData)) {
throw new ThisShouldNeverHappenException("responseData is not a string");
}
$this->authorizationToken = $this->processAuthorizeResponse($responseData);

View File

@@ -7,16 +7,23 @@ use App\Entity\DTO\QRCode\QRCode;
use App\Service\CacheableQRCodeGeneratorInterface;
use App\Service\Remote\Edge\QRCodeEntityConverter;
use App\Service\Remote\Exception\UsetrenoQRCodeException;
use App\Service\Remote\Exception\UsetrenoQRCodeRemoteServerErrorException;
use Nubium\Exception\ThisShouldNeverHappenException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
{
use RetryingFailClientTrait;
const QRCODE_API = 'https://topapi.top-test.cz/chameleon/api/v1/qr-code/create-for-bank-account-payment';
const QRCODE_METHOD = 'POST';
public function __construct(protected readonly LoggerInterface $logger,
protected readonly UsetrenoHttpClient $client,
protected readonly QRCodeEntityConverter $codeEntityConverter) { }
protected readonly QRCodeEntityConverter $codeEntityConverter,
protected readonly float $retryWaitSeconds,
protected readonly int $retryCount) { }
public function generateQRCodeFromEntity(QRCode $entity): string
{
@@ -27,27 +34,41 @@ class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
"edgeEntity" => $edgeEntity
]);
$response = $this->client->request(static::QRCODE_METHOD, static::QRCODE_API, [
'json' => $edgeEntity,
]);
$responseData = $this->retryingFailRequest($this->retryCount, $this->retryWaitSeconds,
[TransportException::class, UsetrenoQRCodeRemoteServerErrorException::class], $this->logger,
function() use ($edgeEntity) {
$response = $this->client->request(static::QRCODE_METHOD, static::QRCODE_API, [
'json' => $edgeEntity,
]);
$statusCode = $response->getStatusCode();
$responseData = $response->getContent(false);
$statusCode = $response->getStatusCode();
$responseData = $response->getContent(false);
if ($statusCode != 200) {
$this->logger->error("qrcode request status code is not ok", [
"AUTHORIZE_API" => static::QRCODE_API,
"statusCode" => $statusCode,
"content" => $responseData,
]);
if ($statusCode != 200) {
$this->logger->error("qrcode request status code is not ok", [
"AUTHORIZE_API" => static::QRCODE_API,
"statusCode" => $statusCode,
"content" => $responseData,
]);
throw new UsetrenoQRCodeException("Return code is not 200 OK (got: code: $statusCode)");
if ($statusCode > 500) {
throw new UsetrenoQRCodeRemoteServerErrorException("Return code is not 200 OK (got: code: $statusCode)");
}
throw new UsetrenoQRCodeException("Return code is not 200 OK (got: code: $statusCode)");
}
$this->logger->debug("QRCode generation success", [
"responseContent" => $responseData,
]);
return $responseData;
});
if (!is_string($responseData)) {
throw new ThisShouldNeverHappenException("responseData is not a string");
}
$this->logger->debug("QRCode generation success", [
"responseContent" => $responseData,
]);
return $this->parseBase64String($this->processQRCodeResponseEntity($responseData));
}

View File

@@ -49,6 +49,18 @@
"tests/bootstrap.php"
]
},
"sentry/sentry-symfony": {
"version": "4.13",
"recipe": {
"repo": "github.com/symfony/recipes-contrib",
"branch": "main",
"version": "4.6",
"ref": "153de5f041f7e8a9c19f3674b800b76be0e6fd90"
},
"files": [
"config/packages/sentry.yaml"
]
},
"symfony/asset-mapper": {
"version": "7.0",
"recipe": {

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Tests\Service\Remote;
use App\Service\Remote\RetryingFailClientTrait;
use App\Tests\Common\LoggerTrait;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class RetryingFailClientTraitTest extends TestCase {
use LoggerTrait;
private int $callCount = 0;
protected function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->callCount = 0;
}
public function testSuccess() {
$trait = new class {
use RetryingFailClientTrait {
retryingFailRequest as public; // make the method public
}
};
$result = $trait->retryingFailRequest(2, 0, [\RuntimeException::class], $this->getLogger(), function () {
$this->callCount = $this->callCount + 1;
return 'foo';
});
$this->assertEquals(1, $this->callCount);
$this->assertEquals('foo', $result);
}
public function testRetyingFail() {
$trait = new class {
use RetryingFailClientTrait {
retryingFailRequest as public; // make the method public
}
};
try {
$trait->retryingFailRequest(2, 0, [\RuntimeException::class], $this->getLogger(), function () {
$this->callCount = $this->callCount + 1;
throw new \RuntimeException("test");
});
} catch (\RuntimeException) {
// do nothing
}
$this->assertEquals(3, $this->callCount);
}
}

View File

@@ -24,7 +24,7 @@ class UsetrenoHttpClientTest extends TestCase
]);
$authorizedRequestResponse = clone $authMockResponse;
$mockedClient = new MockHttpClient([$authMockResponse, $authorizedRequestResponse]);
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar");
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar", 0, 0);
$client->request("POST", "https://www.root.cz/");
$this->assertEquals("https://topapi.top-test.cz/chameleon/api/v1/token", $authMockResponse->getRequestUrl());
$headers = $authorizedRequestResponse->getRequestOptions()['headers'];
@@ -48,7 +48,7 @@ class UsetrenoHttpClientTest extends TestCase
$authorizedRequestResponse = clone $authMockResponse;
$mockedClient = new MockHttpClient([$authMockResponse, $authorizedRequestResponse]);
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar");
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar", 0, 0);
$client->request("POST", "https://www.root.cz/");
}
}

View File

@@ -75,7 +75,7 @@ class UsetrenoQRCodeProviderTest extends TestCase
->method('convert')
->will($this->returnValue($edgeEntity));
return new UsetrenoQRCodeProvider($this->getLogger(), $mock, $converterMock);
return new UsetrenoQRCodeProvider($this->getLogger(), $mock, $converterMock, 0, 0);
}
protected function createQRCodeEntityPair() {