|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace FRZB\Component\MetricsPower\Logger\ContextExtractor; |
|
17
|
|
|
|
|
18
|
|
|
use FRZB\Component\DependencyInjection\Attribute\AsService; |
|
19
|
|
|
use FRZB\Component\DependencyInjection\Attribute\AsTagged; |
|
20
|
|
|
use FRZB\Component\MetricsPower\Helper\ClassHelper; |
|
21
|
|
|
use FRZB\Component\MetricsPower\Helper\MetricalHelper; |
|
22
|
|
|
use FRZB\Component\MetricsPower\Logger\Data\LoggerContext; |
|
23
|
|
|
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; |
|
24
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
|
25
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
26
|
|
|
|
|
27
|
|
|
#[AsService, AsTagged(ContextExtractorInterface::class)] |
|
28
|
|
|
final readonly class WorkerMessageFailedEventContextExtractor implements ContextExtractorInterface |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
private const MESSAGE = '[MetricsPower] [ERROR] [MESSAGE: Operation failed] [TARGET_CLASS: {target_class}] [EXCEPTION_CLASS: {exception_class}] [EXCEPTION_MESSAGE: {exception_message}]'; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
private SerializerInterface $serializer, |
|
34
|
|
|
) {} |
|
35
|
|
|
|
|
36
|
|
|
public function extract(WorkerMessageFailedEvent $target): LoggerContext |
|
37
|
|
|
{ |
|
38
|
|
|
$exception = $target->getThrowable(); |
|
39
|
|
|
$message = $target->getEnvelope()->getMessage(); |
|
40
|
|
|
$context = [ |
|
41
|
|
|
'target_class' => ClassHelper::getShortName($target), |
|
42
|
|
|
'target_values' => $this->serializer->serialize($target, JsonEncoder::FORMAT), |
|
43
|
|
|
'message_class' => ClassHelper::getShortName($message), |
|
44
|
|
|
'message_values' => $this->serializer->serialize($message, JsonEncoder::FORMAT), |
|
45
|
|
|
'exception_class' => ClassHelper::getShortName($exception), |
|
46
|
|
|
'exception_message' => $exception->getMessage(), |
|
47
|
|
|
'exception_trace' => $exception->getTrace(), |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
if (($options = MetricalHelper::getFirstOptions($message)) && $options->isSerializable()) { |
|
51
|
|
|
$context += [ |
|
52
|
|
|
'options_class' => ClassHelper::getShortName($options), |
|
53
|
|
|
'options_values' => $this->serializer->serialize($options, JsonEncoder::FORMAT), |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return new LoggerContext(self::MESSAGE, $context); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public static function getType(): string |
|
61
|
|
|
{ |
|
62
|
|
|
return WorkerMessageFailedEvent::class; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|