Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
5 / 6
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GaugeMetric
83.33% covered (warning)
83.33%
5 / 6
50.00% covered (danger)
50.00%
1 / 2
4.07
0.00% covered (danger)
0.00%
0 / 1
 set
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 getTypeIndicator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7declare( strict_types=1 );
8
9namespace Wikimedia\Stats\Metrics;
10
11use Wikimedia\Stats\Exceptions\IllegalOperationException;
12use Wikimedia\Stats\Sample;
13
14/**
15 * Gauge Metric Implementation
16 *
17 * Gauge Metrics can be set to any numeric value and are identified by type "g".
18 *
19 * @author Cole White
20 * @since 1.38
21 */
22class GaugeMetric implements MetricInterface {
23    use MetricTrait;
24
25    /**
26     * The StatsD protocol type indicator:
27     * https://github.com/statsd/statsd/blob/v0.9.0/docs/metric_types.md
28     * https://docs.datadoghq.com/developers/dogstatsd/datagram_shell/?tab=metrics
29     */
30    private const TYPE_INDICATOR = "g";
31
32    /**
33     * Sets metric to value.
34     *
35     * @param float $value
36     * @return void
37     */
38    public function set( float $value ): void {
39        foreach ( $this->baseMetric->getStatsdNamespaces() as $namespace ) {
40            $this->baseMetric->getStatsdDataFactory()->gauge( $namespace, $value );
41        }
42
43        try {
44            $this->baseMetric->addSample( new Sample( $this->baseMetric->getLabelValues(), $value ) );
45        } catch ( IllegalOperationException $ex ) {
46            // Log the condition and give the caller something that will absorb calls.
47            trigger_error( "Stats: ({$this->getName()}): {$ex->getMessage()}", E_USER_WARNING );
48        }
49    }
50
51    /** @inheritDoc */
52    public function getTypeIndicator(): string {
53        return self::TYPE_INDICATOR;
54    }
55}