| 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 | * Gauge Metric Implementation |
| 29 | * |
| 30 | * Gauge Metrics can be set to any numeric value and are identified by type "g". |
| 31 | * |
| 32 | * @author Cole White |
| 33 | * @since 1.38 |
| 34 | */ |
| 35 | class GaugeMetric implements MetricInterface { |
| 36 | use MetricTrait; |
| 37 | |
| 38 | /** |
| 39 | * The StatsD protocol type indicator: |
| 40 | * https://github.com/statsd/statsd/blob/v0.9.0/docs/metric_types.md |
| 41 | * https://docs.datadoghq.com/developers/dogstatsd/datagram_shell/?tab=metrics |
| 42 | */ |
| 43 | private const TYPE_INDICATOR = "g"; |
| 44 | |
| 45 | /** |
| 46 | * Sets metric to value. |
| 47 | * |
| 48 | * @param float $value |
| 49 | * @return void |
| 50 | */ |
| 51 | public function set( float $value ): void { |
| 52 | foreach ( $this->baseMetric->getStatsdNamespaces() as $namespace ) { |
| 53 | $this->baseMetric->getStatsdDataFactory()->gauge( $namespace, $value ); |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | $this->baseMetric->addSample( new Sample( $this->baseMetric->getLabelValues(), $value ) ); |
| 58 | } catch ( IllegalOperationException $ex ) { |
| 59 | // Log the condition and give the caller something that will absorb calls. |
| 60 | trigger_error( $ex->getMessage(), E_USER_WARNING ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** @inheritDoc */ |
| 65 | public function getTypeIndicator(): string { |
| 66 | return self::TYPE_INDICATOR; |
| 67 | } |
| 68 | } |