Skip to content

Commit

Permalink
Add X-Xhprof-Enabled header support
Browse files Browse the repository at this point in the history
  • Loading branch information
maantje committed Oct 18, 2023
1 parent 763b226 commit 4b0b043
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
6 changes: 6 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,11 @@ 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`

## 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

0 comments on commit 4b0b043

Please sign in to comment.