Skip to content

Commit

Permalink
maintenance: getLagTimes migrate to StatsFactory
Browse files Browse the repository at this point in the history
Creates two metrics, one emitting milliseconds for
backwards-compatibility and another emitting seconds, aligned with
Prometheus' upstream recommendations.

Update test.

Bug: T359382
Change-Id: Id5a22937b60a209b6ba46633879551d24cf93a45
  • Loading branch information
shdubsh committed May 15, 2024
1 parent c38486c commit 06e6bda
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
17 changes: 15 additions & 2 deletions maintenance/getLagTimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct() {
public function execute() {
$services = $this->getServiceContainer();
$lbFactory = $services->getDBLoadBalancerFactory();
$stats = $services->getStatsdDataFactory();
$stats = $services->getStatsFactory();
$lbsByType = [
'main' => $lbFactory->getAllMainLBs(),
'external' => $lbFactory->getAllExternalLBs()
Expand Down Expand Up @@ -71,7 +71,20 @@ public function execute() {

if ( $this->hasOption( 'report' ) ) {
$group = ( $type === 'external' ) ? 'external' : $cluster;
$stats->gauge( "loadbalancer.lag.$group.$host", (int)( $lag * 1e3 ) );

// $lag is the lag duration in seconds
// emit milliseconds for backwards-compatibility
$stats->getGauge( 'loadbalancer_lag_milliseconds' )
->setLabel( 'group', $group )
->setLabel( 'host', $host )
->copyToStatsdAt( "loadbalancer.lag.$group.$host" )
->set( (int)( $lag * 1e3 ) );

// emit seconds also to align with Prometheus' recommendations
$stats->getGauge( 'loadbalancer_lag_seconds' )
->setLabel( 'group', $group )
->setLabel( 'host', $host )
->set( (int)$lag );
}
}
}
Expand Down
40 changes: 22 additions & 18 deletions tests/phpunit/maintenance/GetLagTimesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace MediaWiki\Tests\Maintenance;

use IBufferingStatsdDataFactory;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\LBFactory;
use Wikimedia\Stats\StatsFactory;

/**
* @covers \GetLagTimes
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testReportedOutput( $lag, $expected ) {
$this->expectOutputRegex( $expected );
}

public function testSendsToStatsd() {
public function testStats() {
$lbFactory = $this->createMock( LBFactory::class );
$lbFactory->method( 'getAllMainLBs' )
->willReturn( [
Expand All @@ -65,25 +65,29 @@ public function testSendsToStatsd() {
] );
$this->setService( 'DBLoadBalancerFactory', $lbFactory );

// The Statsd service
$gaugeArgs = [
[ 'loadbalancer.lag.cluster1.localhost', 1000 ],
[ 'loadbalancer.lag.cluster2.localhost', 0 ],
[ 'loadbalancer.lag.external.localhost', 14000 ],
];
$stats = $this->createMock( IBufferingStatsdDataFactory::class );
$stats->expects( $this->exactly( 3 ) )
->method( 'gauge' )
->willReturnCallback( function ( $key, $value ) use ( &$gaugeArgs ): void {
[ $nextKey, $nextValue ] = array_shift( $gaugeArgs );
$this->assertSame( $nextKey, $key );
$this->assertSame( $nextValue, $value );
} );

$this->setService( 'StatsdDataFactory', $stats );
$dummyGauge = StatsFactory::newNull()->getGauge( 'dummy' );
$sfmock = $this->createConfiguredMock( StatsFactory::class, [
'getGauge' => $dummyGauge
] );
$this->setService( 'StatsFactory', $sfmock );

$this->maintenance->setOption( 'report', true );
$this->maintenance->execute();

$expectedSamples = [
// milliseconds
[ 'cluster1.localhost', 1000.0 ],
[ 'cluster2.localhost', 0.0 ],
[ 'external.localhost', 14000.0 ],
// seconds
[ 'cluster1.localhost', 1.0 ],
[ 'cluster2.localhost', 0.0 ],
[ 'external.localhost', 14.0 ],
];
foreach ( $dummyGauge->getSamples() as $sample ) {
$namespaced = implode( '.', $sample->getLabelValues() );
$this->assertContains( [ $namespaced, $sample->getValue() ], $expectedSamples );
}
}

}

0 comments on commit 06e6bda

Please sign in to comment.