Skip to content

Commit

Permalink
feat: Track host with requests
Browse files Browse the repository at this point in the history
  • Loading branch information
owenconti committed May 17, 2022
1 parent a3ad66b commit b433113
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/LaravelServerAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public function logRequest(Request $request, Response $response): Analytics
$analytics = Analytics::create([
'user_id' => $userId,
'method' => $this->requestDetails->getMethod(),
'host' => $this->requestDetails->getHost(),
'path' => $this->requestDetails->getPath(),
'status_code' => $this->requestDetails->getStatusCode(),
'user_agent' => $this->requestDetails->getUserAgent(),
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getTable()
}

protected $fillable = [
'user_id', 'path', 'method', 'status_code', 'duration_ms', 'user_agent', 'query_params', 'ip_address'
'user_id', 'path', 'method', 'status_code', 'duration_ms', 'user_agent', 'query_params', 'ip_address', 'host'
];

protected $casts = [
Expand Down
10 changes: 10 additions & 0 deletions src/RequestDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function getMethod(): string
return strtoupper($this->request->getMethod());
}

/**
* Returns the host of the request.
*
* @return string
*/
public function getHost(): string
{
return $this->request->getHost();
}

/**
* Returns the path of the request.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

// phpcs:ignoreFile

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;

class AddHostToAnalyticsTable extends Migration
{
public function up()
{
Schema::table(ServerAnalytics::getAnalyticsDataTable(), function (Blueprint $table) {
$table->string('host')->nullable()->before('path');
});
}

public function down()
{
Schema::table(ServerAnalytics::getAnalyticsDataTable(), function (Blueprint $table) {
$table->dropColumn('host');
});
}
}

0 comments on commit b433113

Please sign in to comment.