Skip to content

Conversation

@lucas-angeli-gimenes
Copy link
Collaborator

Description

This PR adds the HTTP response status code attribute to the trace span when an exception is thrown during request handling. Previously, when an exception occurred, the span would only record the exception but not include the HTTP status code, making it harder to correlate errors with their corresponding HTTP responses in observability tools.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Performance/Refactor
  • CI/CD/Chore

Checklist

  • Title follows Conventional Commits (e.g., feat: add SQS aspect)
  • Tests added/updated
  • composer test passes locally
  • Documentation updated (README/Docs)
  • No breaking changes (or documented if any)

Copilot AI review requested due to automatic review settings February 11, 2026 18:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds the http.response.status_code attribute to trace spans when exceptions occur during request handling. Previously, when an exception was thrown, the trace span would record the exception but omit the HTTP status code attribute, making it harder to correlate errors with their corresponding HTTP responses in observability tools.

Changes:

  • Modified TraceMiddleware to extract and record HTTP status codes from exceptions (500 for generic exceptions, actual status code for HttpException)
  • Added test coverage for both generic RuntimeException and HttpException scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Middleware/TraceMiddleware.php Added exception status code extraction logic and setAttributes call in catch block to record http.response.status_code
tests/Unit/Middleware/TraceMiddlewareTest.php Added HttpException import and new test case for HttpException, updated existing exception test to verify status code attribute

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 66 to 70
$statusCode = $exception instanceof HttpException ? $exception->getStatusCode() : 500;

$scope->setAttributes([
HttpAttributes::HTTP_RESPONSE_STATUS_CODE => $statusCode,
]);
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

The exception status code extraction logic is inconsistent with the approach used in MetricMiddleware. MetricMiddleware uses a helper method getHttpStatusCodeForException() that checks if the exception's code is a valid HTTP status code using Swoole\Http\Status::getReasonPhrase(). This approach handles a broader range of exceptions beyond just HttpException.

Consider extracting this logic into AbstractMiddleware or using a similar validation approach to maintain consistency across the codebase. The current implementation might miss exceptions that have valid HTTP status codes in their code property but are not instances of HttpException.

Copilot uses AI. Check for mistakes.
@lucas-angeli-gimenes lucas-angeli-gimenes merged commit 5e3fc39 into main Feb 12, 2026
@lucas-angeli-gimenes lucas-angeli-gimenes deleted the exception_status_code branch February 12, 2026 11:13
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.

5 participants