Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
92.86% covered (success)
92.86%
13 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
CounterMetric
96.88% covered (success)
96.88%
31 / 32
92.86% covered (success)
92.86%
13 / 14
19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 increment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 incrementBy
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getComponent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTypeIndicator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSamples
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSampleCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSampleRate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSampleRate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getLabelKeys
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLabel
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 copyToStatsdAt
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 fresh
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
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
20declare( strict_types=1 );
21
22namespace Wikimedia\Stats\Metrics;
23
24use InvalidArgumentException;
25use Psr\Log\LoggerInterface;
26use Wikimedia\Stats\Exceptions\IllegalOperationException;
27use Wikimedia\Stats\Sample;
28
29/**
30 * Counter Metric Implementation
31 *
32 * Counter Metrics only ever increase and are identified by type "c".
33 *
34 * @author Cole White
35 * @since 1.38
36 */
37class CounterMetric 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 = "c";
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     * Increments metric by one.
62     *
63     * @return void
64     */
65    public function increment(): void {
66        $this->incrementBy( 1 );
67    }
68
69    /**
70     * Increments metric by provided value.
71     *
72     * @param float $value
73     * @return void
74     */
75    public function incrementBy( float $value ): void {
76        foreach ( $this->baseMetric->getStatsdNamespaces() as $namespace ) {
77            $this->baseMetric->getStatsdDataFactory()->updateCount( $namespace, $value );
78        }
79
80        try {
81            $this->baseMetric->addSample( new Sample( $this->baseMetric->getLabelValues(), $value ) );
82        } catch ( IllegalOperationException $ex ) {
83            // Log the condition and give the caller something that will absorb calls.
84            trigger_error( $ex->getMessage(), E_USER_WARNING );
85        }
86    }
87
88    /** @inheritDoc */
89    public function getName(): string {
90        return $this->baseMetric->getName();
91    }
92
93    /** @inheritDoc */
94    public function getComponent(): string {
95        return $this->baseMetric->getComponent();
96    }
97
98    /** @inheritDoc */
99    public function getTypeIndicator(): string {
100        return self::TYPE_INDICATOR;
101    }
102
103    /** @inheritDoc */
104    public function getSamples(): array {
105        return $this->baseMetric->getSamples();
106    }
107
108    /** @inheritDoc */
109    public function getSampleCount(): int {
110        return $this->baseMetric->getSampleCount();
111    }
112
113    /** @inheritDoc */
114    public function getSampleRate(): float {
115        return $this->baseMetric->getSampleRate();
116    }
117
118    /** @inheritDoc */
119    public function setSampleRate( float $sampleRate ) {
120        try {
121            $this->baseMetric->setSampleRate( $sampleRate );
122        } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
123            // Log the condition and give the caller something that will absorb calls.
124            trigger_error( $ex->getMessage(), E_USER_WARNING );
125            return new NullMetric;
126        }
127        return $this;
128    }
129
130    /** @inheritDoc */
131    public function getLabelKeys(): array {
132        return $this->baseMetric->getLabelKeys();
133    }
134
135    /** @inheritDoc */
136    public function setLabel( string $key, string $value ) {
137        try {
138            $this->baseMetric->addLabel( $key, $value );
139        } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
140            // Log the condition and give the caller something that will absorb calls.
141            trigger_error( $ex->getMessage(), E_USER_WARNING );
142            return new NullMetric;
143        }
144        return $this;
145    }
146
147    /** @inheritDoc */
148    public function copyToStatsdAt( $statsdNamespaces ) {
149        try {
150            $this->baseMetric->setStatsdNamespaces( $statsdNamespaces );
151        } catch ( InvalidArgumentException $ex ) {
152            // Log the condition and give the caller something that will absorb calls.
153            trigger_error( $ex->getMessage(), E_USER_WARNING );
154            return new NullMetric;
155        }
156        return $this;
157    }
158
159    /** @inheritDoc */
160    public function fresh(): CounterMetric {
161        $this->baseMetric->clearLabels();
162        return $this;
163    }
164}