Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.87% |
26 / 31 |
|
76.92% |
10 / 13 |
CRAP | |
0.00% |
0 / 1 |
GaugeMetric | |
83.87% |
26 / 31 |
|
76.92% |
10 / 13 |
19.36 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
set | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getComponent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTypeIndicator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSamples | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSampleCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSampleRate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setSampleRate | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
getLabelKeys | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setLabel | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
copyToStatsdAt | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
2.86 | |||
fresh | |
100.00% |
2 / 2 |
|
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 Psr\Log\LoggerInterface; |
26 | use Wikimedia\Stats\Exceptions\IllegalOperationException; |
27 | use Wikimedia\Stats\Sample; |
28 | |
29 | /** |
30 | * Gauge Metric Implementation |
31 | * |
32 | * Gauge Metrics can be set to any numeric value and are identified by type "g". |
33 | * |
34 | * @author Cole White |
35 | * @since 1.38 |
36 | */ |
37 | class GaugeMetric implements MetricInterface { |
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 | * @var string |
45 | */ |
46 | private const TYPE_INDICATOR = "g"; |
47 | |
48 | /** @var BaseMetricInterface */ |
49 | private BaseMetricInterface $baseMetric; |
50 | |
51 | /** @var LoggerInterface */ |
52 | private LoggerInterface $logger; |
53 | |
54 | /** @inheritDoc */ |
55 | public function __construct( $baseMetric, $logger ) { |
56 | $this->baseMetric = $baseMetric; |
57 | $this->logger = $logger; |
58 | } |
59 | |
60 | /** |
61 | * Sets metric to value. |
62 | * |
63 | * @param float $value |
64 | * @return void |
65 | */ |
66 | public function set( float $value ): void { |
67 | foreach ( $this->baseMetric->getStatsdNamespaces() as $namespace ) { |
68 | $this->baseMetric->getStatsdDataFactory()->gauge( $namespace, $value ); |
69 | } |
70 | |
71 | try { |
72 | $this->baseMetric->addSample( new Sample( $this->baseMetric->getLabelValues(), $value ) ); |
73 | } catch ( IllegalOperationException $ex ) { |
74 | // Log the condition and give the caller something that will absorb calls. |
75 | trigger_error( $ex->getMessage(), E_USER_WARNING ); |
76 | } |
77 | } |
78 | |
79 | /** @inheritDoc */ |
80 | public function getName(): string { |
81 | return $this->baseMetric->getName(); |
82 | } |
83 | |
84 | /** @inheritDoc */ |
85 | public function getComponent(): string { |
86 | return $this->baseMetric->getComponent(); |
87 | } |
88 | |
89 | /** @inheritDoc */ |
90 | public function getTypeIndicator(): string { |
91 | return self::TYPE_INDICATOR; |
92 | } |
93 | |
94 | /** @inheritDoc */ |
95 | public function getSamples(): array { |
96 | return $this->baseMetric->getSamples(); |
97 | } |
98 | |
99 | /** @inheritDoc */ |
100 | public function getSampleCount(): int { |
101 | return $this->baseMetric->getSampleCount(); |
102 | } |
103 | |
104 | /** @inheritDoc */ |
105 | public function getSampleRate(): float { |
106 | return $this->baseMetric->getSampleRate(); |
107 | } |
108 | |
109 | /** @inheritDoc */ |
110 | public function setSampleRate( float $sampleRate ) { |
111 | try { |
112 | $this->baseMetric->setSampleRate( $sampleRate ); |
113 | } catch ( IllegalOperationException | InvalidArgumentException $ex ) { |
114 | // Log the condition and give the caller something that will absorb calls. |
115 | trigger_error( $ex->getMessage(), E_USER_WARNING ); |
116 | return new NullMetric; |
117 | } |
118 | return $this; |
119 | } |
120 | |
121 | /** @inheritDoc */ |
122 | public function getLabelKeys(): array { |
123 | return $this->baseMetric->getLabelKeys(); |
124 | } |
125 | |
126 | /** @inheritDoc */ |
127 | public function setLabel( string $key, string $value ) { |
128 | try { |
129 | $this->baseMetric->addLabel( $key, $value ); |
130 | } catch ( IllegalOperationException | InvalidArgumentException $ex ) { |
131 | // Log the condition and give the caller something that will absorb calls. |
132 | trigger_error( $ex->getMessage(), E_USER_WARNING ); |
133 | return new NullMetric; |
134 | } |
135 | return $this; |
136 | } |
137 | |
138 | /** @inheritDoc */ |
139 | public function copyToStatsdAt( $statsdNamespaces ) { |
140 | try { |
141 | $this->baseMetric->setStatsdNamespaces( $statsdNamespaces ); |
142 | } catch ( InvalidArgumentException $ex ) { |
143 | // Log the condition and give the caller something that will absorb calls. |
144 | trigger_error( $ex->getMessage(), E_USER_WARNING ); |
145 | return new NullMetric; |
146 | } |
147 | return $this; |
148 | } |
149 | |
150 | /** @inheritDoc */ |
151 | public function fresh(): GaugeMetric { |
152 | $this->baseMetric->clearLabels(); |
153 | return $this; |
154 | } |
155 | } |