Skip to content

Commit d898550

Browse files
authored
Merge pull request #50 from renoki-co/feature/callable-exports
[feature] Added callable support
2 parents dfbc682 + fd25c79 commit d898550

4 files changed

Lines changed: 46 additions & 12 deletions

File tree

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ To do so, use the `exportResponse` function. The function will return the respon
181181
```php
182182
use RenokiCo\LaravelExporter\Exporter;
183183

184-
Exporter::exportResponse(<<<'PROM'
185-
# TYPE openswoole_max_conn gauge
186-
openswoole_max_conn 256
187-
# TYPE openswoole_coroutine_num gauge
188-
openswoole_coroutine_num 1
189-
PROM;)
184+
Exporter::exportResponse(function () {
185+
return <<<'PROM'
186+
# TYPE openswoole_max_conn gauge
187+
openswoole_max_conn 256
188+
# TYPE openswoole_coroutine_num gauge
189+
openswoole_coroutine_num 1
190+
PROM;
191+
);
190192
```
191193

192194
When accessing the `/metrics` endpoint, the response will be the one declared earlier.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"sort-packages": true
4848
},
4949
"minimum-stability": "dev",
50+
"prefer-stable": true,
5051
"extra": {
5152
"laravel": {
5253
"providers": [

src/Exporter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ public static function metrics(array $classes)
9393
* Export a string as response to a group instead of the computed
9494
* metrics by the given collectors.
9595
*
96-
* @param string $group
97-
* @param string $text
96+
* @param string|callable $handler
97+
* @param string|callable $group
9898
* @return void
9999
*/
100-
public static function exportResponse(string $text, string $group = 'metrics')
100+
public static function exportResponse(string|callable $handler, string $group = 'metrics')
101101
{
102-
static::$plainTextResponses[$group] = $text;
102+
static::$plainTextResponses[$group] = $handler;
103103
}
104104

105105
/**
@@ -155,8 +155,8 @@ public static function run(string $group = 'metrics')
155155
*/
156156
public static function exportAsPlainText(string $group = 'metrics'): string
157157
{
158-
if ($text = static::$plainTextResponses[$group] ?? false) {
159-
return $text;
158+
if ($handler = static::$plainTextResponses[$group] ?? false) {
159+
return is_callable($handler) ? $handler() : $handler;
160160
}
161161

162162
return (new RenderTextFormat)->render(

tests/MetricTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,35 @@ public function test_unknown_group(): void
109109
$registry = Exporter::run('you-dont-know-me');
110110
self::assertNotNull($registry);
111111
}
112+
113+
public function test_response_with_callable_plain_text(): void
114+
{
115+
Exporter::metrics([OutsideMetric::class]);
116+
117+
Exporter::metric(OutsideMetric::class)->incBy(20);
118+
119+
$this->assertStringContainsString(
120+
'laravel_outside_metric{label="default-label"} 20',
121+
Exporter::exportAsPlainText()
122+
);
123+
124+
Exporter::metric(OutsideMetric::class)->incBy(20, ['label' => 'injected-value']);
125+
126+
$triggered = false;
127+
128+
Exporter::exportResponse(function () use (&$triggered) {
129+
$triggered = true;
130+
131+
return 'some-random-text';
132+
});
133+
134+
$this->assertFalse($triggered);
135+
136+
$this->assertStringContainsString(
137+
'some-random-text',
138+
Exporter::exportAsPlainText()
139+
);
140+
141+
$this->assertTrue($triggered);
142+
}
112143
}

0 commit comments

Comments
 (0)