feat(symfony): create root span for console command execution#661
feat(symfony): create root span for console command execution#661PuvaanRaaj wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #661 +/- ##
============================================
- Coverage 81.69% 80.77% -0.92%
+ Complexity 1821 1448 -373
============================================
Files 129 95 -34
Lines 7144 5347 -1797
============================================
- Hits 5836 4319 -1517
+ Misses 1308 1028 -280 Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5d1cc18e62
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| Command::class, | ||
| 'run', |
There was a problem hiding this comment.
Capture the final console exit code
When a Symfony app has a ConsoleEvents::TERMINATE listener that changes the exit code, this span is already finishing in the Command::run() post hook, before Symfony dispatches that terminate event. Symfony documents that terminate listeners run after the command and may change the exit code, so a command returning 0 can later exit non-zero while this instrumentation exports symfony.console.exit_code=0 and leaves the span status unset; hook/defer to the application-level final exit code instead.
Useful? React with 👍 / 👎.
Auto-instrument Symfony console commands with an INTERNAL root span (name, exit code, exception/status on failure), matching the HTTP kernel instrumentation. Fixes open-telemetry/opentelemetry-php#1774. Risk-Level: medium AI-Agent: claude-opus-4-8
5d1cc18 to
1f450e0
Compare
The gap
The Symfony auto-instrumentation creates a default root span for HTTP requests (via the
HttpKernel::handlehook), but nothing equivalent existed for console commands. When a Symfony CLI command runs, no root span was created, so console executions produced no trace unless the user wired up their ownConsoleEventslistener by hand.This was reported in open-telemetry/opentelemetry-php#1774.
The fix
Adds a new
ConsoleInstrumentationthat hooksSymfony\Component\Console\Command\Command::run()and creates anINTERNALroot span for the command execution, mirroring the existing HTTP kernel instrumentation idiom (sameCachedInstrumentation/hook()pre+post /Context::storage()scope handling):commandwhen unnamed).code.*attributes (code.function.name,code.file.path,code.line.number) set like the other hooks.symfony.console.exit_code.StatusCode::STATUS_ERRORand records the exception on a thrownThrowable, and setsSTATUS_ERRORfor a non-success (non-zero) return value.Wired into
_register.phpalongside the existing registrations, andsymfony/consoleadded torequire-devwith the same^5.4||^6.0constraint used forsymfony/messengerandsymfony/http-client.Fixes open-telemetry/opentelemetry-php#1774
Testing
Added
ConsoleInstrumentationTest(integration) covering: a successful command produces one INTERNAL root span with the expected name and exit-code attribute; a non-zero exit code sets ERROR status; a throwing command records the exception and sets ERROR status with the exception message as description.Verified locally with the
opentelemetryextension loaded: full package suite passes (vendor/bin/phpunit— 25 tests, 136 assertions) andphp-cs-fixer --dry-runreports no style violations.