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

Add X-Xhprof-Enabled header support #5

Merged
merged 1 commit into from
Oct 18, 2023
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ To get started, install the package via composer:
```bash
composer require --dev maantje/xhprof-buggregator-laravel
```

## Usage

Set the buggregator endpoint in your environment file:
Expand All @@ -23,6 +24,13 @@ Enable Xhprof in your environment file when needed, but remember to disable it w
XHPROF_ENABLED=true
```

Alternatively, you can include the `X-Xhprof-Enabled` header in your request to explicitly enable or disable profiling for that specific call. When this header is present, it takes precedence over the environment variable.

Enabled values: `true` `1` `on` `yes`
Disabled values: `false` `0` `off` `no`

This feature works great with a browser extension like [ModHeader](https://addons.mozilla.org/en-US/firefox/addon/modheader-firefox/). It lets you switch profiling on and off right from your browser.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
14 changes: 13 additions & 1 deletion src/XhprofServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/xhprof.php', 'xhprof');

if (! config('xhprof.enabled')) {
if (! $this->isEnabled()) {
return;
}

Expand Down Expand Up @@ -62,4 +62,16 @@ public function boot(): void
__DIR__.'/../config/xhprof.php' => config_path('xhprof.php'),
]);
}

/**
* Checks if the profiler should be enabled
*/
private function isEnabled(): bool
{
if (request()->hasHeader(XhprofProfiler::HEADER)) {
return filter_var(request()->header(XhprofProfiler::HEADER), FILTER_VALIDATE_BOOLEAN);
}

return config()->get('xhprof.enabled');
}
}
2 changes: 2 additions & 0 deletions src/middleware/XhprofProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class XhprofProfiler
{
public const HEADER = 'X-Xhprof-Enabled';

public function __construct(private readonly Profiler $profiler)
{
//
Expand Down
15 changes: 15 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

use Illuminate\Http\Request;
use Maantje\XhprofBuggregatorLaravel\Tests\TestCase;
use Symfony\Component\HttpFoundation\HeaderBag;

uses(TestCase::class)->in(__DIR__);

function setXhprofEnabledHeader(string $value): void
{
$request = Request::create('/path');

$request->headers = new HeaderBag([
'X-Xhprof-Enabled' => $value,
]);

app()->bind('request', function () use ($request) {
return $request;
});
}
30 changes: 30 additions & 0 deletions tests/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
config()->set('xhprof.enabled', true);
});

it('does not register middleware when header is given', function () {
setXhprofEnabledHeader('false');

$provider = new XhprofServiceProvider(app());

$provider->register();

/** @var \Illuminate\Foundation\Http\Kernel $kernel */
$kernel = app(Kernel::class);

expect(
$kernel->hasMiddleware(XhprofProfiler::class)
)->toBeFalse();
});

it('registers middleware', function () {
$provider = new XhprofServiceProvider(app());

Expand All @@ -28,6 +43,21 @@
config()->set('xhprof.enabled', false);
});

it('registers middleware when header is given', function () {
setXhprofEnabledHeader('true');

$provider = new XhprofServiceProvider(app());

$provider->register();

/** @var \Illuminate\Foundation\Http\Kernel $kernel */
$kernel = app(Kernel::class);

expect(
$kernel->hasMiddleware(XhprofProfiler::class)
)->toBeTrue();
});

it('does not register middleware', function () {
$provider = new XhprofServiceProvider(app());

Expand Down