Skip to content

feat(symfony): create root span for console command execution#661

Open
PuvaanRaaj wants to merge 1 commit into
open-telemetry:mainfrom
PuvaanRaaj:fix/1774-symfony-console-span
Open

feat(symfony): create root span for console command execution#661
PuvaanRaaj wants to merge 1 commit into
open-telemetry:mainfrom
PuvaanRaaj:fix/1774-symfony-console-span

Conversation

@PuvaanRaaj

Copy link
Copy Markdown
Contributor

The gap

The Symfony auto-instrumentation creates a default root span for HTTP requests (via the HttpKernel::handle hook), 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 own ConsoleEvents listener by hand.

This was reported in open-telemetry/opentelemetry-php#1774.

The fix

Adds a new ConsoleInstrumentation that hooks Symfony\Component\Console\Command\Command::run() and creates an INTERNAL root span for the command execution, mirroring the existing HTTP kernel instrumentation idiom (same CachedInstrumentation / hook() pre+post / Context::storage() scope handling):

  • Span name taken from the command name (falls back to command when unnamed).
  • code.* attributes (code.function.name, code.file.path, code.line.number) set like the other hooks.
  • Exit code recorded as symfony.console.exit_code.
  • Error status: sets StatusCode::STATUS_ERROR and records the exception on a thrown Throwable, and sets STATUS_ERROR for a non-success (non-zero) return value.

Wired into _register.php alongside the existing registrations, and symfony/console added to require-dev with the same ^5.4||^6.0 constraint used for symfony/messenger and symfony/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 opentelemetry extension loaded: full package suite passes (vendor/bin/phpunit — 25 tests, 136 assertions) and php-cs-fixer --dry-run reports no style violations.

@PuvaanRaaj
PuvaanRaaj requested a review from a team as a code owner July 18, 2026 08:03
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.77%. Comparing base (176b3fc) to head (1f450e0).

Additional details and impacted files

Impacted file tree graph

@@             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     
Flag Coverage Δ
Context/Swoole 0.00% <ø> (ø)
Exporter/Instana 49.80% <ø> (ø)
Instrumentation/AwsSdk 82.14% <ø> (ø)
Instrumentation/CakePHP 20.42% <ø> (ø)
Instrumentation/CodeIgniter 79.31% <ø> (ø)
Instrumentation/Curl 86.88% <ø> (ø)
Instrumentation/Doctrine 92.82% <ø> (ø)
Instrumentation/ExtAmqp 88.80% <ø> (ø)
Instrumentation/ExtRdKafka ?
Instrumentation/Guzzle 79.76% <ø> (ø)
Instrumentation/HttpAsyncClient 78.94% <ø> (ø)
Instrumentation/HttpConfig 28.76% <ø> (ø)
Instrumentation/IO 0.00% <ø> (ø)
Instrumentation/Laravel ?
Instrumentation/Magento2 88.12% <ø> (ø)
Instrumentation/MongoDB 76.84% <ø> (ø)
Instrumentation/MySqli ?
Instrumentation/OpenAIPHP 86.71% <ø> (ø)
Instrumentation/PostgreSql 91.36% <ø> (ø)
Instrumentation/Psr14 77.41% <ø> (ø)
Instrumentation/Psr15 89.74% <ø> (ø)
Instrumentation/Psr16 97.43% <ø> (ø)
Instrumentation/Psr18 79.41% <ø> (ø)
Instrumentation/Psr6 97.56% <ø> (ø)
Instrumentation/ReactPHP 99.41% <ø> (ø)
Instrumentation/Session 94.28% <ø> (ø)
Instrumentation/Slim 84.21% <ø> (ø)
Metrics/Runtime ?
Propagation/CloudTrace 90.69% <ø> (ø)
Propagation/Instana 98.07% <ø> (ø)
Propagation/ServerTiming 94.73% <ø> (ø)
Propagation/TraceResponse 94.73% <ø> (ø)
ResourceDetectors/Azure 91.66% <ø> (ø)
ResourceDetectors/DigitalOcean 100.00% <ø> (ø)
Sampler/Xray 78.38% <ø> (ø)
Shims/OpenTracing 92.99% <ø> (ø)
SqlCommenter 95.58% <ø> (ø)
Utils/Test 87.79% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 34 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 176b3fc...1f450e0. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +36 to +37
Command::class,
'run',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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
@PuvaanRaaj
PuvaanRaaj force-pushed the fix/1774-symfony-console-span branch from 5d1cc18 to 1f450e0 Compare July 18, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[opentelemetry-auto-symfony] missing console default root span creation

1 participant