Skip to content

Commit

Permalink
test(*): Add test for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
julienloizelet committed Jan 3, 2025
1 parent fe617fb commit 62727ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/LapiRemediation.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ public function pushUsageMetrics(
// We update the count of each origin/remediation, one by one
// because we want to handle the case where an origin/remediation/count has been updated
// between the time we get the count and the time we update it
// $delta is negative, so we decrement the count
$this->updateMetricsOriginsCount($origin, $remediation, $delta);
}
}
Expand Down Expand Up @@ -299,14 +300,14 @@ private function buildMetricsItems(array $originsCount): array
if ($count <= 0) {
continue;
}
// Count all processed metrics, even clean ones
// Count all processed metrics, even bypass ones
$processed += $count;
// Prepare data to update origins count item after processing
$originsToUpdate[$origin][$remediation] = -$count;
if (Constants::REMEDIATION_BYPASS === $remediation) {
continue;
}
// Create "dropped" metrics
// Create "dropped" metrics (all that is not a bypass)
$metricsItems[] = [
'name' => 'dropped',
'value' => $count,
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/LapiRemediationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,43 @@ public function testPushUsageMetricsInLiveMode($cacheType)

$result = $remediation->pushUsageMetrics('test-remediation-php-unit', 'v0.0.0', 'crowdsec-php-bouncer-unit-test');
$this->assertArrayHasKey('remediation_components', $result, 'Should return a remediation_components key');
$items = $result['remediation_components'][0]['metrics'][0]['items'];

$this->assertEquals(
$items[0],
[
'name' => 'dropped',
'value' => 1,
'unit' => 'request',
'labels' => [
'origin' => 'CAPI',
'remediation' => 'ban',
],
],
'Should have CAPI/ban metrics'. json_encode($items[0])
);
$this->assertEquals(
$items[1],
[
'name' => 'dropped',
'value' => 1,
'unit' => 'request',
'labels' => [
'origin' => 'lists:tor',
'remediation' => 'captcha',
],
],
'Should have lists:tor/captcha metrics'. json_encode($items[1])
);
$this->assertEquals(
$items[2],
[
'name' => 'processed',
'value' => 3,
'unit' => 'request',
],
'Should have processed metrics'. json_encode($items[2])
);
$firstPushTime = time();
$item = $this->cacheStorage->getItem(AbstractCache::CONFIG);
$configItem = $item->get();
Expand Down

0 comments on commit 62727ce

Please sign in to comment.