| 1 |
<?php |
| 2 |
namespace Wikimedia\Telemetry; |
| 3 |
|
| 4 |
use Wikimedia\Assert\Assert; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class Tracer implements TracerInterface { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
private const TRACE_ID_BYTES_LENGTH = 16; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private const SPAN_ID_BYTES_LENGTH = 8; |
| 20 |
|
| 21 |
private Clock $clock; |
| 22 |
private SamplerInterface $sampler; |
| 23 |
private ExporterInterface $exporter; |
| 24 |
private TracerState $tracerState; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private bool $wasShutdown = false; |
| 31 |
|
| 32 |
public function __construct( |
| 33 |
Clock $clock, |
| 34 |
SamplerInterface $sampler, |
| 35 |
ExporterInterface $exporter, |
| 36 |
TracerState $tracerState |
| 37 |
) { |
| 38 |
$this->clock = $clock; |
| 39 |
$this->sampler = $sampler; |
| 40 |
$this->exporter = $exporter; |
| 41 |
$this->tracerState = $tracerState; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
public function createSpan( string $spanName ): SpanInterface { |
| 46 |
$activeSpanContext = $this->tracerState->getActiveSpanContext(); |
| 47 |
|
| 48 |
|
| 49 |
if ( !$this->wasShutdown ) { |
| 50 |
Assert::precondition( |
| 51 |
$activeSpanContext !== null, |
| 52 |
'Attempted to create a span with the currently active span as the implicit parent, ' . |
| 53 |
'but no span was active. Use createRootSpan() to create a span with no parent (i.e. a root span).' |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
return $this->newSpan( $spanName, $activeSpanContext ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
public function createRootSpan( string $spanName ): SpanInterface { |
| 62 |
return $this->newSpan( $spanName, null ); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public function createSpanWithParent( string $spanName, SpanContext $parentSpanContext ): SpanInterface { |
| 67 |
return $this->newSpan( $spanName, $parentSpanContext ); |
| 68 |
} |
| 69 |
|
| 70 |
private function newSpan( string $spanName, ?SpanContext $parentSpanContext ): SpanInterface { |
| 71 |
$traceId = $parentSpanContext !== null ? |
| 72 |
$parentSpanContext->getTraceId() : $this->generateId( self::TRACE_ID_BYTES_LENGTH ); |
| 73 |
$spanId = $this->generateId( self::SPAN_ID_BYTES_LENGTH ); |
| 74 |
$sampled = $this->sampler->shouldSample( $parentSpanContext ); |
| 75 |
|
| 76 |
$spanContext = new SpanContext( |
| 77 |
$traceId, |
| 78 |
$spanId, |
| 79 |
$parentSpanContext !== null ? $parentSpanContext->getSpanId() : null, |
| 80 |
$spanName, |
| 81 |
$sampled |
| 82 |
); |
| 83 |
|
| 84 |
if ( $this->wasShutdown || !$sampled ) { |
| 85 |
return new NoopSpan( $spanContext ); |
| 86 |
} |
| 87 |
|
| 88 |
return new Span( |
| 89 |
$this->clock, |
| 90 |
$this->tracerState, |
| 91 |
$spanContext |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public function shutdown(): void { |
| 97 |
$this->wasShutdown = true; |
| 98 |
$this->exporter->export( $this->tracerState ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private function generateId( int $bytesLength ): string { |
| 108 |
return bin2hex( random_bytes( $bytesLength ) ); |
| 109 |
} |
| 110 |
} |