Debian Codegraph @

includes/libs/telemetry/TracerState.php

raw symbols SCIP
1 <?php
2 namespace Wikimedia\Telemetry;
3
4 use Wikimedia\Assert\Assert;
5
6 /**
7 * Holds shared telemetry state, such as finished span data buffered for export.
8 *
9 * Since this data is tied to the lifetime of a given web request or process, this class is a singleton to
10 * avoid discarding data in the case of MediaWiki service container resets.
11 *
12 * @since 1.43
13 * @internal
14 */
15 class TracerState {
16 /**
17 * Shared tracer state for the current process or web request, `null` if uninitialized.
18 * @var TracerState|null
19 */
20 private static ?TracerState $instance = null;
21
22 /**
23 * List of already finished spans to be exported.
24 * @var SpanContext[]
25 */
26 private array $finishedSpanContexts = [];
27
28 /**
29 * Stack holding contexts for activated spans.
30 * @var SpanContext[]
31 */
32 private array $activeSpanContextStack = [];
33
34 /**
35 * Get or initialize the shared tracer state for the current process or web request.
36 * @return TracerState
37 */
38 public static function getInstance(): TracerState {
39 self::$instance ??= new self();
40 return self::$instance;
41 }
42
43 /**
44 * Reset shared tracer state. Useful for testing.
45 * @return void
46 */
47 public static function destroyInstance(): void {
48 Assert::precondition(
49 defined( 'MW_PHPUNIT_TEST' ),
50 'This function can only be called in tests'
51 );
52
53 self::$instance = null;
54 }
55
56 /**
57 * Add the given span to the list of finished spans.
58 * @param SpanContext $context
59 * @return void
60 */
61 public function addSpanContext( SpanContext $context ): void {
62 $this->finishedSpanContexts[] = $context;
63 }
64
65 /**
66 * Get the list of finished spans.
67 * @return SpanContext[]
68 */
69 public function getSpanContexts(): array {
70 return $this->finishedSpanContexts;
71 }
72
73 /**
74 * Clear the list of finished spans.
75 */
76 public function clearSpanContexts(): void {
77 $this->finishedSpanContexts = [];
78 }
79
80 /**
81 * Make the given span the active span.
82 * @param SpanContext $spanContext Context of the span to activate
83 * @return void
84 */
85 public function activateSpan( SpanContext $spanContext ): void {
86 $this->activeSpanContextStack[] = $spanContext;
87 }
88
89 /**
90 * Deactivate the given span, if it was the active span.
91 * @param SpanContext $spanContext Context of the span to deactivate
92 * @return void
93 */
94 public function deactivateSpan( SpanContext $spanContext ): void {
95 $activeSpanContext = $this->getActiveSpanContext();
96
97 Assert::invariant(
98 $activeSpanContext !== null && $activeSpanContext->getSpanId() === $spanContext->getSpanId(),
99 'Attempted to deactivate a span which is not the active span.'
100 );
101
102 array_pop( $this->activeSpanContextStack );
103 }
104
105 /**
106 * Get the context of the currently active span, or `null` if no span is active.
107 * @return SpanContext|null
108 */
109 public function getActiveSpanContext(): ?SpanContext {
110 return $this->activeSpanContextStack[count( $this->activeSpanContextStack ) - 1] ?? null;
111 }
112 }