Lines
0.00%
0 / 20
Functions and Methods
0.00%
0 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ActionCallTracer | 0.00% | 0 / 20 | 0.00% | 0 / 4 | 156 | 0.00% | 0 / 1 | |||
| __construct | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| traceCallSent | 0.00% | 0 / 9 | 0.00% | 0 / 1 | 20 | |||||
| traceCallAnswered | 0.00% | 0 / 9 | 0.00% | 0 / 1 | 42 | |||||
| describeCall | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Broker; | |
| 6 | ||
| 7 | use Psr\Log\LoggerInterface; | |
| 8 | use Stewart\Contracts\Exception\ActionCallException; | |
| 9 | use Stewart\Contracts\Time\Duration; | |
| 10 | use Stewart\Runtime\Ipc\Message\ActionCallRequest; | |
| 11 | use Stewart\Runtime\Logging\LogOrigin; | |
| 12 | use Stewart\Runtime\Logging\LogVerbosityPolicy; | |
| 13 | use Stewart\Runtime\Model\HaCallOutcome; | |
| 14 | use Throwable; | |
| 15 | ||
| 16 | final readonly class ActionCallTracer | |
| 17 | { | |
| 18 | public function __construct( | |
| 19 | private LoggerInterface $logger, | |
| 20 | private LogVerbosityPolicy $verbosity, | |
| 21 | ) {} | |
| 22 | ||
| 23 | public function traceCallSent(ActionCallRequest $request): void | |
| 24 | { | |
| 25 | if (!$this->verbosity->isTracing($request->scope)) { | |
| 26 | return; | |
| 27 | } | |
| 28 | ||
| 29 | $details = $this->describeCall($request); | |
| 30 | $target = $request->target?->toArray() ?? []; | |
| 31 | ||
| 32 | if ($target !== []) { | |
| 33 | $details['target'] = $target; | |
| 34 | } | |
| 35 | ||
| 36 | if ($request->data !== []) { | |
| 37 | $details['data'] = $request->data; | |
| 38 | } | |
| 39 | ||
| 40 | $this->logger->debug('Action call sent', [...$details, LogOrigin::CONTEXT_KEY => LogOrigin::forScope($request->scope, traced: true)]); | |
| 41 | } | |
| 42 | ||
| 43 | public function traceCallAnswered(ActionCallRequest $request, Duration $took, ?Throwable $failure = null): void | |
| 44 | { | |
| 45 | if (!$this->verbosity->isTracing($request->scope)) { | |
| 46 | return; | |
| 47 | } | |
| 48 | ||
| 49 | $outcome = match (true) { | |
| 50 | $failure === null => HaCallOutcome::Succeeded, | |
| 51 | $failure instanceof ActionCallException => HaCallOutcome::failedWith($failure->reason), | |
| 52 | default => HaCallOutcome::Unreachable, | |
| 53 | }; | |
| 54 | ||
| 55 | $details = [...$this->describeCall($request), 'outcome' => $outcome->value, 'took' => (string) $took]; | |
| 56 | ||
| 57 | if ($failure !== null) { | |
| 58 | $details['error'] = $failure->getMessage(); | |
| 59 | } | |
| 60 | ||
| 61 | $this->logger->debug('Action call answered', [...$details, LogOrigin::CONTEXT_KEY => LogOrigin::forScope($request->scope, traced: true)]); | |
| 62 | } | |
| 63 | ||
| 64 | /** @return array<string, mixed> */ | |
| 65 | private function describeCall(ActionCallRequest $request): array | |
| 66 | { | |
| 67 | return ['action' => $request->domain . '.' . $request->action]; | |
| 68 | } | |
| 69 | } |