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

Release v11.9.1 - Added API metrics tracking #2666

Merged
merged 3 commits into from
Jan 20, 2025
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
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http;

use App\Http\Middleware\ApiAuthentication;
use App\Http\Middleware\Api\ApiAuthentication;
use App\Http\Middleware\DebugBarMessageLogger;
use App\Http\Middleware\DebugInfoContextLogger;
use App\Http\Middleware\EncryptCookies;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Middleware;
namespace App\Http\Middleware\Api;

use App\Service\User\UserServiceInterface;
use Closure;
Expand Down
34 changes: 34 additions & 0 deletions app/Http/Middleware/Api/ApiMetrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Middleware\Api;

use App\Models\Metrics\Metric;
use App\Service\Metric\MetricServiceInterface;
use Auth;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ApiMetrics
{
public function __construct(private readonly MetricServiceInterface $metricService)
{
}

/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next): Response
{
if (!app()->runningUnitTests()) {
$this->metricService->storeMetricByModel(
Auth::user(),
Metric::CATEGORY_API_CALL,
$request->path(),
1
);
}

return $next($request);
}
}
5 changes: 3 additions & 2 deletions app/Models/Metrics/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class Metric extends Model
use HasGenericModelRelation;

public const CATEGORY_DUNGEON_ROUTE_MDT_COPY = 1;
public const CATEGORY_API_CALL = 10;

public const ALL_CATEGORIES = [
self::CATEGORY_DUNGEON_ROUTE_MDT_COPY,
self::CATEGORY_API_CALL,
];

public const TAG_MDT_COPY_VIEW = 'view';

public const TAG_MDT_COPY_VIEW = 'view';
public const TAG_MDT_COPY_EMBED = 'embed';

public const ALL_TAGS = [
Expand Down
5 changes: 3 additions & 2 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Http\Middleware\Api\ApiMetrics;
use App\Models\Laratrust\Role;
use App\Models\User;
use Illuminate\Cache\RateLimiting\Limit;
Expand Down Expand Up @@ -57,7 +58,7 @@ protected function mapWebRoutes(): void
protected function mapApiRoutes(): void
{
Route::prefix('api')
->middleware(['api', 'throttle:api-general'])
->middleware(['api', 'throttle:api-general', ApiMetrics::class])
->group(base_path('routes/api.php'));
}

Expand Down Expand Up @@ -108,7 +109,7 @@ private function configureApiRateLimiting(): void
return $this->noLimitForExemptionsApi($request) ?? Limit::perMinute(self::RATE_LIMIT_OVERRIDE_PER_MINUTE_API ?? 120)->by($this->userKey($request));
});
RateLimiter::for('api-combatlog-correct-event', function (Request $request) {
return $this->noLimitForExemptionsApi($request) ?? Limit::perMinute(self::RATE_LIMIT_OVERRIDE_PER_MINUTE_API ?? 5)->by($this->userKey($request));
return $this->noLimitForExemptionsApi($request) ?? Limit::perMinute(self::RATE_LIMIT_OVERRIDE_PER_MINUTE_API ?? 240)->by($this->userKey($request));
});
RateLimiter::for('api-create-dungeonroute-thumbnail', function (Request $request) {
return $this->noLimitForExemptionsApi($request) ?? Limit::perMinute(self::RATE_LIMIT_OVERRIDE_PER_MINUTE_API ?? 30)->by($this->userKey($request));
Expand Down
6 changes: 3 additions & 3 deletions app/Service/Metric/MetricService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public function storeMetric(?int $modelId, ?string $modelClass, int $category, s
]);
}

public function storeMetricByModel(Model $model, int $category, string $tag, int $value): Metric
public function storeMetricByModel(?Model $model, int $category, string $tag, int $value): Metric
{
return Metric::create([
'model_id' => $model->id,
'model_class' => $model::class,
'model_id' => $model?->id,
'model_class' => $model !== null ? $model::class : null,
'category' => $category,
'tag' => $tag,
'value' => $value,
Expand Down
2 changes: 1 addition & 1 deletion app/Service/Metric/MetricServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface MetricServiceInterface
{
public function storeMetric(?int $modelId, ?string $modelClass, int $category, string $tag, int $value): Metric;

public function storeMetricByModel(Model $model, int $category, string $tag, int $value): Metric;
public function storeMetricByModel(?Model $model, int $category, string $tag, int $value): Metric;

public function aggregateMetrics(): bool;
}
25 changes: 25 additions & 0 deletions database/seeders/releases/v11.9.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": 267,
"release_changelog_id": 274,
"version": "v11.9.1",
"title": "Added API metrics tracking",
"backup_db": 1,
"silent": 1,
"spotlight": 0,
"released": 0,
"created_at": "2025-01-20T21:38:53+00:00",
"updated_at": "2025-01-20T21:38:53+00:00",
"changelog": {
"id": 274,
"release_id": 267,
"description": null,
"changes": [
{
"release_changelog_id": 274,
"release_changelog_category_id": 12,
"ticket_id": 2664,
"change": "API calls are now tracked and graphed so I can get some insight in how it's being used."
}
]
}
}
Loading