MediaWiki master
RunningTimer.php
Go to the documentation of this file.
1<?php
8
9use Wikimedia\Timestamp\ConvertibleTimestamp;
10
20
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
38 public function setLabel( string $key, string $value ) {
39 $this->workingLabels[$key] = $value;
40 return $this;
41 }
42
49 public function setLabels( array $labels ) {
50 $this->workingLabels = $labels;
51 return $this;
52 }
53
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}
Null Metric Implementation.
RunningTimer Implementation.
stop()
Stop the running timer.
setLabels(array $labels)
Handle for MetricInterface::setLabels.
setLabel(string $key, string $value)
Handle for MetricInterface::setLabel.
__construct(float $startTime, TimingMetric $metric, array $initialLabels)
Timing Metric Implementation.