Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.10% covered (warning)
87.10%
27 / 31
80.00% covered (warning)
80.00%
8 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Span
87.10% covered (warning)
87.10%
27 / 31
80.00% covered (warning)
80.00%
8 / 10
12.31
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __destruct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getContext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAttributes
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setSpanKind
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setSpanStatus
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 start
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 end
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 activate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 deactivate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Wikimedia\Telemetry;
3
4use 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 */
12class 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 setSpanStatus( int $status ): SpanInterface {
57        $this->context->setSpanStatus( $status );
58        return $this;
59    }
60
61    /** @inheritDoc */
62    public function start( ?int $epochNanos = null ): SpanInterface {
63        Assert::precondition(
64            !$this->context->wasStarted(),
65            'Cannot start a span more than once'
66        );
67
68        $this->context->setStartEpochNanos( $epochNanos ?? $this->clock->getCurrentNanoTime() );
69
70        return $this;
71    }
72
73    /** @inheritDoc */
74    public function end( ?int $epochNanos = null ): void {
75        Assert::precondition(
76            $this->context->wasStarted(),
77            'Cannot end a span that has not been started'
78        );
79
80        // Make duplicate end() calls a no-op, since it may occur legitimately,
81        // e.g. when a span wrapped in an RAII ScopedSpan wrapper is ended explicitly.
82        if ( !$this->context->wasEnded() ) {
83            $this->context->setEndEpochNanos( $epochNanos ?? $this->clock->getCurrentNanoTime() );
84            $this->tracerState->addSpanContext( $this->context );
85        }
86    }
87
88    /** @inheritDoc */
89    public function activate(): SpanInterface {
90        $this->tracerState->activateSpan( $this->getContext() );
91        return $this;
92    }
93
94    /** @inheritDoc */
95    public function deactivate(): SpanInterface {
96        $this->tracerState->deactivateSpan( $this->getContext() );
97        return $this;
98    }
99
100}