Documentation menu
CodeIgniter 4 Integration
The Perfbase CodeIgniter 4 package provides automatic profiling for HTTP requests and Spark CLI commands. Supports CodeIgniter 4.3.8+.
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 CodeIgniter 4 package:
composer require perfbase/codeigniter4Run the install command to publish the config file and register the filter:
php spark perfbase:installThis publishes app/Config/Perfbase.php and patches your app/Config/Filters.php to add the perfbase filter alias and global before/after entries.
Configuration
Add your API key to .env and enable profiling:
perfbase.enabled = true
perfbase.apiKey = your-api-keyOr configure in the published app/Config/Perfbase.php:
namespace Config;
use Perfbase\CodeIgniter4\Config\Perfbase as BasePerfbase;
class Perfbase extends BasePerfbase
{
public bool $enabled = true;
public string $apiKey = 'your-api-key';
}Environment variables (set in .env) take precedence over class property defaults.
All options
| Env Variable | Type | Default | Description |
|---|---|---|---|
perfbase.enabled | bool | false | Master on/off switch. Requires apiKey to be set. |
perfbase.apiKey | string | '' | Your project API key. Required. |
perfbase.apiUrl | string | https://ingress.perfbase.cloud | Ingestion endpoint. |
perfbase.sampleRate | float | 0.1 | Fraction of requests to profile (0.0–1.0). 0.1 = 10%. |
perfbase.timeout | int | 10 | HTTP timeout in seconds for trace submission. |
perfbase.proxy | string|null | null | Optional proxy URL (http, https, socks5, socks5h). |
perfbase.flags | int | DefaultFlags | SDK feature flags bitmask. See Feature flags. |
perfbase.appVersion | string | '' | Your application version, recorded on traces. |
perfbase.debug | bool | false | When true, profiling errors are thrown instead of silently logged. |
perfbase.logErrors | bool | true | Log profiling errors via error_log(). |
perfbase.userResolver | string|null | null | Custom user resolver class or service name. See User identification. |
Include and exclude filters
Control which routes and commands are profiled via env variables or config properties:
perfbase.includeHttp = *
perfbase.excludeHttp = /health,/ready,/metrics
perfbase.includeConsole = *
perfbase.excludeConsole =Or in app/Config/Perfbase.php:
public array $includeHttp = ['*'];
public array $excludeHttp = ['/health', '/ready'];
public array $includeConsole = ['*'];
public array $excludeConsole = [];Pattern types
- Glob patterns:
/api/*,/admin/users/*(matched viafnmatch()) - Regex patterns:
/^GET \/api\//(wrapped in forward slashes) - Catch-all:
*or.*matches everything
HTTP patterns are tested against: the request path, {METHOD} {path}, the route pattern (e.g., users/(:num)), {METHOD} {routePattern}, the controller class, the method name, and Controller::method.
HTTP profiling
HTTP requests are profiled via a CI4 filter registered as a global before/after filter. The filter:
- Before: creates a profiling lifecycle using the matched route pattern and starts the trace span.
- After: records the HTTP status code, stops the span, and submits the trace.
If an uncaught exception occurs (skipping the after phase), the package’s exception handler intercepts it, finalizes the trace, and then delegates to CI4’s normal exception handling.
Each request gets a span named http.{METHOD}.{identifier} using the route pattern when available (e.g., http.GET./users/(:num) rather than http.GET./users/42).
Automatic attributes
| Attribute | Source |
|---|---|
hostname | gethostname() |
php_version | PHP version |
environment | CI4’s ENVIRONMENT constant |
app_version | Config value |
source | http |
action | e.g., GET /users/(:num) |
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 user resolver (see below) |
Spark CLI profiling
Spark commands are profiled automatically via command lifecycle events. The package decorates CI4’s Commands service to fire profiling events before and after each command.
Each command gets a span named console.{command} (e.g., console.migrate, console.db:seed). The exit code and any exceptions are recorded as attributes.
Uses the console include/exclude filter context.
User identification
The package resolves user IDs for HTTP traces using this priority:
- Custom resolver: set
perfbase.userResolverto a class name implementingUserResolverInterface, or a CI4 service name. - CodeIgniter Shield: if Shield is installed, the authenticated user is detected automatically.
- Request property: falls back to
$request->userif it exists.
Custom resolver example
namespace App\Support;
use CodeIgniter\HTTP\RequestInterface;
use Perfbase\CodeIgniter4\Contracts\UserResolverInterface;
class MyUserResolver implements UserResolverInterface
{
public function resolve(RequestInterface $request): ?string
{
return session('user_id') ? (string) session('user_id') : null;
}
}Configure it:
perfbase.userResolver = App\Support\MyUserResolverSample rate
The sample rate controls what fraction of requests are profiled:
perfbase.sampleRate = 1.0 # Profile everything (development)
perfbase.sampleRate = 0.1 # 10% of requests (production)
perfbase.sampleRate = 0.01 # 1% of requests (high-traffic)When a request isn’t sampled, no profiling overhead is incurred.
Doctor command
Verify your installation is correctly configured:
php spark perfbase:doctorThis checks: profiling enabled, API key set, SDK initialization, PHP extension availability, filter alias registered, global before/after filters, Shield detection, and worker-mode compatibility.
Error handling
Profiling never crashes your application. In production (perfbase.debug = false), all profiling errors are caught and optionally logged via error_log(). In development (perfbase.debug = true), errors are thrown so you can catch configuration issues early.
If the Perfbase PHP extension is not installed, the package detects this gracefully and silently disables profiling.