Test Failed
Pull Request — master (#816)
by butschster
06:13 queued 32s
created

LogTracer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 1
b 0
f 0
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A trace() 0 28 1
A __construct() 0 7 1
A getContext() 0 3 1
A withContext() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Telemetry;
6
7
use Psr\Log\LoggerInterface;
8
use Spiral\Core\ContainerScope;
9
use Spiral\Core\InvokerInterface;
10
use Spiral\Core\ScopeInterface;
11
use Spiral\Logger\LogsInterface;
12
13
final class LogTracer implements TracerInterface
14
{
15
    private readonly LoggerInterface $logger;
16
17
    public function __construct(
18
        private readonly ScopeInterface $scope,
19
        private readonly ClockInterface $clock,
20
        LogsInterface $logs,
21
        string $channel = 'telemetry'
22
    ) {
23
        $this->logger = $logs->getLogger($channel);
0 ignored issues
show
Bug introduced by
The property logger is declared read-only in Spiral\Telemetry\LogTracer.
Loading history...
24
    }
25
26
    public function trace(
27
        string $name,
28
        callable $callback,
29
        array $attributes = [],
30
        bool $scoped = false,
31
        bool $debug = false,
32
        ?TraceKind $traceKind = null,
33
        ?int $startTime = null
34
    ): mixed {
35
        $span = new Span($name);
36
37
        $startTime ??= $this->clock->now();
38
39
        $result = $this->scope->runScope([
40
            SpanInterface::class => $span,
41
        ], static fn (): mixed => ContainerScope::getContainer()->get(InvokerInterface::class)->invoke($callback));
42
43
        $elapsed = $this->clock->now() - $startTime;
44
45
        $this->logger->debug(\sprintf('Trace [%s] - [%01.4f ms.]', $name, $elapsed / 1_000_000_000), [
0 ignored issues
show
Bug introduced by
The constant Spiral\Telemetry\1_000_000_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
46
            'attributes' => $span->getAttributes(),
47
            'status' => $span->getStatus(),
48
            'scoped' => $scoped,
49
            'trace_kind' => $traceKind,
50
            'elapsed' => $elapsed,
51
        ]);
52
53
        return $result;
54
    }
55
56
    public function withContext(?array $context): self
57
    {
58
        return $this;
59
    }
60
61
    public function getContext(): ?array
62
    {
63
        return null;
64
    }
65
}
66