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\Attribute\OptionsInterface; |
21
|
|
|
use FRZB\Component\MetricsPower\Helper\ClassHelper; |
22
|
|
|
use FRZB\Component\MetricsPower\Logger\Data\Context; |
23
|
|
|
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; |
24
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
25
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @implements ContextExtractorInterface<SendMessageToTransportsEvent, OptionsInterface> |
29
|
|
|
*/ |
30
|
|
|
#[AsService, AsTagged(ContextExtractorInterface::class)] |
31
|
|
|
class SendMessageToTransportsEventContextExtractor implements ContextExtractorInterface |
32
|
|
|
{ |
33
|
|
|
private const MESSAGE = '[MetricsPower] [INFO] [MESSAGE: Sent to transports {transport_name}] [OPTIONS_CLASS: {options_class}] [TARGET_CLASS: {target_class}] [MESSAGE_CLASS: {message_class}]'; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
private readonly SerializerInterface $serializer, |
37
|
|
|
) {} |
38
|
|
|
|
39
|
|
|
public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context |
40
|
|
|
{ |
41
|
|
|
$context = [ |
42
|
|
|
'target_class' => ClassHelper::getShortName($target), |
43
|
|
|
'message_class' => ClassHelper::getShortName($target->getEnvelope()->getMessage()), |
44
|
|
|
'transport_name' => implode(',', $target->getSenders()), |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
if ($options?->isSerializable()) { |
48
|
|
|
$context += ['message_values' => $this->serializer->serialize($target->getEnvelope()->getMessage(), JsonEncoder::FORMAT)]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($options) { |
52
|
|
|
$context += [ |
53
|
|
|
'options_class' => ClassHelper::getShortName($options), |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new Context(self::MESSAGE, $context); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function getType(): string |
61
|
|
|
{ |
62
|
|
|
return SendMessageToTransportsEvent::class; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|