| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Spiral\Telemetry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Ramsey\Uuid\Uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Ramsey\Uuid\UuidFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Spiral\Core\ScopeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @internal The component is under development. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Something may be changed in the future. We will stable it soon. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Feedback is welcome {@link https://github.com/spiral/framework/discussions/822}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | final class LogTracer extends AbstractTracer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private array $context = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 2 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         ScopeInterface $scope, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         private readonly ClockInterface $clock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         private readonly LoggerInterface $logger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         private readonly UuidFactoryInterface $uuidFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 2 |  |         parent::__construct($scope); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 2 |  |     public function trace( | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         string $name, | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         callable $callback, | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         array $attributes = [], | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         bool $scoped = false, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         ?TraceKind $traceKind = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         ?int $startTime = null | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     ): mixed { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 2 |  |         $span = new Span($name, $attributes); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 2 |  |         $this->context['telemetry'] = $this->uuidFactory->uuid4()->toString(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 2 |  |         $startTime ??= $this->clock->now(); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 2 |  |         $result = $this->runScope($span, $callback); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 2 |  |         $elapsed = $this->clock->now() - $startTime; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 2 |  |         $this->logger->debug(\sprintf('Trace [%s] - [%01.4f ms.]', $name, $elapsed / 1_000_000), [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 2 |  |             'attributes' => $span->getAttributes(), | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 2 |  |             'status' => $span->getStatus(), | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 2 |  |             'scoped' => $scoped, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 2 |  |             'trace_kind' => $traceKind, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 2 |  |             'elapsed' => $elapsed, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 2 |  |             'id' => $this->context['telemetry'], | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 2 |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 2 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |     public function getContext(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         return $this->context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  |