Debian Codegraph @

includes/profiler/ProfilingContext.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Profiler;
4
5 /**
6 * Class for tracking request-level classification information for profiling/stats/logging
7 *
8 * @note: this avoids the use of MediaWikiServices so that shutdown functions can use it
9 *
10 * @since 1.40
11 */
12 class ProfilingContext {
13 private string $entryPoint = 'unknown';
14 private string $handler = 'unknown';
15
16 private bool $initialized = false;
17
18 private static ?ProfilingContext $instance = null;
19
20 public static function singleton(): ProfilingContext {
21 self::$instance ??= new self();
22
23 return self::$instance;
24 }
25
26 /**
27 * Set entry point name and principle handler name for this request
28 *
29 * @param string $entryPoint Entry point script name (alphanumeric characters)
30 * @param string $handler Handler name (printable ASCII characters)
31 */
32 public function init( string $entryPoint, string $handler ) {
33 // Ignore nested nesting (e.g. rest.php handlers that use ApiMain)
34 if ( !$this->initialized ) {
35 $this->initialized = true;
36 $this->entryPoint = $entryPoint;
37 $this->handler = $handler;
38 }
39 }
40
41 /**
42 * @return bool Whether the context was initialized yet
43 */
44 public function isInitialized() {
45 return $this->initialized;
46 }
47
48 /**
49 * @return string Entry point name for this request (e.g. "index", "api", "load")
50 */
51 public function getEntryPoint(): string {
52 return $this->entryPoint;
53 }
54
55 /**
56 * @return string Handler name for this request (e.g. "edit", "recentchanges")
57 */
58 public function getHandler(): string {
59 return $this->handler;
60 }
61
62 /**
63 * @return string Statsd metric name prefix for this entry point and handler
64 */
65 public function getHandlerMetricPrefix(): string {
66 // Replace any characters that may have a special meaning in statsd/graphite
67 return $this->entryPoint . '_' . strtr(
68 $this->handler,
69 [ '{' => '', '}' => '', ':' => '_', '/' => '_', '.' => '_' ]
70 );
71 }
72
73 /**
74 * @internal For testing only
75 */
76 public static function destroySingleton() {
77 self::$instance = null;
78 }
79 }