Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.30% |
36 / 37 |
|
92.31% |
12 / 13 |
CRAP | |
0.00% |
0 / 1 |
MetricTrait | |
97.30% |
36 / 37 |
|
92.31% |
12 / 13 |
21 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getComponent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSamples | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSampleCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSampleRate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setSampleRate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getLabelKeys | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
setLabel | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
setLabels | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
copyToStatsdAt | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
fresh | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isHistogram | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 |
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 | |
28 | /** |
29 | * Implementation code common to all metric types. |
30 | * |
31 | * @internal |
32 | * @since 1.43 |
33 | */ |
34 | trait MetricTrait { |
35 | |
36 | private BaseMetricInterface $baseMetric; |
37 | private LoggerInterface $logger; |
38 | private ?string $bucket = null; |
39 | |
40 | /** @inheritDoc */ |
41 | public function __construct( $baseMetric, $logger ) { |
42 | $this->baseMetric = $baseMetric; |
43 | $this->logger = $logger; |
44 | } |
45 | |
46 | /** @inheritDoc */ |
47 | public function getName(): string { |
48 | return $this->baseMetric->getName(); |
49 | } |
50 | |
51 | /** @inheritDoc */ |
52 | public function getComponent(): string { |
53 | return $this->baseMetric->getComponent(); |
54 | } |
55 | |
56 | /** @inheritDoc */ |
57 | public function getSamples(): array { |
58 | return $this->baseMetric->getSamples(); |
59 | } |
60 | |
61 | /** @inheritDoc */ |
62 | public function getSampleCount(): int { |
63 | return $this->baseMetric->getSampleCount(); |
64 | } |
65 | |
66 | /** @inheritDoc */ |
67 | public function getSampleRate(): float { |
68 | return $this->baseMetric->getSampleRate(); |
69 | } |
70 | |
71 | /** @inheritDoc */ |
72 | public function setSampleRate( float $sampleRate ) { |
73 | try { |
74 | $this->baseMetric->setSampleRate( $sampleRate ); |
75 | } catch ( IllegalOperationException | InvalidArgumentException $ex ) { |
76 | // Log the condition and give the caller something that will absorb calls. |
77 | trigger_error( "Stats: ({$this->getName()}) {$ex->getMessage()}", E_USER_WARNING ); |
78 | return new NullMetric; |
79 | } |
80 | return $this; |
81 | } |
82 | |
83 | /** @inheritDoc */ |
84 | public function getLabelKeys(): array { |
85 | $labelKeys = $this->baseMetric->getLabelKeys(); |
86 | if ( $this->bucket ) { |
87 | $labelKeys[] = 'le'; |
88 | } |
89 | return $labelKeys; |
90 | } |
91 | |
92 | /** @inheritDoc */ |
93 | public function setLabel( string $key, string $value ) { |
94 | if ( strcasecmp( $key, 'le' ) === 0 ) { |
95 | trigger_error( "Stats: ({$this->getName()}) 'le' cannot be used as a label key", E_USER_WARNING ); |
96 | return new NullMetric(); |
97 | } |
98 | try { |
99 | $this->baseMetric->addLabel( $key, $value ); |
100 | } catch ( IllegalOperationException | InvalidArgumentException $ex ) { |
101 | // Log the condition and give the caller something that will absorb calls. |
102 | trigger_error( "Stats: ({$this->getName()}) {$ex->getMessage()}", E_USER_WARNING ); |
103 | return new NullMetric; |
104 | } |
105 | return $this; |
106 | } |
107 | |
108 | /** @inheritDoc */ |
109 | public function setLabels( array $labels ) { |
110 | foreach ( $labels as $key => $value ) { |
111 | $metric = $this->setLabel( $key, $value ); |
112 | if ( $metric instanceof NullMetric ) { |
113 | return $metric; |
114 | } |
115 | } |
116 | return $this; |
117 | } |
118 | |
119 | /** @inheritDoc */ |
120 | public function copyToStatsdAt( $statsdNamespaces ) { |
121 | try { |
122 | $this->baseMetric->setStatsdNamespaces( $statsdNamespaces ); |
123 | } catch ( InvalidArgumentException $ex ) { |
124 | // Log the condition and give the caller something that will absorb calls. |
125 | trigger_error( "Stats: ({$this->getName()}) {$ex->getMessage()}", E_USER_WARNING ); |
126 | return new NullMetric; |
127 | } |
128 | return $this; |
129 | } |
130 | |
131 | /** @inheritDoc */ |
132 | public function fresh(): self { |
133 | $this->baseMetric->clearLabels(); |
134 | return $this; |
135 | } |
136 | |
137 | /** @inheritDoc */ |
138 | public function isHistogram(): bool { |
139 | return ( $this instanceof CounterMetric && $this->bucket !== null ); |
140 | } |
141 | } |