Skip to content

Commit bca0929

Browse files
committed
wip
1 parent 9996f2d commit bca0929

File tree

9 files changed

+59
-50
lines changed

9 files changed

+59
-50
lines changed

config/prometheus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212

1313
'middleware' => [
14-
Spatie\Prometheus\Middleware\AllowIps::class,
14+
\Spatie\Prometheus\Http\Middleware\AllowIps::class,
1515
],
1616

1717
'actions' => [

phpunit.xml.dist

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
5-
backupGlobals="false"
6-
bootstrap="vendor/autoload.php"
7-
colors="true"
8-
processIsolation="false"
9-
stopOnFailure="false"
10-
executionOrder="random"
11-
failOnWarning="true"
12-
failOnRisky="true"
13-
failOnEmptyTestSuite="true"
14-
beStrictAboutOutputDuringTests="true"
15-
cacheDirectory=".phpunit.cache"
16-
backupStaticProperties="false"
17-
>
18-
<testsuites>
19-
<testsuite name="Spatie Test Suite">
20-
<directory>tests</directory>
21-
</testsuite>
22-
</testsuites>
23-
<coverage>
24-
<include>
25-
<directory suffix=".php">./src</directory>
26-
</include>
27-
<report>
28-
<html outputDirectory="build/coverage"/>
29-
<text outputFile="build/coverage.txt"/>
30-
<clover outputFile="build/logs/clover.xml"/>
31-
</report>
32-
</coverage>
33-
<logging>
34-
<junit outputFile="build/report.junit.xml"/>
35-
</logging>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<testsuites>
4+
<testsuite name="Spatie Test Suite">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<coverage>
9+
<report>
10+
<html outputDirectory="build/coverage"/>
11+
<text outputFile="build/coverage.txt"/>
12+
<clover outputFile="build/logs/clover.xml"/>
13+
</report>
14+
</coverage>
15+
<logging>
16+
<junit outputFile="build/report.junit.xml"/>
17+
</logging>
18+
<source>
19+
<include>
20+
<directory suffix=".php">./src</directory>
21+
</include>
22+
</source>
3623
</phpunit>

src/Collectors/Gauge.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\Prometheus\Collectors;
44

5+
use Closure;
56
use Illuminate\Support\Facades\App;
67
use Illuminate\Support\Str;
78
use Prometheus\CollectorRegistry;
@@ -10,15 +11,15 @@ class Gauge implements Collector
1011
{
1112
public function __construct(
1213
protected string $label,
14+
protected null|float|Closure $value = null,
1315
protected ?string $name = null,
1416
protected ?string $namespace = null,
1517
protected ?string $helpText = null,
16-
protected mixed $value = null,
1718
)
1819
{
1920
$this->name = $name ?? Str::slug($this->label, '_');
2021

21-
$this->namespace = strtolower(App::getNamespace());
22+
$this->namespace = Str::of(App::getNamespace())->slug()->lower();
2223
}
2324

2425
public function namespace(string $namespace): self
@@ -42,7 +43,7 @@ public function helpText(string $helpText): self
4243
return $this;
4344
}
4445

45-
public function value(mixed $value): self
46+
public function value(float|Closure $value): self
4647
{
4748
$this->value = $value;
4849

@@ -54,7 +55,7 @@ public function register(CollectorRegistry $registry): self
5455
$gauge = $registry->getOrRegisterGauge(
5556
$this->namespace,
5657
$this->name,
57-
$this->helpText,
58+
$this->helpText ?? '',
5859
);
5960

6061
$gauge->set(value($this->value));

src/Controllers/PrometheusMetricsController.php renamed to src/Http/Controllers/PrometheusMetricsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Spatie\Prometheus\Controllers;
3+
namespace Spatie\Prometheus\Http\Controllers;
44

55
use Prometheus\RenderTextFormat;
66
use Spatie\Prometheus\Prometheus;
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Spatie\Prometheus\Middleware;
3+
namespace Spatie\Prometheus\Http\Middleware;
44

55
use Closure;
66
use Illuminate\Http\Request;
@@ -10,7 +10,11 @@ class AllowIps
1010
{
1111
public function handle(Request $request, Closure $next)
1212
{
13-
$allowedIps = config('horizon-exporter.ip_whitelist');
13+
$allowedIps = config('horizon-exporter.ip_whitelist', []);
14+
15+
if (! count($allowedIps)) {
16+
return $next($request);
17+
}
1418

1519
if (IpUtils::checkIp($request->ip(), $allowedIps)) {
1620
return $next($request);

src/Prometheus.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class Prometheus
1313

1414
public function addGauge(
1515
string $label,
16+
null|float|callable $value = null,
1617
?string $name = null,
1718
?string $namespace = null,
1819
?string $helpText = null,
19-
mixed $value = null,
2020
): Gauge
2121
{
22-
$collector = new Gauge($label, $name, $namespace, $helpText, $value);
22+
$collector = new Gauge($label, $value, $name, $namespace, $helpText);
2323

2424
$this->registerCollector($collector);
2525

src/PrometheusServiceProvider.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Spatie\Prometheus;
44

5+
use Illuminate\Support\Facades\Route;
56
use Prometheus\CollectorRegistry;
67
use Prometheus\Storage\InMemory;
78
use Spatie\Health\Health;
89
use Spatie\LaravelPackageTools\Package;
910
use Spatie\LaravelPackageTools\PackageServiceProvider;
10-
use Spatie\Prometheus\Commands\PrometheusCommand;
11-
use Spatie\Prometheus\Controllers\PrometheusMetricsController;
12-
use \Illuminate\Support\Facades\Route;
13-
use Spatie\Prometheus\Middleware\AllowIps;
11+
use Spatie\Prometheus\Http\Controllers\PrometheusMetricsController;
12+
use Spatie\Prometheus\Http\Middleware\AllowIps;
1413

1514
class PrometheusServiceProvider extends PackageServiceProvider
1615
{

tests/ExampleTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
<?php
22

3-
it('can test', function () {
4-
expect(true)->toBeTrue();
3+
use Spatie\Prometheus\Facades\Prometheus;
4+
use function Spatie\Snapshots\assertMatchesSnapshot;
5+
6+
it('can render prometheus collectors', function () {
7+
Prometheus::addGauge('my gauge', function() {
8+
return 123.45;
9+
});
10+
11+
$content = $this
12+
->get('prometheus')
13+
->assertSuccessful()
14+
->content();
15+
16+
assertMatchesSnapshot($content);
517
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# HELP app_my_gauge
2+
# TYPE app_my_gauge gauge
3+
app_my_gauge 123.45
4+
# HELP php_info Information about the PHP environment.
5+
# TYPE php_info gauge
6+
php_info{version="8.2.0"} 1

0 commit comments

Comments
 (0)