msstc4symfony / profiling-bundle
Lightweight span profiling for Symfony: nested timings for requests, commands and code blocks
Package info
github.com/msstc4symfony/profiling-bundle
Type:symfony-bundle
pkg:composer/msstc4symfony/profiling-bundle
Requires
- php: >=8.2
- composer-runtime-api: >=2.2
- monolog/monolog: ^3.5
- psr/log: ^2.0|^3.0
- symfony/config: ^7.4|^8.0
- symfony/console: ^7.4|^8.0
- symfony/dependency-injection: ^7.4|^8.0
- symfony/event-dispatcher: ^7.4|^8.0
- symfony/framework-bundle: ^7.4|^8.0
- symfony/http-foundation: ^7.4|^8.0
- symfony/http-kernel: ^7.4|^8.0
- symfony/polyfill-php84: ^1.31
- symfony/service-contracts: ^3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.76
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: >=11.0
- rector/rector: ^2.1
- roave/security-advisories: dev-latest
Suggests
- doctrine/dbal: To profile DBAL queries (auto_spans.dbal)
- doctrine/doctrine-bundle: To register the DBAL query spans middleware on its connections (auto_spans.dbal)
- guzzlehttp/guzzle: To profile Guzzle requests (auto_spans.guzzle)
- msstc4symfony/metrics-bridge-profiling: To export span durations as Prometheus metrics
- msstc4symfony/sql-labeller: Needed by auto_spans.dbal for the statement type and table (register its vcs repository, see README)
- symfony/http-client: To profile Symfony HttpClient requests (auto_spans.http_client)
- symfony/messenger: To profile consumed messages (messages option)
- symfony/monolog-bundle: To write finished spans to the "profiling" log channel
Provides
None
Conflicts
- doctrine/dbal: <3.7
- doctrine/doctrine-bundle: <2.13
- guzzlehttp/guzzle: <7.8
- symfony/http-client: <7.4
- symfony/monolog-bundle: <3.10
- symfony/symfony: *
Replaces
None
README
Lightweight spans for Symfony applications: measure how long an HTTP request, a console
command, a DBAL query, an outgoing HTTP call or any block of code takes, nested into a tree,
and write each finished span to the profiling Monolog channel
(request orders > sql select orders, with duration in seconds and the span context).
Compatibility
| Bundle | PHP | Symfony |
|---|---|---|
| 2.2 | 8.2+ | 7.4, 8.x |
| 2.1 | 8.2+ | 7.4, 8.x |
| 2.0 | 8.2+ | 7.4, 8.x |
| 1.1 | 8.2+ | 7.4, 8.x |
| 1.0 | 8.4+ | 7.4, 8.x |
Symfony 8.x needs PHP 8.4; on PHP 8.2 and 8.3 Composer installs Symfony 7.4.
Installation
composer require msstc4symfony/profiling-bundle
auto_spans.dbal also needs msstc4symfony/sql-labeller:
composer require msstc4symfony/sql-labeller
Symfony Flex registers the bundle automatically; without Flex add it to config/bundles.php:
Msstc4Symfony\ProfilingBundle\ProfilingBundle::class => ['all' => true],
Declare the profiling log channel (doc/monolog.yaml) and route it to a handler.
Configuration
Nothing is profiled by default. config/packages/msstc4symfony_profiling.yaml:
msstc4symfony_profiling: # Main HTTP requests with these route names get a "request <route>" span. It starts on # kernel.request at priority 0 (after the router and the firewall) and ends on # kernel.terminate, i.e. after the response was sent. routes: ['api_orders_list'] # Console commands with these names get a "cli command <name>" span, from console.command # to console.terminate (kernel.reset in between, e.g. in messenger:consume, keeps it open). commands: ['app:import'] # Messages of these classes (or their parents/interfaces) get a "message <class>" span # while a worker handles them (needs symfony/messenger). messages: ['App\Message\ImportOrders'] # Span message prefixes; set at most one list. spans: # Only spans starting with one of these are recorded ([] records nothing). whitelist: ~ # Spans starting with one of these are dropped; everything else is recorded. blacklist: ['sql '] # Automatic spans for libraries, all off by default. Plain true/false only: an %env()% value is # rejected, the flag decides the container wiring. auto_spans: # "sql <type> <table>" around every Doctrine DBAL query (needs doctrine/dbal >= 3.7, DoctrineBundle # >= 2.13 and msstc4symfony/sql-labeller). dbal: false # Detached "http <METHOD> <host>" span for every Symfony HttpClient request (needs symfony/http-client # >= 7.4 and framework.http_client). http_client: false # Detached "http <METHOD> <host>" span for every request of a Guzzle client service (needs # guzzlehttp/guzzle >= 7.8). guzzle: false
Automatic spans
| Option | Span message | Context | End context |
|---|---|---|---|
dbal |
sql <select|insert|update|delete|other> <table> (unknown without a table), e.g. sql select orders |
connection (DoctrineBundle connection name), profiling_automatic |
exception (class) when the query throws |
http_client |
http <METHOD> <host>, e.g. http GET api.example.com |
url_path, profiling_automatic, profiling_outgoing_request, profiling_detached |
status; exception (class) on a transport or HTTP error; cancelled (before the headers) or body_cancelled + status (after them); stream_closed when a toStream() stream is closed; response_destroyed when the response is destroyed unfinished |
guzzle |
http <METHOD> <host> |
url_path, profiling_automatic, profiling_outgoing_request, profiling_detached |
status; exception (class) when the promise is rejected, plus status when the rejection carries a response; exception when the handler throws |
Enabling an option whose packages are missing is a configuration error ("msstc4symfony_profiling.auto_spans.<option>" needs <packages> installed.). With the packages present, dbal without DoctrineBundle registered and
doctrine.dbal configured, and http_client with framework.http_client disabled fail the container compilation
with a LogicException naming the option.
url_path is the path without the query, with ids replaced: UUIDs become :uuid, numbers :id, hex strings of 24+
characters :hash (/orders/42 → /orders/:id). Other secrets in path segments (tokens, e-mails) are kept: do not
put them in paths, or drop such spans with a decision maker.
- DBAL: a driver middleware per DoctrineBundle connection (tag
doctrine.middleware, priority -100: outside middlewares of the default priority 0, metrics-bundle's included), aroundConnection::query(),exec()andStatement::execute(). The span ends when the driver returns: fetching the rows is not included, and on failureexceptionnames the driver's exception class, before DBAL converts it.prepare()and transactions (beginTransaction(),commit(),rollBack()) get no span. Table and statement type come frommsstc4symfony/sql-labeller.spans.blacklist: ['sql ']drops every SQL span;['sql select ']only the SELECTs. - HttpClient: decorates
http_client.transport(decoration priority -25), so every client, default and scoped, gets one span with the resolved host. The span ends when the body was fully received (getContent(),toArray(), the last chunk ofstream(), the end oftoStream()), on a failure (also a transport error in the middle of atoStream()body), oncancel(), when thetoStream()stream is closed or when the response is destroyed. Nothing is read inrequest(): concurrent requests stay concurrent; arequest()that throws (invalid options) still gets a span withexception. Clients above the transport see each attempt: withretry_failedevery attempt is its own span, a retried one ending withbody_cancelledand its status (e.g. 503); responses served by the HttpClientcachingoption never reach the transport and get no span. - Guzzle: a handler-stack middleware added as the outermost one of every Guzzle client service (
unshift()), so one span covers redirects and retries. tracing-bundle pushes its middleware to the other end of the stack, so the order of the two bundles does not matter. Clients built fromGuzzleHttp\Client::__construct()get it through theirhandlerconfig, other clients withgetConfig()through a service configurator (an existing configurator is kept; also clients configured withmax_host_connections,max_total_connections,transport_sharingormultiplexand nohandler, which Guzzle 8 refuses next to a given handler); a client the bundle cannot reach, or whosehandleris not aHandlerStack, is named in the container compilation log and not profiled (at run time the configurator leaves such a client alone too). Only client services are seen: a client defined inline (inline_service()) as an argument of another service is not profiled. The span ends when the promise settles, forsend()and asynchronous pools alike; a promise never waited for keeps its span open untilendAll()orkernel.reset(in a long-running command withoutkernel.reset, such spans accumulate). - No feedback loop: spans the integrations open (
profiling_automaticin their context,DetachedSpanFactoryInterface::AUTOMATIC) are not recorded while end processors run or while a span is being created (decision makers, assemblers, create processors), so a log handler of theprofilingchannel or a decision maker reading its rules through DBAL or HTTP does not profile itself. - All three integrations reach the factory through the
DetachedSpanFactoryInterfaceservice. To decorate the factory, decorate theProfilingFactoryservice (every span then goes through the decorator) with a class that implementsDetachedSpanFactoryInterfaceand delegatescreateDetachedSpan()as well; decorating only theProfilingFactoryInterfacealias leaves the automatic spans undecorated.
Detached spans
An HTTP response completes after the code that sent the request went on, while the factory's span stack is strictly
nested. DetachedSpanFactoryInterface::createDetachedSpan() (implemented by ProfilingFactory and
NullProfilingFactory, autowirable) opens a span whose parent is the span open now, but which is not pushed on the
stack: spans opened meanwhile do not become its children, and ending its parent does not end it — a detached span
may end after its parent, the guarantees below about children ending with their parent do not apply to it.
Decision makers and create processors apply as for createSpan(); its context carries DetachedSpanFactoryInterface::DETACHED
({"profiling_detached": true}) so a create processor can recognise it. End it yourself, preferably with
endSpan(); detached spans still open are ended with IMPLICIT_END by endAll() and by kernel.reset (also
under a span kept by keepOpenOnReset()), including detached spans opened while those end the stack. End
processors that open a new span for every span they process would keep that going: after
ProfilingFactory::MAX_END_PASSES (64) rounds the factory stops and logs an error.
The spans of the http_client and guzzle integrations also carry DetachedSpanFactoryInterface::OUTGOING_REQUEST
({"profiling_outgoing_request": true}), so a create processor can tell an outgoing request from other detached work:
tracing-bridge-profiling, for one, reserves the trace span id of such a span for the request's traceparent.
Usage
use Msstc4Symfony\ProfilingBundle\Application\ProfilingFactoryInterface; final class OrderImporter { public function __construct(private ProfilingFactoryInterface $profiling) {} public function import(): void { $span = $this->profiling->createSpan('import orders', ['source' => 'erp']); // ... $span->end(['rows' => 120]); } }
Or use ProfilingFactoryOwnerTrait; in an autowired service: the factory is injected through
a #[Required] setter and falls back to a no-op factory outside the container.
- A span opened while another is open becomes its child; ending a parent first ends every
child still open (except detached spans), innermost first, at the parent's end time — when both the parent and the
child implement
EndableAtInterface(AbstractSpandoes; a create-processor wrapper should delegate it); otherwise the child ends when the factory gets to it, after the parent — with the contextProfilingFactoryInterface::IMPLICIT_END({"profiling_implicit_end": true}), as do spans closed byendAll()/kernel.reset.end()fixes the duration (monotonic clock); calling it again does nothing. - Spans rejected by the decision makers are
NullSpans: they keep nesting intact but are never passed to end processors. - Open spans are ended on
kernel.terminate/console.terminateand onkernel.reset; a running command's span (commands) and the spans below it survivekernel.reset(ProfilingFactoryInterface::keepOpenOnReset()). If you decorateProfilingFactoryInterface, delegatekeepOpenOnReset()to the inner factory, or the command span ends on the firstkernel.reset. - A failing assembler, decision maker, create or end processor never breaks the profiled
code: the exception is logged on the default
loggerand profiling carries on. An exception from your own end handler reaches the code that calledend()on that span (after every handler ran and the span was processed; only the first exception is rethrown). Framework code ends spans withProfilingFactoryInterface::endSpan(), which logs instead; so do implicit andendAll()endings.
Workers
kernel.reset runs after every consumed message and ends every open span except the span of
a command listed in commands: a messenger:consume entry measures the whole run, until
console.terminate. Profile each message with messages; its span becomes a child of that
command span when both are configured. Spans left open by a handler end with the message
span or, without one, with the next kernel.reset. A message span ends with the message's own
handled/failed event; batch handlers acknowledge later, so their span ends when the worker
moves on (WorkerRunningEvent, before kernel.reset), with message_acknowledged: false.
Failures add message_failed and message_will_retry. Vetoed messages are not profiled. This
assumes Messenger's default synchronous execution; with an asynchronous execution strategy
(Symfony 8.1+) handled events come after WorkerRunningEvent, so message spans would only
measure the dispatch.
Extension points (autoconfigured by interface)
| Interface | Role |
|---|---|
AllowSpanDecisionMakerInterface |
votes whether a span is recorded (true/false/null = abstain); asked by priority (#[AsTaggedItem(priority: ...)]), the first vote wins. The built-in list maker runs last (-1024) and abstains when no list is set |
SpanAssemblerInterface |
builds the span object for a message |
CreateSpanProcessorInterface |
can enrich or wrap a span when it is created. A wrapper must delegate end(), isEnded() and addEndHandler() to the wrapped span, and should implement EndableAtInterface by delegating too (so its children and itself end at their parent's end time); the factory binds its end handler to the returned span |
EndSpanProcessorInterface |
receives every recorded span once, after it ended (LoggerProcessor is built in; msstc4symfony/metrics-bridge-profiling adds Prometheus durations). Order with #[AsTaggedItem(priority: ...)] |
Do not remove the factory's own end handler (getEndHandlers() lists it): such a span is
never processed and is dropped from the stack.
Span messages become log messages and, with metrics-bridge-profiling, Prometheus label values: keep
them low-cardinality (request orders_show, not request /orders/42) and put ids into the
context.
Public API
Applications and bridges use or implement only these; every other class is @internal and may change in a minor
release.
Namespace Msstc4Symfony\ProfilingBundle\ |
Classes |
|---|---|
Application |
ProfilingFactoryInterface, DetachedSpanFactoryInterface, ProfilingFactory, NullProfilingFactory, ProfilingFactoryOwnerTrait |
Application\Span |
SpanInterface, EndableAtInterface, AbstractSpan, Span, NullSpan |
Application\Assembler |
SpanAssemblerInterface, SpanAssembler |
Application\DecisionMaker\AllowSpan |
AllowSpanDecisionMakerInterface |
Application\Processor\CreateSpan |
CreateSpanProcessorInterface |
Application\Processor\EndSpan |
EndSpanProcessorInterface |
| (root) | ProfilingBundle |
The configuration keys under msstc4symfony_profiling (see Configuration) are public too.
Local development
COMPOSER=composer-ci.json composer install
COMPOSER=composer-ci.json make check
make test
make fix
License
MIT, see LICENSE.