Debian Codegraph @

includes/libs/Stats/Metrics/CounterMetric.php

raw symbols SCIP
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 Wikimedia\Stats\Exceptions\IllegalOperationException;
25 use Wikimedia\Stats\Sample;
26
27 /**
28 * Counter Metric Implementation
29 *
30 * Counter Metrics only ever increase and are identified by type "c".
31 *
32 * @author Cole White
33 * @since 1.38
34 */
35 class CounterMetric implements MetricInterface {
36 use MetricTrait;
37
38 /**
39 * The StatsD protocol type indicator:
40 * https://github.com/statsd/statsd/blob/v0.9.0/docs/metric_types.md
41 * https://docs.datadoghq.com/developers/dogstatsd/datagram_shell/?tab=metrics
42 */
43 private const TYPE_INDICATOR = "c";
44
45 /**
46 * Increments metric by one.
47 *
48 * @return void
49 */
50 public function increment(): void {
51 $this->incrementBy( 1 );
52 }
53
54 /**
55 * Increments metric by provided value.
56 *
57 * @param float $value
58 * @return void
59 */
60 public function incrementBy( float $value ): void {
61 foreach ( $this->baseMetric->getStatsdNamespaces() as $namespace ) {
62 $this->baseMetric->getStatsdDataFactory()->updateCount( $namespace, $value );
63 }
64
65 try {
66 $this->baseMetric->addSample( new Sample( $this->baseMetric->getLabelValues(), $value ) );
67 } catch ( IllegalOperationException $ex ) {
68 // Log the condition and give the caller something that will absorb calls.
69 trigger_error( $ex->getMessage(), E_USER_WARNING );
70 }
71 }
72
73 /** @inheritDoc */
74 public function getTypeIndicator(): string {
75 return self::TYPE_INDICATOR;
76 }
77 }