Passed
Pull Request — master (#816)
by butschster
06:49
created

LogTracer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 15
dl 0
loc 42
rs 10
c 1
b 0
f 0
ccs 11
cts 13
cp 0.8462

3 Methods

Rating   Name   Duplication   Size   Complexity  
A trace() 0 28 1
A __construct() 0 5 1
A getContext() 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\Container;
9
use Spiral\Core\InvokerInterface;
10
use Spiral\Core\ScopeInterface;
11
12
final class LogTracer implements TracerInterface
13
{
14 2
    public function __construct(
15
        private readonly ScopeInterface $scope,
16
        private readonly ClockInterface $clock,
17
        private readonly LoggerInterface $logger
18
    ) {
19
    }
20
21 2
    public function trace(
22
        string $name,
23
        callable $callback,
24
        array $attributes = [],
25
        bool $scoped = false,
26
        bool $debug = false,
27
        ?TraceKind $traceKind = null,
28
        ?int $startTime = null
29
    ): mixed {
30 2
        $span = new Span($name);
31
32 2
        $startTime ??= $this->clock->now();
33
34 2
        $result = $this->scope->runScope([
35
            SpanInterface::class => $span,
36 2
        ], static fn (InvokerInterface $invoker): mixed => $invoker->invoke($callback));
37
38 2
        $elapsed = $this->clock->now() - $startTime;
39
40 2
        $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...
41 2
            'attributes' => $span->getAttributes(),
42 2
            'status' => $span->getStatus(),
43
            'scoped' => $scoped,
44
            'trace_kind' => $traceKind,
45
            'elapsed' => $elapsed,
46
        ]);
47
48 2
        return $result;
49
    }
50
51
    public function getContext(): array
52
    {
53
        return [];
54
    }
55
}
56