Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.19% |
23 / 27 |
|
77.78% |
7 / 9 |
CRAP | |
0.00% |
0 / 1 |
Span | |
85.19% |
23 / 27 |
|
77.78% |
7 / 9 |
11.39 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
__destruct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getContext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setAttributes | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setSpanKind | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
start | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
end | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
activate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deactivate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Wikimedia\Telemetry; |
3 | |
4 | use Wikimedia\Assert\Assert; |
5 | |
6 | /** |
7 | * Represents an OpenTelemetry span, i.e. a single operation within a trace. |
8 | * |
9 | * @since 1.43 |
10 | * @see https://opentelemetry.io/docs/specs/otel/trace/api/#span |
11 | */ |
12 | class Span implements SpanInterface { |
13 | |
14 | private Clock $clock; |
15 | |
16 | private TracerState $tracerState; |
17 | |
18 | private SpanContext $context; |
19 | |
20 | public function __construct( |
21 | Clock $clock, |
22 | TracerState $tracerState, |
23 | SpanContext $context |
24 | ) { |
25 | $this->clock = $clock; |
26 | $this->tracerState = $tracerState; |
27 | $this->context = $context; |
28 | } |
29 | |
30 | public function __destruct() { |
31 | $this->end(); |
32 | $activeSpanContext = $this->tracerState->getActiveSpanContext(); |
33 | if ( $this->context->equals( $activeSpanContext ) ) { |
34 | $this->deactivate(); |
35 | } |
36 | } |
37 | |
38 | /** @inheritDoc */ |
39 | public function getContext(): SpanContext { |
40 | return $this->context; |
41 | } |
42 | |
43 | /** @inheritDoc */ |
44 | public function setAttributes( array $attributes ): SpanInterface { |
45 | $this->context->setAttributes( $attributes ); |
46 | return $this; |
47 | } |
48 | |
49 | /** @inheritDoc */ |
50 | public function setSpanKind( int $spanKind ): SpanInterface { |
51 | $this->context->setSpanKind( $spanKind ); |
52 | return $this; |
53 | } |
54 | |
55 | /** @inheritDoc */ |
56 | public function start( ?int $epochNanos = null ): SpanInterface { |
57 | Assert::precondition( |
58 | !$this->context->wasStarted(), |
59 | 'Cannot start a span more than once' |
60 | ); |
61 | |
62 | $this->context->setStartEpochNanos( $epochNanos ?? $this->clock->getCurrentNanoTime() ); |
63 | |
64 | return $this; |
65 | } |
66 | |
67 | /** @inheritDoc */ |
68 | public function end( ?int $epochNanos = null ): void { |
69 | Assert::precondition( |
70 | $this->context->wasStarted(), |
71 | 'Cannot end a span that has not been started' |
72 | ); |
73 | |
74 | // Make duplicate end() calls a no-op, since it may occur legitimately, |
75 | // e.g. when a span wrapped in an RAII ScopedSpan wrapper is ended explicitly. |
76 | if ( !$this->context->wasEnded() ) { |
77 | $this->context->setEndEpochNanos( $epochNanos ?? $this->clock->getCurrentNanoTime() ); |
78 | $this->tracerState->addSpanContext( $this->context ); |
79 | } |
80 | } |
81 | |
82 | /** @inheritDoc */ |
83 | public function activate(): void { |
84 | $this->tracerState->activateSpan( $this->getContext() ); |
85 | } |
86 | |
87 | /** @inheritDoc */ |
88 | public function deactivate(): void { |
89 | $this->tracerState->deactivateSpan( $this->getContext() ); |
90 | } |
91 | |
92 | } |