| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
trait MetricTrait { |
| 35 |
|
| 36 |
private BaseMetricInterface $baseMetric; |
| 37 |
|
| 38 |
|
| 39 |
private LoggerInterface $logger; |
| 40 |
|
| 41 |
|
| 42 |
public function __construct( $baseMetric, $logger ) { |
| 43 |
$this->baseMetric = $baseMetric; |
| 44 |
$this->logger = $logger; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
public function getName(): string { |
| 49 |
return $this->baseMetric->getName(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
public function getComponent(): string { |
| 54 |
return $this->baseMetric->getComponent(); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
public function getSamples(): array { |
| 59 |
return $this->baseMetric->getSamples(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public function getSampleCount(): int { |
| 64 |
return $this->baseMetric->getSampleCount(); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
public function getSampleRate(): float { |
| 69 |
return $this->baseMetric->getSampleRate(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
public function setSampleRate( float $sampleRate ) { |
| 74 |
try { |
| 75 |
$this->baseMetric->setSampleRate( $sampleRate ); |
| 76 |
} catch ( IllegalOperationException | InvalidArgumentException $ex ) { |
| 77 |
|
| 78 |
trigger_error( $ex->getMessage(), E_USER_WARNING ); |
| 79 |
return new NullMetric; |
| 80 |
} |
| 81 |
return $this; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
public function getLabelKeys(): array { |
| 86 |
return $this->baseMetric->getLabelKeys(); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
public function setLabel( string $key, string $value ) { |
| 91 |
try { |
| 92 |
$this->baseMetric->addLabel( $key, $value ); |
| 93 |
} catch ( IllegalOperationException | InvalidArgumentException $ex ) { |
| 94 |
|
| 95 |
trigger_error( $ex->getMessage(), E_USER_WARNING ); |
| 96 |
return new NullMetric; |
| 97 |
} |
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
public function setLabels( array $labels ) { |
| 103 |
try { |
| 104 |
foreach ( $labels as $key => $value ) { |
| 105 |
$this->baseMetric->addLabel( $key, $value ); |
| 106 |
} |
| 107 |
} catch ( IllegalOperationException | InvalidArgumentException $ex ) { |
| 108 |
|
| 109 |
trigger_error( $ex->getMessage(), E_USER_WARNING ); |
| 110 |
return new NullMetric; |
| 111 |
} |
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
public function copyToStatsdAt( $statsdNamespaces ) { |
| 117 |
try { |
| 118 |
$this->baseMetric->setStatsdNamespaces( $statsdNamespaces ); |
| 119 |
} catch ( InvalidArgumentException $ex ) { |
| 120 |
|
| 121 |
trigger_error( $ex->getMessage(), E_USER_WARNING ); |
| 122 |
return new NullMetric; |
| 123 |
} |
| 124 |
return $this; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
public function fresh(): self { |
| 129 |
$this->baseMetric->clearLabels(); |
| 130 |
return $this; |
| 131 |
} |
| 132 |
} |