Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics for prometheus #114

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/quality-assurance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [7.4, 8.0, 8.1, 8.2]
php-version: [8.3]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
Expand All @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [7.4, 8.0, 8.1]
php-version: [8.3]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
Expand Down
7 changes: 7 additions & 0 deletions README.md
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": "^7.4|^8.0",
"php": "^8.3",
"ext-json": "*",
"ext-mbstring": "*",
"ext-pcntl": "*",
Expand All @@ -34,7 +34,8 @@
"require-dev": {
"cego/php-cs-fixer": "^2.0",
"orchestra/testbench": "^6.0.0|^7.0.0|^8.0.0|^9.0.0",
"open-telemetry/sdk": "^1.0"
"open-telemetry/sdk": "^1.0",
"spatie/laravel-prometheus": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/RequestInsurance/OpenTelemetry/_register.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
return;
}

if(class_exists(CachedInstrumentation::class) === false) {
if (class_exists(CachedInstrumentation::class) === false) {
return;
}

Expand Down
29 changes: 29 additions & 0 deletions src/RequestInsurance/RequestInsuranceMetrics.php
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]],
]);
}
}
5 changes: 5 additions & 0 deletions src/RequestInsurance/RequestInsuranceServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ protected function publishAndLoadPackageComponents(): void
EditApprovalsStatus::class,
PrettyPrintTextArea::class,
]);

// To avoid a hard dependency on spatie/prometheus-laravel and keep non-laravel and 7.4 support.
if (class_exists('Spatie\Prometheus\Prometheus')) {
$this->app->make(RequestInsuranceMetrics::class)->registerMetrics();
}
}

/**
Expand Down
63 changes: 63 additions & 0 deletions tests/Unit/MetricsTest.php
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);
}
}