Skip to content

Commit

Permalink
Merge pull request #26 from ekateiva/feature/wipe-storage-config
Browse files Browse the repository at this point in the history
Add config setting if the collector registry should be wiped
  • Loading branch information
freekmurze authored Jan 3, 2024
2 parents e54f128 + e802ff9 commit f8d2c63
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"require-dev": {
"laravel/horizon": "^5.16.1",
"laravel/pint": "^1.10",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^7",
"larastan/larastan": "^2.6.0",
"orchestra/testbench": "^8.5.5",
Expand Down
5 changes: 5 additions & 0 deletions config/prometheus.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
'actions' => [
'render_collectors' => Spatie\Prometheus\Actions\RenderCollectorsAction::class,
],

/**
* Allow storage to be wiped after a render of data in metrics controller.
*/
'wipe_storage_after_rendering' => false,
];
4 changes: 3 additions & 1 deletion src/Actions/RenderCollectorsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ protected function renderRegistry(): string

$result = $renderer->render($metricSamples);

$this->registry->wipeStorage();
if (config('prometheus.wipe_storage_after_rendering')) {
$this->registry->wipeStorage();
}

return $result;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Actions/RenderCollectorsActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Spatie\Prometheus\Tests\Actions;

use Prometheus\CollectorRegistry;
use Spatie\Prometheus\Actions\RenderCollectorsAction;

it('does not wipe storage by default', closure: function () {
$mock = $this->mock(CollectorRegistry::class, function (\Mockery\MockInterface $mock) {
$mock
->shouldReceive('getMetricFamilySamples')
->once()
->andReturn([]);

$mock->shouldNotReceive('wipeStorage');
});

$subject = new RenderCollectorsAction($mock);
$subject->execute([]);
});

it('wipes storage if config is set', closure: function () {
config()->set('prometheus.wipe_storage_after_rendering', true);

$mock = $this->mock(CollectorRegistry::class, function (\Mockery\MockInterface $mock) {
$mock
->shouldReceive('getMetricFamilySamples')
->once()
->andReturn([]);

$mock
->shouldReceive('wipeStorage')
->once();
});

$subject = new RenderCollectorsAction($mock);
$subject->execute([]);
});
24 changes: 23 additions & 1 deletion tests/Http/Controllers/PrometheusMetricsControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Spatie\Prometheus\Facades\Prometheus;
use Spatie\Prometheus\Tests\TestSupport\Actions\TestRenderCollectorsAction;

it('can render a simple gauge', closure: function () {
Prometheus::addGauge('my gauge', function () {
Expand Down Expand Up @@ -82,7 +83,7 @@
assertPrometheusResultsMatchesSnapshot();
});

it('will render the gauges on the correct urls', closure: function () {
it('will render the gauges on the default url', closure: function () {
config()->set('prometheus.urls', [
'default' => '/prometheus',
'alternative' => '/my-alternative-route',
Expand All @@ -94,6 +95,19 @@
Prometheus::addGauge('my alternative gauge', 123.45)->urlName('alternative');

assertPrometheusResultsMatchesSnapshot();
});

it('will render the gauges on the alternative url', closure: function () {
config()->set('prometheus.urls', [
'default' => '/prometheus',
'alternative' => '/my-alternative-route',
]);

$this->reloadServiceProvider();

Prometheus::addGauge('my default gauge', 123.45);
Prometheus::addGauge('my alternative gauge', 123.45)->urlName('alternative');

assertPrometheusResultsMatchesSnapshot('alternative');
});

Expand All @@ -102,3 +116,11 @@

assertPrometheusResultsMatchesSnapshot();
});

it('can replace default render collectors action', closure: function () {
config()->set('prometheus.actions.render_collectors', TestRenderCollectorsAction::class);

Prometheus::addGauge('my gauge', 123.45);

assertPrometheusResultsMatchesSnapshot();
});
17 changes: 17 additions & 0 deletions tests/TestSupport/Actions/TestRenderCollectorsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\Prometheus\Tests\TestSupport\Actions;

use Spatie\Prometheus\Actions\RenderCollectorsAction;

class TestRenderCollectorsAction extends RenderCollectorsAction
{
protected function renderRegistry(): string
{
$result = "# TestRenderCollectorsAction\n";

$result .= parent::renderRegistry();

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TestRenderCollectorsAction
# HELP app_my_gauge
# TYPE app_my_gauge gauge
app_my_gauge 123.45

0 comments on commit f8d2c63

Please sign in to comment.