Skip to content

Commit

Permalink
Merge pull request #50 from renoki-co/feature/callable-exports
Browse files Browse the repository at this point in the history
[feature] Added callable support
  • Loading branch information
rennokki authored Nov 17, 2022
2 parents dfbc682 + fd25c79 commit d898550
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,14 @@ To do so, use the `exportResponse` function. The function will return the respon
```php
use RenokiCo\LaravelExporter\Exporter;

Exporter::exportResponse(<<<'PROM'
# TYPE openswoole_max_conn gauge
openswoole_max_conn 256
# TYPE openswoole_coroutine_num gauge
openswoole_coroutine_num 1
PROM;)
Exporter::exportResponse(function () {
return <<<'PROM'
# TYPE openswoole_max_conn gauge
openswoole_max_conn 256
# TYPE openswoole_coroutine_num gauge
openswoole_coroutine_num 1
PROM;
);
```

When accessing the `/metrics` endpoint, the response will be the one declared earlier.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
Expand Down
12 changes: 6 additions & 6 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public static function metrics(array $classes)
* Export a string as response to a group instead of the computed
* metrics by the given collectors.
*
* @param string $group
* @param string $text
* @param string|callable $handler
* @param string|callable $group
* @return void
*/
public static function exportResponse(string $text, string $group = 'metrics')
public static function exportResponse(string|callable $handler, string $group = 'metrics')
{
static::$plainTextResponses[$group] = $text;
static::$plainTextResponses[$group] = $handler;
}

/**
Expand Down Expand Up @@ -155,8 +155,8 @@ public static function run(string $group = 'metrics')
*/
public static function exportAsPlainText(string $group = 'metrics'): string
{
if ($text = static::$plainTextResponses[$group] ?? false) {
return $text;
if ($handler = static::$plainTextResponses[$group] ?? false) {
return is_callable($handler) ? $handler() : $handler;
}

return (new RenderTextFormat)->render(
Expand Down
31 changes: 31 additions & 0 deletions tests/MetricTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,35 @@ public function test_unknown_group(): void
$registry = Exporter::run('you-dont-know-me');
self::assertNotNull($registry);
}

public function test_response_with_callable_plain_text(): void
{
Exporter::metrics([OutsideMetric::class]);

Exporter::metric(OutsideMetric::class)->incBy(20);

$this->assertStringContainsString(
'laravel_outside_metric{label="default-label"} 20',
Exporter::exportAsPlainText()
);

Exporter::metric(OutsideMetric::class)->incBy(20, ['label' => 'injected-value']);

$triggered = false;

Exporter::exportResponse(function () use (&$triggered) {
$triggered = true;

return 'some-random-text';
});

$this->assertFalse($triggered);

$this->assertStringContainsString(
'some-random-text',
Exporter::exportAsPlainText()
);

$this->assertTrue($triggered);
}
}

0 comments on commit d898550

Please sign in to comment.