Skip to content
Draft
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
16 changes: 16 additions & 0 deletions config/boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,20 @@

'browser_logs_watcher' => env('BOOST_BROWSER_LOGS_WATCHER', true),

/*
|--------------------------------------------------------------------------
| Telemetry
|--------------------------------------------------------------------------
|
| Boost collects anonymous usage telemetry to help improve the tool.
| Only tool names and invocation counts are collected - no file paths,
| code, or identifying information is ever sent to telemetry.
|
*/

'telemetry' => [
'enabled' => env('BOOST_TELEMETRY_ENABLED', true),
'url' => env('BOOST_TELEMETRY_URL', 'https://boost.laravel.com/api/telemetry'),
],

];
2 changes: 0 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector;
use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
Expand All @@ -17,7 +16,6 @@
ReadOnlyPropertyRector::class,
EncapsedStringsToSprintfRector::class,
DisallowedEmptyRuleFixerRector::class,
FunctionLikeToFirstClassCallableRector::class,
])
->withPreparedSets(
deadCode: true,
Expand Down
3 changes: 3 additions & 0 deletions src/BoostServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\View\Compilers\BladeCompiler;
use Laravel\Boost\Mcp\Boost;
use Laravel\Boost\Middleware\InjectBoost;
use Laravel\Boost\Telemetry\TelemetryCollector;
use Laravel\Mcp\Facades\Mcp;
use Laravel\Roster\Roster;

Expand Down Expand Up @@ -58,6 +59,8 @@ public function register(): void

return $roster;
});

$this->app->singleton(TelemetryCollector::class);
}

public function boot(Router $router): void
Expand Down
8 changes: 8 additions & 0 deletions src/Mcp/ToolExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Dotenv\Dotenv;
use Illuminate\Support\Env;
use Laravel\Boost\Telemetry\TelemetryCollector;
use Laravel\Mcp\Response;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
Expand Down Expand Up @@ -41,11 +42,14 @@ protected function executeInSubprocess(string $toolClass, array $arguments): Res
timeout: $this->getTimeout($arguments)
);

$wordCount = 0;

try {
$process->mustRun();

$output = $process->getOutput();
$decoded = json_decode($output, true);
$wordCount = str_word_count($output);

if (json_last_error() !== JSON_ERROR_NONE) {
return Response::error('Invalid JSON output from tool process: '.json_last_error_msg());
Expand All @@ -61,6 +65,10 @@ protected function executeInSubprocess(string $toolClass, array $arguments): Res
$errorOutput = $process->getErrorOutput().$process->getOutput();

return Response::error("Process tool execution failed: {$errorOutput}");
} finally {
if (config('boost.telemetry.enabled')) {
app(TelemetryCollector::class)->record($toolClass, $wordCount);
}
}
}

Expand Down
118 changes: 118 additions & 0 deletions src/Telemetry/TelemetryCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace Laravel\Boost\Telemetry;

use Composer\InstalledVersions;
use Laravel\Boost\Concerns\MakesHttpRequests;
use Throwable;

class TelemetryCollector
{
use MakesHttpRequests;

public array $toolData = [];

protected bool $enabled;

protected string $url;

protected string $sessionId;

protected string $laravelVersion;

protected float $sessionStartTime;

public function __construct()
{
$this->sessionStartTime = microtime(true);
$this->enabled = config('boost.telemetry.enabled', false);
if ($this->enabled) {
$this->url = config('boost.telemetry.url', 'https://boost.laravel.com/api/telemetry');
$this->sessionId = hash('sha256', base_path());
$this->laravelVersion = app()->version();
app()->terminating($this->flush(...));

if (extension_loaded('pcntl')) {
pcntl_async_signals(true);
pcntl_signal(SIGINT, $this->flush(...));
pcntl_signal(SIGTERM, $this->flush(...));
}
}
}

public function __destruct()
{
$this->flush();
}

public function record(string $toolName, int $wordCount): void
{
if (! $this->enabled) {
return;
}

if (! isset($this->toolData[$toolName])) {
$this->toolData[$toolName] = [];
}

$tokens = $this->calculateTokens($wordCount);
$this->toolData[$toolName][] = ['tokens' => $tokens];
}

protected function calculateTokens(int $wordCount): int
{
return (int) round($wordCount * 1.3);
}

public function flush(): void
{
if ($this->toolData === [] || ! $this->enabled) {
return;
}

try {
$this->client()
->timeout(5)
->post($this->url, ['data' => $this->buildPayload()]);
} catch (Throwable) {
//
} finally {
$this->toolData = [];
$this->sessionStartTime = microtime(true);
}
}

protected function buildPayload(): string
{
$version = InstalledVersions::getVersion('laravel/boost');
$sessionEndTime = microtime(true);

return base64_encode(json_encode([
'session_id' => $this->sessionId,
'boost_version' => $version,
'php_version' => PHP_VERSION,
'os' => PHP_OS_FAMILY,
'laravel_version' => $this->laravelVersion,
'session_start' => date('c', (int) $this->sessionStartTime),
'session_end' => date('c', (int) $sessionEndTime),
'tools' => $this->formatToolsData(),
'timestamp' => date('c'),
]));
}

protected function formatToolsData(): array
{
$formatted = [];

foreach ($this->toolData as $toolName => $invocations) {
$formatted[$toolName] = [];
foreach ($invocations as $index => $invocation) {
$formatted[$toolName][(string) ($index + 1)] = $invocation;
}
}

return $formatted;
}
}
Loading