Lines
0.00%
0 / 20
Methods
0.00%
0 / 4
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __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 | ||
| 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 | } |