Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdatePeriodicMetrics
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 initServices
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace WikimediaEvents\Maintenance;
4
5use InvalidArgumentException;
6use MediaWiki\Logger\LoggerFactory;
7use MediaWiki\Maintenance\Maintenance;
8use Psr\Log\LoggerInterface;
9use Wikimedia\Stats\StatsFactory;
10use WikimediaEvents\PeriodicMetrics\WikimediaEventsMetricsFactory;
11
12// @codeCoverageIgnoreStart
13$IP = getenv( 'MW_INSTALL_PATH' );
14if ( $IP === false ) {
15    $IP = __DIR__ . '/../../..';
16}
17require_once "$IP/maintenance/Maintenance.php";
18// @codeCoverageIgnoreEnd
19
20/**
21 * Generates snapshots for several metrics so that they can be pulled by Prometheus.
22 * Copied, with modification, from MediaModeration maintenance/updateMetrics.php
23 */
24class UpdatePeriodicMetrics extends Maintenance {
25
26    private StatsFactory $statsFactory;
27    private WikimediaEventsMetricsFactory $wikimediaEventsMetricsFactory;
28    private LoggerInterface $logger;
29
30    public function __construct() {
31        parent::__construct();
32        $this->requireExtension( 'WikimediaEvents' );
33
34        $this->addDescription( 'Generates a snapshot of several metrics which can then be pulled by Prometheus.' );
35        $this->addOption( 'verbose', 'Output values of metrics calculated. Default is to not output.' );
36        $this->addOption(
37            'global-metrics',
38            'Generates metrics which are global (i.e. not per-wiki). ' .
39            'Without specifying this option the script will only generate per-wiki metrics.'
40        );
41    }
42
43    /**
44     * Initialise dependencies (services and logger).
45     */
46    private function initServices(): void {
47        $this->statsFactory = $this->getServiceContainer()->getStatsFactory();
48        $this->wikimediaEventsMetricsFactory = $this->getServiceContainer()->get( 'WikimediaEventsMetricsFactory' );
49        $this->logger = LoggerFactory::getInstance( 'WikimediaEvents' );
50    }
51
52    /** @inheritDoc */
53    public function execute() {
54        $this->initServices();
55
56        if ( $this->hasOption( 'global-metrics' ) ) {
57            $metrics = $this->wikimediaEventsMetricsFactory->getAllGlobalMetrics();
58        } else {
59            $metrics = $this->wikimediaEventsMetricsFactory->getAllPerWikiMetrics();
60        }
61
62        foreach ( $metrics as $metricName ) {
63            try {
64                $metric = $this->wikimediaEventsMetricsFactory->newMetric( $metricName );
65            } catch ( InvalidArgumentException $_ ) {
66                $this->error( 'ERROR: Metric "' . $metricName . '" failed to be constructed' );
67                $this->logger->error(
68                    'Metric {metric_name} failed to be constructed.', [ 'metric_name' => $metricName ]
69                );
70                continue;
71            }
72
73            $gaugeMetric = $this->statsFactory
74                ->withComponent( 'WikimediaEvents' )
75                ->getGauge( $metric->getName() );
76
77            foreach ( $metric->getLabels() as $key => $value ) {
78                $gaugeMetric->setLabel( $key, $value );
79            }
80
81            $metricValue = $metric->calculate();
82            $gaugeMetric->set( $metricValue );
83
84            if ( $this->hasOption( 'verbose' ) ) {
85                $outputString = $metric->getName();
86                if ( count( $metric->getLabels() ) ) {
87                    $outputString .= ' with label(s) ' . implode( ',', $metric->getLabels() );
88                }
89                $outputString .= ' is ' . $metricValue . '.' . PHP_EOL;
90                $this->output( $outputString );
91            }
92        }
93    }
94}
95
96// @codeCoverageIgnoreStart
97$maintClass = UpdatePeriodicMetrics::class;
98require_once RUN_MAINTENANCE_IF_MAIN;
99// @codeCoverageIgnoreEnd