Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.57% covered (warning)
78.57%
11 / 14
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProfilingContext
78.57% covered (warning)
78.57%
11 / 14
71.43% covered (warning)
71.43%
5 / 7
8.63
0.00% covered (danger)
0.00%
0 / 1
 singleton
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 init
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isInitialized
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntryPoint
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHandlerMetricPrefix
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 destroySingleton
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace 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 */
12class 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}