From a26e4611414baa2d64266b5cc84ecd99728f7c2c Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 17 Nov 2022 16:40:45 +0200 Subject: [PATCH 1/2] Added callable support --- README.md | 14 ++++++++------ composer.json | 1 + src/Exporter.php | 12 ++++++------ tests/MetricTest.php | 30 ++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 00279ce..d27e3f1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index 98e2d13..f01e8c7 100644 --- a/composer.json +++ b/composer.json @@ -47,6 +47,7 @@ "sort-packages": true }, "minimum-stability": "dev", + "prefer-stable": true, "extra": { "laravel": { "providers": [ diff --git a/src/Exporter.php b/src/Exporter.php index efc509f..9c90e16 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -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; } /** @@ -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( diff --git a/tests/MetricTest.php b/tests/MetricTest.php index 965b76a..9a11d56 100644 --- a/tests/MetricTest.php +++ b/tests/MetricTest.php @@ -109,4 +109,34 @@ 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); + } } From fd25c794e8c792bc2fd42b2c0f8687b0c580c23a Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 17 Nov 2022 16:41:50 +0200 Subject: [PATCH 2/2] Linting --- tests/MetricTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/MetricTest.php b/tests/MetricTest.php index 9a11d56..f9f6be5 100644 --- a/tests/MetricTest.php +++ b/tests/MetricTest.php @@ -127,6 +127,7 @@ public function test_response_with_callable_plain_text(): void Exporter::exportResponse(function () use (&$triggered) { $triggered = true; + return 'some-random-text'; });