Documentation menu
CakePHP Integration
The Perfbase CakePHP plugin provides automatic HTTP request profiling and CakePHP console command profiling. HTTP profiling works on CakePHP 4.4+ and 5.x. Console command profiling is automatic on CakePHP 5 and opt-in on CakePHP 4 through ProfiledCommand.
Installation
Install the Perfbase PHP extension first. This is what does the actual profiling:
bash -c "$(curl -fsSL https://cdn.perfbase.com/install.sh)"Then install the CakePHP plugin:
composer require perfbase/cakephp:^1.0Load the plugin in your application:
// src/Application.php
public function bootstrap(): void
{
parent::bootstrap();
$this->addPlugin(\Perfbase\CakePHP\PerfbasePlugin::class);
}Configuration
Configure the plugin via Configure::write() in your app config, or by creating a config/perfbase.php file in your application:
// config/app_local.php
'Perfbase' => [
'enabled' => true,
'api_key' => env('PERFBASE_API_KEY'),
'sample_rate' => 0.1,
],Or as a standalone config file:
// config/perfbase.php
return [
'enabled' => true,
'api_key' => env('PERFBASE_API_KEY'),
'sample_rate' => 0.1,
];Configuration is resolved in three layers (later wins): package defaults → your config/perfbase.php → Configure::write('Perfbase', ...) calls.
All options
| Key | Type | Default | Description |
|---|---|---|---|
enabled | bool | false | Master on/off switch. Requires api_key to be set. |
api_key | string|null | null | Your project API key. Required. |
api_url | string | https://ingress.perfbase.cloud | Ingestion endpoint. |
sample_rate | float | 0.1 | Fraction of eligible requests and commands to profile (0.0–1.0). 0.1 = 10%. |
profile_http_status_codes | int[] | 200..299, 500..599 | HTTP response status codes that should be submitted. |
timeout | int | 10 | HTTP timeout in seconds for trace submission. |
proxy | string|null | null | Optional proxy URL (http, https, socks5, socks5h). |
flags | int | DefaultFlags | SDK feature flags bitmask. See Feature flags. |
environment | string | production | Environment label recorded on traces. |
app_version | string | '' | Your application version, recorded on traces. |
include.http | string[] | ['*'] | HTTP request include filters. Requests must match these filters to be profiled. |
include.console | string[] | ['*'] | Console command include filters. Commands must match these filters to be profiled. |
exclude.http | string[] | [] | HTTP request exclude filters. Matching requests are skipped. |
exclude.console | string[] | [] | Console command exclude filters. Matching commands are skipped. |
debug | bool | false | When true, profiling errors are thrown instead of silently logged. |
log_errors | bool | true | Log profiling errors via error_log(). |
Include and exclude filters
Control which HTTP requests and console commands are profiled:
'Perfbase' => [
// ...
'include' => [
'http' => ['*'], // All routes (default)
'console' => ['*'], // All commands (default)
],
'exclude' => [
'http' => ['/health'],
'console' => ['server'],
],
],Pattern types
- Glob patterns:
/api/*,/admin/articles/*(matched viafnmatch()) - Regex patterns:
/^GET \/api\//(wrapped in forward slashes) - Catch-all:
*or.*matches everything
HTTP patterns are tested against multiple representations of the request: the route template (e.g., /articles/:id), the controller/action path, the raw URI, and {METHOD} {path}.
HTTP middleware
The plugin adds PerfbaseMiddleware to the middleware queue automatically when profiling is enabled. The middleware wraps each request in a profiling lifecycle:
- Creates a profiling lifecycle and starts the trace span.
- Executes the rest of the middleware stack and your controller.
- Records the HTTP status code from the response.
- Stops the span and submits the trace in a
finallyblock, so traces are submitted even when exceptions occur.
Each request uses the stable span name http. The matched route template is recorded in the action attribute when available (e.g., GET /articles/:id rather than GET /articles/42).
Automatic attributes
| Attribute | Source |
|---|---|
hostname | gethostname() |
php_version | PHP version |
environment | Config value |
app_version | Config value |
source | http |
action | e.g., GET /articles/:id |
http_method | Request method |
http_url | Scheme + host + path (no query string) |
http_status_code | Response status code |
user_ip | Client IP address |
user_agent | User agent string |
user_id | From the identity request attribute (compatible with CakePHP Authentication plugin) |
Console command profiling
CakePHP 5: automatic
On CakePHP 5, all commands are profiled automatically via Command.beforeExecute and Command.afterExecute event listeners. No code changes required.
Each command uses the stable span name artisan. The command name is recorded in the action attribute, and the exit code is recorded as an attribute.
CakePHP 4: opt-in
CakePHP 4 does not emit command lifecycle events, so profiling is opt-in. Extend ProfiledCommand instead of Command:
use Perfbase\CakePHP\Command\ProfiledCommand;
class ImportCommand extends ProfiledCommand
{
public function execute(Arguments $args, ConsoleIo $io): ?int
{
// Your command logic, profiled automatically
return self::CODE_SUCCESS;
}
}Sample rate
The sample rate controls what fraction of eligible HTTP requests and console commands are profiled:
'Perfbase' => [
'sample_rate' => 1.0, // Profile everything (development)
'sample_rate' => 0.1, // 10% of requests (production)
'sample_rate' => 0.01, // 1% of requests (high-traffic)
],When a unit of work isn’t sampled, no extension trace span is started.
Error handling
Profiling never crashes your application. In production (debug: false), all profiling errors are caught and optionally logged via error_log(). In development (debug: true), errors are thrown so you can catch configuration issues early.
If the Perfbase PHP extension is not installed, the plugin detects this gracefully and silently disables profiling.
Using the SDK directly
Access the SDK via PerfbaseLocator for manual profiling:
use Perfbase\CakePHP\Support\PerfbaseLocator;
$perfbase = PerfbaseLocator::get();
if ($perfbase) {
$perfbase->setAttribute('batch_size', (string) $count);
}See the PHP SDK docs for the full API reference.
Current scope
CakePHP queue profiling is not wired in v1. Transport, retry behavior, and buffering are delegated to the PHP SDK rather than reimplemented in this plugin.