Debian Codegraph @

includes/libs/Stats/Metrics/BaseMetricInterface.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\IBufferingStatsdDataFactory;
25 use Wikimedia\Stats\Sample;
26
27 /**
28 * Base Metric Interface
29 *
30 * Interface for defining a Base Metric.
31 *
32 * @author Cole White
33 * @since 1.41
34 */
35 interface BaseMetricInterface {
36
37 /**
38 * @param string $component The component this metric will track.
39 * @param string $name The Metric Name.
40 */
41 public function __construct( string $component, string $name );
42
43 /**
44 * Records a Metric Sample.
45 *
46 * @param Sample $sample
47 * @return void
48 */
49 public function addSample( Sample $sample ): void;
50
51 /**
52 * Returns the configured sample rate.
53 *
54 * @return float
55 */
56 public function getSampleRate(): float;
57
58 /**
59 * Sets the sample rate.
60 *
61 * @param float $sampleRate
62 */
63 public function setSampleRate( float $sampleRate ): void;
64
65 /**
66 * Returns subset of samples corresponding to sample rate setting.
67 *
68 * @return Sample[]
69 */
70 public function getSamples(): array;
71
72 /**
73 * Returns a count of samples recorded by the metric.
74 *
75 * @return int
76 */
77 public function getSampleCount(): int;
78
79 /**
80 * Returns the Metric Name
81 *
82 * @return string
83 */
84 public function getName(): string;
85
86 /**
87 * Add a label with key => value.
88 * Note that the order in which labels are added is significant for StatsD output.
89 *
90 * Example:
91 * ```php
92 * $statsFactory->withComponent( 'demo' )
93 * ->getCounter( 'testMetric_total' )
94 * ->setLabel( 'first', 'foo' )
95 * ->setLabel( 'second', 'bar' )
96 * ->setLabel( 'third', 'baz' )
97 * ->increment();
98 * ```
99 * statsd: "mediawiki.demo.testMetric_total.foo.bar.baz"
100 * prometheus: "mediawiki_demo_testMetric_total{first='foo',second='bar',third='baz'}
101 *
102 * @param string $key
103 * @param string $value
104 * @return void
105 */
106 public function addLabel( string $key, string $value ): void;
107
108 /**
109 * Returns array of label keys.
110 *
111 * @return string[]
112 */
113 public function getLabelKeys(): array;
114
115 /**
116 * Returns an array of label values in the order of label keys.
117 *
118 * @return string[]
119 */
120 public function getLabelValues(): array;
121
122 /**
123 * Returns the Metric Component
124 *
125 * @return string
126 */
127 public function getComponent(): string;
128
129 /**
130 * Clears the working labels.
131 *
132 * @return void
133 */
134 public function clearLabels(): void;
135
136 /**
137 * Gets StatsD Data Factory instance or null.
138 */
139 public function getStatsdDataFactory();
140
141 /**
142 * Validates and sets legacy StatsD namespaces.
143 *
144 * @param string|string[] $statsdNamespaces
145 * @return void
146 */
147 public function setStatsdNamespaces( $statsdNamespaces ): void;
148
149 /**
150 * Returns the configured legacy StatsD namespaces.
151 *
152 * @return string[]
153 */
154 public function getStatsdNamespaces(): array;
155
156 /**
157 * StatsD Data Factory instance to copy metrics to.
158 *
159 * @param IBufferingStatsdDataFactory $statsdDataFactory
160 * @return BaseMetricInterface
161 */
162 public function withStatsdDataFactory( IBufferingStatsdDataFactory $statsdDataFactory );
163 }