-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from cego/lejo/prometheus-metrics
Metrics for prometheus
- Loading branch information
Showing
7 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
# request-insurance | ||
|
||
[![QA](https://github.com/cego/request-insurance/actions/workflows/quality-assurance.yml/badge.svg)](https://github.com/cego/request-insurance/actions/workflows/quality-assurance.yml) | ||
|
||
# Supported versions | ||
|
||
| Package version | PHP versions supported | Status | ||
|-----------------|------------------------|---| | ||
| ^1 | ^7.4,^8.0 | Security and bug fixes only | ||
| ^2 | ^8.3 | Active development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Cego\RequestInsurance; | ||
|
||
use Spatie\Prometheus\Prometheus; | ||
use Cego\RequestInsurance\Enums\State; | ||
use Cego\RequestInsurance\Models\RequestInsurance; | ||
|
||
class RequestInsuranceMetrics | ||
{ | ||
public function __construct( | ||
private Prometheus $prometheus | ||
) { | ||
} | ||
|
||
public function registerMetrics(): void | ||
{ | ||
$this->prometheus->addGauge('request_insurances_count') | ||
->namespace('request_insurance') | ||
->label('status') | ||
->value(fn () => [ | ||
[fn () => RequestInsurance::query()->where('state', State::FAILED)->count(), [State::FAILED]], | ||
[fn () => RequestInsurance::query()->where('state', State::PENDING)->count(), [State::PENDING]], | ||
[fn () => RequestInsurance::query()->where('state', State::READY)->count(), [State::READY]], | ||
[fn () => RequestInsurance::query()->where('state', State::PROCESSING)->count(), [State::PROCESSING]], | ||
[fn () => RequestInsurance::query()->where('state', State::WAITING)->count(), [State::WAITING]], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use Tests\TestCase; | ||
use Illuminate\Foundation\Application; | ||
use Spatie\Prometheus\PrometheusServiceProvider; | ||
use Cego\RequestInsurance\Models\RequestInsurance; | ||
|
||
class MetricsTest extends TestCase | ||
{ | ||
/** | ||
* Get package providers. | ||
* | ||
* @param Application $app | ||
* | ||
* @return array | ||
*/ | ||
protected function getPackageProviders($app): array | ||
{ | ||
return [ | ||
...parent::getPackageProviders($app), | ||
PrometheusServiceProvider::class, | ||
]; | ||
} | ||
|
||
public function test_it_gets_metrics_for_request_insurances_count() | ||
{ | ||
|
||
$response = $this->get('prometheus'); | ||
|
||
$response->assertStatus(200); | ||
|
||
// Assert that the response contains the metrics for request_insurances_count | ||
$response->assertSee('request_insurances_count{status="FAILED"} 0', false); | ||
$response->assertSee('request_insurances_count{status="PENDING"} 0', false); | ||
$response->assertSee('request_insurances_count{status="READY"} 0', false); | ||
$response->assertSee('request_insurances_count{status="PROCESSING"} 0', false); | ||
$response->assertSee('request_insurances_count{status="WAITING"} 0', false); | ||
} | ||
|
||
public function test_it_gets_metrics_for_request_insurances_count_with_data() | ||
{ | ||
RequestInsurance::factory()->createMany([ | ||
['state' => 'FAILED'], | ||
['state' => 'PENDING'], | ||
['state' => 'READY'], | ||
['state' => 'PROCESSING'], | ||
['state' => 'WAITING'], | ||
]); | ||
|
||
$response = $this->get('prometheus'); | ||
|
||
$response->assertStatus(200); | ||
|
||
// Assert that the response contains the metrics for request_insurances_count | ||
$response->assertSee('request_insurances_count{status="FAILED"} 1', false); | ||
$response->assertSee('request_insurances_count{status="PENDING"} 1', false); | ||
$response->assertSee('request_insurances_count{status="READY"} 1', false); | ||
$response->assertSee('request_insurances_count{status="PROCESSING"} 1', false); | ||
$response->assertSee('request_insurances_count{status="WAITING"} 1', false); | ||
} | ||
} |