Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
68.42% |
13 / 19 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CounterMetric | |
68.42% |
13 / 19 |
|
50.00% |
2 / 4 |
14.81 | |
0.00% |
0 / 1 |
| increment | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| incrementBy | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
6.20 | |||
| setBucket | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
4.59 | |||
| getTypeIndicator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | declare( strict_types=1 ); |
| 8 | |
| 9 | namespace Wikimedia\Stats\Metrics; |
| 10 | |
| 11 | use InvalidArgumentException; |
| 12 | use Wikimedia\Stats\Exceptions\IllegalOperationException; |
| 13 | use Wikimedia\Stats\Sample; |
| 14 | |
| 15 | /** |
| 16 | * Counter Metric Implementation |
| 17 | * |
| 18 | * Counter Metrics only ever increase and are identified by type "c". |
| 19 | * |
| 20 | * @author Cole White |
| 21 | * @since 1.38 |
| 22 | */ |
| 23 | class CounterMetric implements MetricInterface { |
| 24 | use MetricTrait; |
| 25 | |
| 26 | /** |
| 27 | * The StatsD protocol type indicator: |
| 28 | * https://github.com/statsd/statsd/blob/v0.9.0/docs/metric_types.md |
| 29 | * https://docs.datadoghq.com/developers/dogstatsd/datagram_shell/?tab=metrics |
| 30 | */ |
| 31 | private const TYPE_INDICATOR = "c"; |
| 32 | |
| 33 | /** |
| 34 | * Increments metric by one. |
| 35 | */ |
| 36 | public function increment(): void { |
| 37 | $this->incrementBy( 1 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Increments metric by provided value. |
| 42 | * |
| 43 | * @param float $value |
| 44 | * @return void |
| 45 | */ |
| 46 | public function incrementBy( float $value ): void { |
| 47 | if ( $value < 0 ) { |
| 48 | trigger_error( "Stats: ({$this->getName()}) Counter got negative value", E_USER_WARNING ); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | foreach ( $this->baseMetric->getStatsdNamespaces() as $namespace ) { |
| 53 | $this->baseMetric->getStatsdDataFactory()->updateCount( $namespace, $value ); |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | $labelValues = $this->baseMetric->getLabelValues(); |
| 58 | if ( $this->bucket ) { |
| 59 | $labelValues[] = $this->bucket; |
| 60 | } |
| 61 | $this->baseMetric->addSample( new Sample( $labelValues, $value ) ); |
| 62 | } catch ( IllegalOperationException $ex ) { |
| 63 | // Log the condition and give the caller something that will absorb calls. |
| 64 | trigger_error( "Stats: ({$this->getName()}): {$ex->getMessage()}", E_USER_WARNING ); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sets the bucket value |
| 70 | * |
| 71 | * Only allows float, int, or literal '+Inf' as value. |
| 72 | * |
| 73 | * WARNING: This function exists to support HistogramMetric. It should not be used elsewhere. |
| 74 | * |
| 75 | * @internal |
| 76 | * @param float|int|string $value |
| 77 | * @return CounterMetric |
| 78 | */ |
| 79 | public function setBucket( $value ) { |
| 80 | if ( $value == "+Inf" || ( is_float( $value ) || is_int( $value ) ) ) { |
| 81 | $this->bucket = "{$value}"; |
| 82 | return $this; |
| 83 | } |
| 84 | throw new InvalidArgumentException( |
| 85 | "Stats: ({$this->getName()}) Got illegal bucket value '{$value}' - must be float or '+Inf'" |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | /** @inheritDoc */ |
| 90 | public function getTypeIndicator(): string { |
| 91 | return self::TYPE_INDICATOR; |
| 92 | } |
| 93 | } |