| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * @file |
| 18 | */ |
| 19 | |
| 20 | declare( strict_types=1 ); |
| 21 | |
| 22 | namespace Wikimedia\Stats\Metrics; |
| 23 | |
| 24 | use Wikimedia\Stats\Exceptions\IllegalOperationException; |
| 25 | use Wikimedia\Stats\Sample; |
| 26 | |
| 27 | /** |
| 28 | * Timing Metric Implementation |
| 29 | * |
| 30 | * Timing metrics track duration data which can be broken into histograms. |
| 31 | * They are identified by type "ms". |
| 32 | * |
| 33 | * @author Cole White |
| 34 | * @since 1.38 |
| 35 | */ |
| 36 | class TimingMetric implements MetricInterface { |
| 37 | use MetricTrait; |
| 38 | |
| 39 | /** |
| 40 | * The StatsD protocol type indicator: |
| 41 | * https://github.com/statsd/statsd/blob/v0.9.0/docs/metric_types.md |
| 42 | * https://docs.datadoghq.com/developers/dogstatsd/datagram_shell/?tab=metrics |
| 43 | */ |
| 44 | private const TYPE_INDICATOR = "ms"; |
| 45 | |
| 46 | /** @var float|null */ |
| 47 | private ?float $startTime = null; |
| 48 | |
| 49 | /** |
| 50 | * Starts a timer. |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public function start(): void { |
| 55 | $this->startTime = hrtime( true ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Stops a running timer. |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public function stop(): void { |
| 64 | if ( $this->startTime === null ) { |
| 65 | trigger_error( "Stats: stop() called before start() for metric '{$this->getName()}'", E_USER_WARNING ); |
| 66 | return; |
| 67 | } |
| 68 | $this->observeNanoseconds( hrtime( true ) - $this->startTime ); |
| 69 | $this->startTime = null; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Records a previously calculated observation in milliseconds. |
| 74 | * |
| 75 | * @param float $milliseconds |
| 76 | * @return void |
| 77 | */ |
| 78 | public function observe( float $milliseconds ): void { |
| 79 | foreach ( $this->baseMetric->getStatsdNamespaces() as $namespace ) { |
| 80 | $this->baseMetric->getStatsdDataFactory()->timing( $namespace, $milliseconds ); |
| 81 | } |
| 82 | |
| 83 | try { |
| 84 | $this->baseMetric->addSample( new Sample( $this->baseMetric->getLabelValues(), $milliseconds ) ); |
| 85 | } catch ( IllegalOperationException $ex ) { |
| 86 | // Log the condition and give the caller something that will absorb calls. |
| 87 | trigger_error( $ex->getMessage(), E_USER_WARNING ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Record a previously calculated observation in seconds. |
| 93 | * |
| 94 | * Common usage: |
| 95 | * ```php |
| 96 | * $startTime = microtime( true ) |
| 97 | * # work to be measured... |
| 98 | * $metric->observeSeconds( microtime( true ) - $startTime ) |
| 99 | * ``` |
| 100 | * |
| 101 | * @param float $seconds |
| 102 | * @return void |
| 103 | * @since 1.43 |
| 104 | */ |
| 105 | public function observeSeconds( float $seconds ): void { |
| 106 | $this->observe( $seconds * 1000 ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Record a previously calculated observation in nanoseconds. |
| 111 | * |
| 112 | * Common usage: |
| 113 | * ```php |
| 114 | * $startTime = hrtime( true ) |
| 115 | * # work to be measured... |
| 116 | * $metric->observeNanoseconds( hrtime( true ) - $startTime ) |
| 117 | * ``` |
| 118 | * @param float $nanoseconds |
| 119 | * @return void |
| 120 | * @since 1.43 |
| 121 | */ |
| 122 | public function observeNanoseconds( float $nanoseconds ): void { |
| 123 | $this->observe( $nanoseconds * 1e-6 ); |
| 124 | } |
| 125 | |
| 126 | /** @inheritDoc */ |
| 127 | public function getTypeIndicator(): string { |
| 128 | return self::TYPE_INDICATOR; |
| 129 | } |
| 130 | } |