perfbase / yii3
Yii 3 integration for the Perfbase profiling tool.
Requires
- php: >=8.2 <8.7
- perfbase/php-sdk: ^1.9
- psr/container: ^1.0 || ^2.0
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- symfony/event-dispatcher: ^7.0 || ^8.0
- yiisoft/yii-console: ^2.4
- yiisoft/yii-http: ^1.1
Requires (Dev)
- mockery/mockery: ^1.6
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5 || ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Perfbase for Yii 3
Yii 3 integration for Perfbase.
This package is a thin adapter over perfbase/php-sdk. It keeps the framework layer thin and pushes transport, extension access, and submission behavior into the shared SDK.
Scope
v1 supports:
- HTTP profiling through PSR-15 middleware
- Console command profiling through Symfony Console events
v1 does not support:
- Queue or worker profiling
- Scheduler-specific profiling
- Custom buffering or retries
- Yii-specific profiler UI
Requirements
- PHP
>=8.2 <8.7(Yii packages currently declare support up to PHP 8.5) yiisoft/yii-http^1.1yiisoft/yii-console^2.4perfbase/php-sdk^1.9- The Perfbase PHP extension installed for the target PHP runtime
Installation
composer require perfbase/yii3
The package ships Yii config-plugin files that are discovered automatically by yiisoft/config.
Override params['perfbase'] in your application config as needed, for example in config/common/params.php:
return [ 'perfbase' => [ 'enabled' => true, 'api_key' => $_ENV['PERFBASE_API_KEY'] ?? '', 'sample_rate' => 0.1, 'app_version' => '1.0.0', ], ];
Configuration
These are the effective defaults for params['perfbase']:
[
'enabled' => false,
'debug' => false,
'log_errors' => true,
'api_key' => '',
'api_url' => 'https://ingress.perfbase.cloud',
'sample_rate' => 0.1,
'profile_http_status_codes' => [...range(200, 299), ...range(500, 599)],
'timeout' => 10,
'proxy' => null,
'flags' => \Perfbase\SDK\FeatureFlags::DefaultFlags,
'app_version' => '',
'include' => [
'http' => ['*'],
'console' => ['*'],
],
'exclude' => [
'http' => [],
'console' => ['serve', 'queue:listen', 'queue:listen-all', 'queue:run', 'queue:consume'],
],
]
yiisoft/config merges list arrays by appending, so a list shipped in package params could never be narrowed by your application. The package params therefore leave out include, exclude and profile_http_status_codes; the values above apply in code when you do not set them, and whatever you set replaces them.
Notes:
sample_ratemust be numeric between0.0and1.0- values read from environment variables may be strings:
enabled,debugandlog_errorsaccepttrue/false/1/0/yes/no/on/off;flagsandtimeoutaccept digit strings; blankapi_url,proxy,flagsortimeoutfall back to the defaults flagsis the SDKFeatureFlagsbitmask. The defaultFeatureFlags::DefaultFlagsenables every feature exceptTrackCpuTimeandTrackMemoryAllocation.0enables every feature except the opt-in flagsUsePreciseClockandTrackCpuTime; a mask containing only opt-in flags adds them to that set, and any mask containing a regular flag is used exactlyprofile_http_status_codesdefaults to[...range(200, 299), ...range(500, 599)]. Add codes such as404if you want to keep them, or set it to[]to disable HTTP trace submission entirely.environmentcomes fromYII_ENV, otherwiseproductionapp_versionis application-defined
HTTP Integration
HTTP profiling is middleware-based through PerfbaseHttpMiddleware.php.
The package registers the middleware service through Yii config-plugin. Add it to your HTTP middleware pipeline where you want profiling to wrap application handling:
use Perfbase\Yii3\Middleware\PerfbaseHttpMiddleware; return [ PerfbaseHttpMiddleware::class, // other middleware... ];
Behavior:
- starts profiling before delegating to the next handler
- captures response status on success
- captures exception context on failure
- HTTP traces are only submitted when the response status code is in
profile_http_status_codes - always stops profiling in
finally
HTTP attributes include:
source=httpactionhttp_methodhttp_urlhttp_status_codeuser_ipuser_agentuser_idwhen availablehostnameenvironmentapp_versionphp_version
Route metadata behavior:
- when
yiisoft/routeris installed, the matched pattern fromYiisoft\Router\CurrentRoutebecomes the action (GET /articles/{id}). The router fills it while the inner handler runs, so it is read after the handler returns; include/exclude filters, which run before the handler, see the request path and any route attributes below - route template is otherwise resolved from request attributes such as
routePattern,route, or_route - route name is resolved from
routeNameorroute_name - controller metadata is resolved from
controlleroraction - without route metadata the action falls back to the request path
user_idis read from theuserId/user_idattributes, or fromgetId()/getUserIdentifier()on anidentity/userattributeuser_agentanduser_ipfall back to the PSR-7 request for worker runtimes where$_SERVERis not per requesthttp_urlis the full request URL including the query string, passed through the SDKUrlSanitizer: values of credential-looking parameters (token,password,api_key,signature,nonce, session IDs,email, and similar) becomeREDACTED, and user info and fragments are dropped.actionnever contains the query string
Console Integration
Console profiling is wired through ConsoleProfilerSubscriber.php, which subscribes to Symfony Console events used by Yii3 console packages.
The package registers console event listeners through Yii config-plugin, so no manual event-dispatcher wiring is required in standard Yii3 apps.
Subscribed events:
ConsoleEvents::COMMANDConsoleEvents::ERRORConsoleEvents::TERMINATE
Behavior:
- the span is named
cliand the action is the command name. Earlier releases named this spanartisan. - lifecycles are keyed by the input object identity
- long-running commands are excluded by default:
serve(yiisoft/yii-console) and the yiisoft/queue workers (queue:listen,queue:listen-all,queue:run,queue:consume). A trace started for these would only end when the process exits. If you setexclude.console, include these entries again - exceptions are attached on
ERROR - exit codes are attached on
TERMINATE - lifecycle state is cleaned up even when command execution fails
Filters
Supported contexts:
httpconsole
Supported filter styles:
*.*- glob patterns such as
cache/* - regex patterns such as
/^GET \/api\//
Example:
'perfbase' => [ 'include' => [ 'http' => ['route/*', '/^GET \\/api\\//'], 'console' => ['cache/*', 'migrate'], ], 'exclude' => [ 'http' => ['debug/*'], 'console' => ['help'], ], ],
Error Handling
The adapter is fail-open:
- SDK init failures degrade to a no-op
- missing extension degrades to a no-op
- disabled or filtered requests and commands never build the SDK client, so a disabled install without an API key logs nothing
- submit failures, including an exception thrown by the SDK's
submitTrace(), do not break request or command execution; the application's response is returned and its own exceptions are rethrown
Behavior:
debug=false: swallow and optionally log errorsdebug=true: rethrow adapter/runtime errors
Runtime Architecture
Primary entry points:
- package config files in
config/ ConfigProvider.phpPerfbaseHttpMiddleware.phpConsoleProfilerSubscriber.php- lifecycle classes in
src/Lifecycle - support helpers in
src/Support
ConfigProvider owns default configuration only. Yii integration wiring is delivered through config/params.php, config/di.php, config/di-web.php, and config/events-console.php.
The adapter does not implement its own delivery layer.
Development
Verify locally with:
composer install
composer run test
composer run phpstan
Limitations
- No queue support in v1
- No scheduler-specific integration in v1
- No Yii-specific debug toolbar integration
- HTTP middleware must be added to the host application's middleware pipeline
Documentation
Full documentation is available at perfbase.com/docs.
- Docs: perfbase.com/docs
- Issues: github.com/perfbaseorg/yii3/issues
- Support: support@perfbase.com
License
Apache-2.0. See LICENSE.txt.