Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RunningTimer | |
0.00% |
0 / 16 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| setLabel | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setLabels | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| stop | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace Wikimedia\Stats\Metrics; |
| 8 | |
| 9 | use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 10 | |
| 11 | /** |
| 12 | * RunningTimer Implementation |
| 13 | * |
| 14 | * A class to help TimingMetrics handle instances of recursion. |
| 15 | * |
| 16 | * @author Cole White |
| 17 | * @since 1.45 |
| 18 | */ |
| 19 | class RunningTimer { |
| 20 | |
| 21 | /** @var TimingMetric|NullMetric */ |
| 22 | private $metric; |
| 23 | private ?float $startTime; |
| 24 | private array $workingLabels; |
| 25 | |
| 26 | public function __construct( float $startTime, TimingMetric $metric, array $initialLabels ) { |
| 27 | $this->startTime = $startTime; |
| 28 | $this->metric = $metric; |
| 29 | $this->workingLabels = $initialLabels; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Handle for MetricInterface::setLabel |
| 34 | * |
| 35 | * @see MetricInterface::setLabel |
| 36 | * @return self |
| 37 | */ |
| 38 | public function setLabel( string $key, string $value ) { |
| 39 | $this->workingLabels[$key] = $value; |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Handle for MetricInterface::setLabels |
| 45 | * |
| 46 | * @see MetricInterface::setLabels |
| 47 | * @return self |
| 48 | */ |
| 49 | public function setLabels( array $labels ) { |
| 50 | $this->workingLabels = $labels; |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Stop the running timer. |
| 56 | */ |
| 57 | public function stop() { |
| 58 | if ( $this->startTime === null ) { |
| 59 | trigger_error( |
| 60 | "Stats: ({$this->metric->getName()}) cannot call stop() more than once on a RunningTimer.", |
| 61 | E_USER_WARNING |
| 62 | ); |
| 63 | return; |
| 64 | } |
| 65 | // T406170 - move setting labels near recording the sample. |
| 66 | // |
| 67 | // Downstream label changes can affect upstream usage because they're the same |
| 68 | // metric instance. Here, we'll assume any labels set when the metric was |
| 69 | // initially declared or changed against the RunningTimer instance are correct |
| 70 | // and set them before sample capture time. |
| 71 | $this->metric = $this->metric->setLabels( $this->workingLabels ); |
| 72 | $this->metric->observeNanoseconds( ConvertibleTimestamp::hrtime() - $this->startTime ); |
| 73 | $this->startTime = null; |
| 74 | } |
| 75 | } |