Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
64.71% |
11 / 17 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
CounterMetric | |
64.71% |
11 / 17 |
|
50.00% |
2 / 4 |
16.32 | |
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 | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
6.00 | |||
getTypeIndicator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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 InvalidArgumentException; |
25 | use Wikimedia\Stats\Exceptions\IllegalOperationException; |
26 | use Wikimedia\Stats\Sample; |
27 | |
28 | /** |
29 | * Counter Metric Implementation |
30 | * |
31 | * Counter Metrics only ever increase and are identified by type "c". |
32 | * |
33 | * @author Cole White |
34 | * @since 1.38 |
35 | */ |
36 | class CounterMetric 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 = "c"; |
45 | |
46 | /** |
47 | * Increments metric by one. |
48 | */ |
49 | public function increment(): void { |
50 | $this->incrementBy( 1 ); |
51 | } |
52 | |
53 | /** |
54 | * Increments metric by provided value. |
55 | * |
56 | * @param float $value |
57 | * @return void |
58 | */ |
59 | public function incrementBy( float $value ): void { |
60 | if ( $value < 0 ) { |
61 | trigger_error( "Stats: got negative value for counter \"{$this->getName()}\"", E_USER_WARNING ); |
62 | return; |
63 | } |
64 | |
65 | foreach ( $this->baseMetric->getStatsdNamespaces() as $namespace ) { |
66 | $this->baseMetric->getStatsdDataFactory()->updateCount( $namespace, $value ); |
67 | } |
68 | |
69 | try { |
70 | $labelValues = $this->baseMetric->getLabelValues(); |
71 | if ( $this->bucket ) { |
72 | $labelValues[] = $this->bucket; |
73 | } |
74 | $this->baseMetric->addSample( new Sample( $labelValues, $value ) ); |
75 | } catch ( IllegalOperationException $ex ) { |
76 | // Log the condition and give the caller something that will absorb calls. |
77 | trigger_error( $ex->getMessage(), E_USER_WARNING ); |
78 | } |
79 | } |
80 | |
81 | /** |
82 | * Sets the bucket value |
83 | * |
84 | * Only allows float, int, or literal '+Inf' as value. |
85 | * |
86 | * WARNING: This function exists to support HistogramMetric. It should not be used elsewhere. |
87 | * |
88 | * @internal |
89 | * @param float|int|string $value |
90 | * @return CounterMetric |
91 | */ |
92 | public function setBucket( $value ) { |
93 | if ( $value == "+Inf" || ( is_float( $value ) || is_int( $value ) ) ) { |
94 | $this->bucket = "{$value}"; |
95 | return $this; |
96 | } |
97 | throw new InvalidArgumentException( "Stats: Got illegal bucket value '{$value}' - must be float or '+Inf'" ); |
98 | } |
99 | |
100 | /** @inheritDoc */ |
101 | public function getTypeIndicator(): string { |
102 | return self::TYPE_INDICATOR; |
103 | } |
104 | } |