aboutsummaryrefslogtreecommitdiffstats
path: root/includes/libs/Stats/Metrics
diff options
context:
space:
mode:
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>2024-10-14 10:14:31 +0000
committerGerrit Code Review <gerrit@wikimedia.org>2024-10-14 10:14:31 +0000
commit3bf6821e2df41e14e82dd6045c3262d790765120 (patch)
tree6aa9bd95c0b00c7a77406cc7f5016383e5a6adad /includes/libs/Stats/Metrics
parent3ecaf9436f311b720a297f023cfd37a7ea6b69d2 (diff)
parentad8e0624a4eb5c5fac7383809f75da356fe905a1 (diff)
downloadmediawikicore-3bf6821e2df41e14e82dd6045c3262d790765120.tar.gz
mediawikicore-3bf6821e2df41e14e82dd6045c3262d790765120.zip
Merge "Use MetricsTrait to DRY out stats code"
Diffstat (limited to 'includes/libs/Stats/Metrics')
-rw-r--r--includes/libs/Stats/Metrics/CounterMetric.php87
-rw-r--r--includes/libs/Stats/Metrics/GaugeMetric.php87
-rw-r--r--includes/libs/Stats/Metrics/MetricInterface.php8
-rw-r--r--includes/libs/Stats/Metrics/MetricTrait.php118
-rw-r--r--includes/libs/Stats/Metrics/TimingMetric.php87
5 files changed, 125 insertions, 262 deletions
diff --git a/includes/libs/Stats/Metrics/CounterMetric.php b/includes/libs/Stats/Metrics/CounterMetric.php
index a6bc09c3dcb2..6fb8a085d797 100644
--- a/includes/libs/Stats/Metrics/CounterMetric.php
+++ b/includes/libs/Stats/Metrics/CounterMetric.php
@@ -21,8 +21,6 @@ declare( strict_types=1 );
namespace Wikimedia\Stats\Metrics;
-use InvalidArgumentException;
-use Psr\Log\LoggerInterface;
use Wikimedia\Stats\Exceptions\IllegalOperationException;
use Wikimedia\Stats\Sample;
@@ -35,6 +33,7 @@ use Wikimedia\Stats\Sample;
* @since 1.38
*/
class CounterMetric implements MetricInterface {
+ use MetricTrait;
/**
* The StatsD protocol type indicator:
@@ -43,18 +42,6 @@ class CounterMetric implements MetricInterface {
*/
private const TYPE_INDICATOR = "c";
- /** @var BaseMetricInterface */
- private BaseMetricInterface $baseMetric;
-
- /** @var LoggerInterface */
- private LoggerInterface $logger;
-
- /** @inheritDoc */
- public function __construct( $baseMetric, $logger ) {
- $this->baseMetric = $baseMetric;
- $this->logger = $logger;
- }
-
/**
* Increments metric by one.
*
@@ -84,79 +71,7 @@ class CounterMetric implements MetricInterface {
}
/** @inheritDoc */
- public function getName(): string {
- return $this->baseMetric->getName();
- }
-
- /** @inheritDoc */
- public function getComponent(): string {
- return $this->baseMetric->getComponent();
- }
-
- /** @inheritDoc */
public function getTypeIndicator(): string {
return self::TYPE_INDICATOR;
}
-
- /** @inheritDoc */
- public function getSamples(): array {
- return $this->baseMetric->getSamples();
- }
-
- /** @inheritDoc */
- public function getSampleCount(): int {
- return $this->baseMetric->getSampleCount();
- }
-
- /** @inheritDoc */
- public function getSampleRate(): float {
- return $this->baseMetric->getSampleRate();
- }
-
- /** @inheritDoc */
- public function setSampleRate( float $sampleRate ) {
- try {
- $this->baseMetric->setSampleRate( $sampleRate );
- } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function getLabelKeys(): array {
- return $this->baseMetric->getLabelKeys();
- }
-
- /** @inheritDoc */
- public function setLabel( string $key, string $value ) {
- try {
- $this->baseMetric->addLabel( $key, $value );
- } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function copyToStatsdAt( $statsdNamespaces ) {
- try {
- $this->baseMetric->setStatsdNamespaces( $statsdNamespaces );
- } catch ( InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function fresh(): CounterMetric {
- $this->baseMetric->clearLabels();
- return $this;
- }
}
diff --git a/includes/libs/Stats/Metrics/GaugeMetric.php b/includes/libs/Stats/Metrics/GaugeMetric.php
index e4c1e3eb0176..27103854a601 100644
--- a/includes/libs/Stats/Metrics/GaugeMetric.php
+++ b/includes/libs/Stats/Metrics/GaugeMetric.php
@@ -21,8 +21,6 @@ declare( strict_types=1 );
namespace Wikimedia\Stats\Metrics;
-use InvalidArgumentException;
-use Psr\Log\LoggerInterface;
use Wikimedia\Stats\Exceptions\IllegalOperationException;
use Wikimedia\Stats\Sample;
@@ -35,6 +33,7 @@ use Wikimedia\Stats\Sample;
* @since 1.38
*/
class GaugeMetric implements MetricInterface {
+ use MetricTrait;
/**
* The StatsD protocol type indicator:
@@ -43,18 +42,6 @@ class GaugeMetric implements MetricInterface {
*/
private const TYPE_INDICATOR = "g";
- /** @var BaseMetricInterface */
- private BaseMetricInterface $baseMetric;
-
- /** @var LoggerInterface */
- private LoggerInterface $logger;
-
- /** @inheritDoc */
- public function __construct( $baseMetric, $logger ) {
- $this->baseMetric = $baseMetric;
- $this->logger = $logger;
- }
-
/**
* Sets metric to value.
*
@@ -75,79 +62,7 @@ class GaugeMetric implements MetricInterface {
}
/** @inheritDoc */
- public function getName(): string {
- return $this->baseMetric->getName();
- }
-
- /** @inheritDoc */
- public function getComponent(): string {
- return $this->baseMetric->getComponent();
- }
-
- /** @inheritDoc */
public function getTypeIndicator(): string {
return self::TYPE_INDICATOR;
}
-
- /** @inheritDoc */
- public function getSamples(): array {
- return $this->baseMetric->getSamples();
- }
-
- /** @inheritDoc */
- public function getSampleCount(): int {
- return $this->baseMetric->getSampleCount();
- }
-
- /** @inheritDoc */
- public function getSampleRate(): float {
- return $this->baseMetric->getSampleRate();
- }
-
- /** @inheritDoc */
- public function setSampleRate( float $sampleRate ) {
- try {
- $this->baseMetric->setSampleRate( $sampleRate );
- } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function getLabelKeys(): array {
- return $this->baseMetric->getLabelKeys();
- }
-
- /** @inheritDoc */
- public function setLabel( string $key, string $value ) {
- try {
- $this->baseMetric->addLabel( $key, $value );
- } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function copyToStatsdAt( $statsdNamespaces ) {
- try {
- $this->baseMetric->setStatsdNamespaces( $statsdNamespaces );
- } catch ( InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function fresh(): GaugeMetric {
- $this->baseMetric->clearLabels();
- return $this;
- }
}
diff --git a/includes/libs/Stats/Metrics/MetricInterface.php b/includes/libs/Stats/Metrics/MetricInterface.php
index 96cc1179d1d8..0add417669c6 100644
--- a/includes/libs/Stats/Metrics/MetricInterface.php
+++ b/includes/libs/Stats/Metrics/MetricInterface.php
@@ -65,7 +65,7 @@ interface MetricInterface {
* Sets sample rate on a new metric instance.
*
* @param float $sampleRate
- * @return CounterMetric|GaugeMetric|TimingMetric|NullMetric
+ * @return self|NullMetric
*/
public function setSampleRate( float $sampleRate );
@@ -92,7 +92,7 @@ interface MetricInterface {
*
* @param string $key
* @param string $value
- * @return CounterMetric|GaugeMetric|TimingMetric|NullMetric
+ * @return self|NullMetric
*/
public function setLabel( string $key, string $value );
@@ -102,14 +102,14 @@ interface MetricInterface {
* Takes a namespace or multiple namespaces.
*
* @param string|string[] $statsdNamespaces
- * @return CounterMetric|GaugeMetric|TimingMetric|NullMetric
+ * @return self|NullMetric
*/
public function copyToStatsdAt( $statsdNamespaces );
/**
* Returns metric with cleared labels.
*
- * @return CounterMetric|GaugeMetric|TimingMetric|NullMetric
+ * @return self|NullMetric
*/
public function fresh();
}
diff --git a/includes/libs/Stats/Metrics/MetricTrait.php b/includes/libs/Stats/Metrics/MetricTrait.php
new file mode 100644
index 000000000000..d9f9b9ca5cf1
--- /dev/null
+++ b/includes/libs/Stats/Metrics/MetricTrait.php
@@ -0,0 +1,118 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ * @file
+ */
+
+declare( strict_types=1 );
+
+namespace Wikimedia\Stats\Metrics;
+
+use InvalidArgumentException;
+use Psr\Log\LoggerInterface;
+use Wikimedia\Stats\Exceptions\IllegalOperationException;
+
+/**
+ * Implementation code common to all metric types.
+ *
+ * @internal
+ * @since 1.43
+ */
+trait MetricTrait {
+ /** @var BaseMetricInterface */
+ private BaseMetricInterface $baseMetric;
+
+ /** @var LoggerInterface */
+ private LoggerInterface $logger;
+
+ /** @inheritDoc */
+ public function __construct( $baseMetric, $logger ) {
+ $this->baseMetric = $baseMetric;
+ $this->logger = $logger;
+ }
+
+ /** @inheritDoc */
+ public function getName(): string {
+ return $this->baseMetric->getName();
+ }
+
+ /** @inheritDoc */
+ public function getComponent(): string {
+ return $this->baseMetric->getComponent();
+ }
+
+ /** @inheritDoc */
+ public function getSamples(): array {
+ return $this->baseMetric->getSamples();
+ }
+
+ /** @inheritDoc */
+ public function getSampleCount(): int {
+ return $this->baseMetric->getSampleCount();
+ }
+
+ /** @inheritDoc */
+ public function getSampleRate(): float {
+ return $this->baseMetric->getSampleRate();
+ }
+
+ /** @inheritDoc */
+ public function setSampleRate( float $sampleRate ) {
+ try {
+ $this->baseMetric->setSampleRate( $sampleRate );
+ } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
+ // Log the condition and give the caller something that will absorb calls.
+ trigger_error( $ex->getMessage(), E_USER_WARNING );
+ return new NullMetric;
+ }
+ return $this;
+ }
+
+ /** @inheritDoc */
+ public function getLabelKeys(): array {
+ return $this->baseMetric->getLabelKeys();
+ }
+
+ /** @inheritDoc */
+ public function setLabel( string $key, string $value ) {
+ try {
+ $this->baseMetric->addLabel( $key, $value );
+ } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
+ // Log the condition and give the caller something that will absorb calls.
+ trigger_error( $ex->getMessage(), E_USER_WARNING );
+ return new NullMetric;
+ }
+ return $this;
+ }
+
+ /** @inheritDoc */
+ public function copyToStatsdAt( $statsdNamespaces ) {
+ try {
+ $this->baseMetric->setStatsdNamespaces( $statsdNamespaces );
+ } catch ( InvalidArgumentException $ex ) {
+ // Log the condition and give the caller something that will absorb calls.
+ trigger_error( $ex->getMessage(), E_USER_WARNING );
+ return new NullMetric;
+ }
+ return $this;
+ }
+
+ /** @inheritDoc */
+ public function fresh(): self {
+ $this->baseMetric->clearLabels();
+ return $this;
+ }
+}
diff --git a/includes/libs/Stats/Metrics/TimingMetric.php b/includes/libs/Stats/Metrics/TimingMetric.php
index 915dad1b2aa1..3894ab4a49ae 100644
--- a/includes/libs/Stats/Metrics/TimingMetric.php
+++ b/includes/libs/Stats/Metrics/TimingMetric.php
@@ -21,8 +21,6 @@ declare( strict_types=1 );
namespace Wikimedia\Stats\Metrics;
-use InvalidArgumentException;
-use Psr\Log\LoggerInterface;
use Wikimedia\Stats\Exceptions\IllegalOperationException;
use Wikimedia\Stats\Sample;
@@ -36,6 +34,7 @@ use Wikimedia\Stats\Sample;
* @since 1.38
*/
class TimingMetric implements MetricInterface {
+ use MetricTrait;
/**
* The StatsD protocol type indicator:
@@ -44,21 +43,9 @@ class TimingMetric implements MetricInterface {
*/
private const TYPE_INDICATOR = "ms";
- /** @var BaseMetricInterface */
- private BaseMetricInterface $baseMetric;
-
- /** @var LoggerInterface */
- private LoggerInterface $logger;
-
/** @var float|null */
private ?float $startTime = null;
- /** @inheritDoc */
- public function __construct( $baseMetric, $logger ) {
- $this->baseMetric = $baseMetric;
- $this->logger = $logger;
- }
-
/**
* Starts a timer.
*
@@ -137,79 +124,7 @@ class TimingMetric implements MetricInterface {
}
/** @inheritDoc */
- public function getName(): string {
- return $this->baseMetric->getName();
- }
-
- /** @inheritDoc */
- public function getComponent(): string {
- return $this->baseMetric->getComponent();
- }
-
- /** @inheritDoc */
public function getTypeIndicator(): string {
return self::TYPE_INDICATOR;
}
-
- /** @inheritDoc */
- public function getSamples(): array {
- return $this->baseMetric->getSamples();
- }
-
- /** @inheritDoc */
- public function getSampleCount(): int {
- return $this->baseMetric->getSampleCount();
- }
-
- /** @inheritDoc */
- public function getSampleRate(): float {
- return $this->baseMetric->getSampleRate();
- }
-
- /** @inheritDoc */
- public function setSampleRate( float $sampleRate ) {
- try {
- $this->baseMetric->setSampleRate( $sampleRate );
- } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function getLabelKeys(): array {
- return $this->baseMetric->getLabelKeys();
- }
-
- /** @inheritDoc */
- public function setLabel( string $key, string $value ) {
- try {
- $this->baseMetric->addLabel( $key, $value );
- } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function copyToStatsdAt( $statsdNamespaces ) {
- try {
- $this->baseMetric->setStatsdNamespaces( $statsdNamespaces );
- } catch ( InvalidArgumentException $ex ) {
- // Log the condition and give the caller something that will absorb calls.
- trigger_error( $ex->getMessage(), E_USER_WARNING );
- return new NullMetric;
- }
- return $this;
- }
-
- /** @inheritDoc */
- public function fresh(): TimingMetric {
- $this->baseMetric->clearLabels();
- return $this;
- }
}