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', false),
'url' => env('BOOST_TELEMETRY_URL', 'https://boost.laravel.com/api/telemetry'),
],

];
3 changes: 3 additions & 0 deletions src/BoostServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Laravel\Boost\Install\GuidelineConfig;
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 @@ -67,6 +68,8 @@ public function register(): void
$app->make(Roster::class),
$app->make(GuidelineConfig::class)
));

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

public function boot(Router $router): void
Expand Down
36 changes: 36 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Laravel\Boost\Contracts\Agent;
Expand Down Expand Up @@ -101,6 +102,7 @@ public function handle(
$this->discoverEnvironment();
$this->collectInstallationPreferences();
$this->performInstallation();
$this->enableTelemetry();
$this->outro();

return self::SUCCESS;
Expand Down Expand Up @@ -193,6 +195,40 @@ protected function discoverTools(): array
return $tools;
}

protected function enableTelemetry(): void
{
$envPath = base_path('.env');

if (! File::exists($envPath)) {
return;
}

try {
$envContent = File::get($envPath);
$envKey = 'BOOST_TELEMETRY_ENABLED';

if (preg_match("/^{$envKey}=.*/m", $envContent)) {
return;
}

$enableTelemetry = confirm(
label: 'Enable anonymous telemetry to help improve Laravel Boost?',
hint: 'You can change this anytime by updating `BOOST_TELEMETRY_ENABLED` in your .env file'
);

$envValue = $enableTelemetry ? 'true' : 'false';
$status = $enableTelemetry ? 'enabled' : 'disabled';

$updatedContent = rtrim($envContent).PHP_EOL.PHP_EOL."{$envKey}={$envValue}".PHP_EOL;

File::put($envPath, $updatedContent);

$this->newLine();
} catch (Exception $exception) {
$this->warn("Unable to configure telemetry: {$exception->getMessage()}");
}
}

protected function outro(): void
{
$label = 'https://boost.laravel.com/installed';
Expand Down
4 changes: 4 additions & 0 deletions src/Mcp/Boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use InvalidArgumentException;
use Laravel\Boost\Mcp\Methods\CallToolWithExecutor;
use Laravel\Boost\Mcp\Methods\GetPromptWithTelemetry;
use Laravel\Boost\Mcp\Methods\ReadResourceWithTelemetry;
use Laravel\Boost\Mcp\Prompts\PackageGuidelinePrompt;
use Laravel\Boost\Mcp\Resources\PackageGuidelineResource;
use Laravel\Boost\Mcp\Tools\ApplicationInfo;
Expand Down Expand Up @@ -81,6 +83,8 @@ protected function boot(): void

// Override the tools/call method to use our ToolExecutor
$this->methods['tools/call'] = CallToolWithExecutor::class;
$this->methods['resources/read'] = ReadResourceWithTelemetry::class;
$this->methods['prompts/get'] = GetPromptWithTelemetry::class;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Mcp/Methods/GetPromptWithTelemetry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Laravel\Boost\Mcp\Methods;

use Generator;
use Laravel\Boost\Telemetry\TelemetryCollector;
use Laravel\Mcp\Server\Methods\GetPrompt;
use Laravel\Mcp\Server\ServerContext;
use Laravel\Mcp\Server\Transport\JsonRpcRequest;
use Laravel\Mcp\Server\Transport\JsonRpcResponse;

class GetPromptWithTelemetry extends GetPrompt
{
/**
* @return Generator<JsonRpcResponse>|JsonRpcResponse
*/
public function handle(JsonRpcRequest $request, ServerContext $context): Generator|JsonRpcResponse
{
$promptName = (string) $request->get('name');

try {
return parent::handle($request, $context);
} finally {
if (config('boost.telemetry.enabled')) {
app(TelemetryCollector::class)->recordPrompt($promptName);
}
}
}
}
32 changes: 32 additions & 0 deletions src/Mcp/Methods/ReadResourceWithTelemetry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Laravel\Boost\Mcp\Methods;

use Generator;
use Illuminate\Container\Container;
use Laravel\Boost\Telemetry\TelemetryCollector;
use Laravel\Mcp\Server\Methods\ReadResource;
use Laravel\Mcp\Server\ServerContext;
use Laravel\Mcp\Server\Transport\JsonRpcRequest;
use Laravel\Mcp\Server\Transport\JsonRpcResponse;

class ReadResourceWithTelemetry extends ReadResource
{
/**
* @return Generator<JsonRpcResponse>|JsonRpcResponse
*/
public function handle(JsonRpcRequest $request, ServerContext $context): Generator|JsonRpcResponse
{
$uri = (string) $request->get('uri');

try {
return parent::handle($request, $context);
} finally {
if (config('boost.telemetry.enabled')) {
Container::getInstance()->make(TelemetryCollector::class)->recordResource($uri);
}
}
}
}
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)->recordTool($toolClass, $wordCount);
}
}
}

Expand Down
144 changes: 144 additions & 0 deletions src/Telemetry/TelemetryCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?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 = [];

public array $resourceData = [];

public array $promptData = [];

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 recordTool(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];
}

public function recordResource(string $resourceUri): void
{
if (! $this->enabled) {
return;
}

$this->resourceData[$resourceUri] = ($this->resourceData[$resourceUri] ?? 0) + 1;
}

public function recordPrompt(string $promptName): void
{
if (! $this->enabled) {
return;
}

$this->promptData[$promptName] = ($this->promptData[$promptName] ?? 0) + 1;
}

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

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

try {
$this->client()
->timeout(5)
->post($this->url, ['data' => $this->buildPayload()]);
} catch (Throwable) {
//
} finally {
$this->toolData = [];
$this->resourceData = [];
$this->promptData = [];
$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(),
'resources' => $this->resourceData,
'prompts' => $this->promptData,
'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
Loading