Skip to content
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,25 @@ FLUSH PRIVILEGES;
kubectl exec -n cloudshopt -it deploy/product-service -c app -- sh
# php artisan migrate
```

## GraphQL (Lighthouse)

This service exposes a GraphQL API (powered by Lighthouse) for querying product data.

### Endpoint
- `POST /api/products/graphql`

### Schema
```graphql
type Product {
id: ID!
name: String!
description: String
price: Float!
}

type Query {
products: [Product!]! @all
product(id: ID! @whereKey): Product @find
}
```
30 changes: 30 additions & 0 deletions app/Http/Controllers/MetricsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Controllers;

use Prometheus\CollectorRegistry;
use Prometheus\RenderTextFormat;

class MetricsController extends Controller
{
public function __invoke()
{
// $token = env('METRICS_BEARER_TOKEN');
// if ($token) {
// $auth = request()->header('Authorization', '');
// if (!preg_match('/^Bearer\s+(.+)$/i', $auth, $m) || $m[1] !== $token) {
// return response()->json(['message' => 'Unauthorized'], 401);
// }
// }

$registry = app(CollectorRegistry::class);

$renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());

return response($result, 200, [
'Content-Type' => RenderTextFormat::MIME_TYPE,
'Cache-Control' => 'no-store',
]);
}
}
52 changes: 52 additions & 0 deletions app/Http/Middleware/PrometheusMetrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Prometheus\CollectorRegistry;

class PrometheusMetrics
{
public function handle(Request $request, Closure $next)
{
$start = microtime(true);

$response = $next($request);

$duration = microtime(true) - $start;

$route = $request->route();
$routeLabel = $route?->getName()
?? $route?->uri()
?? 'unknown';

$method = $request->method();
$status = (string) $response->getStatusCode();
$service = config('app.name', 'service');

$registry = app(CollectorRegistry::class);

// Counter: request count
$counter = $registry->getOrRegisterCounter(
'cloudshopt',
'http_requests_total',
'Total HTTP requests',
['service', 'method', 'route', 'status']
);
$counter->inc([$service, $method, $routeLabel, $status]);

// Histogram: request latency
$hist = $registry->getOrRegisterHistogram(
'cloudshopt',
'http_request_duration_seconds',
'HTTP request duration in seconds',
['service', 'method', 'route', 'status'],
// buckets (sekunde)
[0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5]
);
$hist->observe($duration, [$service, $method, $routeLabel, $status]);

return $response;
}
}
23 changes: 16 additions & 7 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
Redis::setDefaultOptions([
'host' => env('REDIS_HOST'),
'port' => (int) env('REDIS_PORT', 6379),
'password' => env('REDIS_PASSWORD') ?: null,
'timeout' => 0.1,
'read_timeout' => 10,
'persistent_connections' => true,
'prefix' => config('app.name', 'service') . '_' . gethostname(),
]);


$this->app->singleton(CollectorRegistry::class, function () {
return new CollectorRegistry(new Redis());
});
}

/**
* Bootstrap any application services.
*/
public function boot(): void
{
//
Expand Down
3 changes: 2 additions & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Middleware\PrometheusMetrics;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
Expand All @@ -12,7 +13,7 @@
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
//
$middleware->append(PrometheusMetrics::class);
})
->withExceptions(function (Exceptions $exceptions): void {
//
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"php": "^8.2",
"laravel/framework": "^12.0",
"laravel/tinker": "^2.10.1",
"nuwave/lighthouse": "^6.64"
"nuwave/lighthouse": "^6.64",
"promphp/prometheus_client_php": "^2.14"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand Down
70 changes: 69 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
});

Route::get('/items', [ProductController::class, 'index']);
Route::get('/items/{id}', [ProductController::class, 'show']);
Route::get('/items/{id}', [ProductController::class, 'show']);
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use App\Http\Controllers\MetricsController;
use Illuminate\Support\Facades\Route;

Route::get('/healthz', fn () => response('ok', 200));

Route::get('/metrics', MetricsController::class);