Skip to content

Commit

Permalink
feat: Record handled exceptions (#280)
Browse files Browse the repository at this point in the history
This change records exceptions even if they are handled by the Kernel.
This is handy for apps that have an error handler registered.
  • Loading branch information
cedricziel authored Jul 11, 2024
1 parent 59b1574 commit 4172d62
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/SymfonyInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,29 @@ public static function register(): void
$span->end();
}
);

hook(
HttpKernel::class,
'handleThrowable',
pre: static function (
HttpKernel $kernel,
array $params,
string $class,
string $function,
?string $filename,
?int $lineno,
): array {
/** @var \Throwable $throwable */
$throwable = $params[0];

Span::getCurrent()
->recordException($throwable, [
TraceAttributes::EXCEPTION_ESCAPED => true,
])
->setStatus(StatusCode::STATUS_ERROR, $throwable->getMessage());

return $params;
},
);
}
}
24 changes: 24 additions & 0 deletions tests/Integration/SymfonyInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenTelemetry\Tests\Instrumentation\Symfony\tests\Integration;

use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator;
use OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator;
use OpenTelemetry\SDK\Trace\ImmutableSpan;
Expand All @@ -18,6 +19,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;

Expand All @@ -40,6 +42,28 @@ public function test_http_kernel_handle_exception(): void
);
}

public function test_http_kernel_marks_root_as_erroneous(): void
{
$this->expectException(HttpException::class);
$kernel = $this->getHttpKernel(new EventDispatcher(), function () {
throw new HttpException(500, 'foo');
});
$this->assertCount(0, $this->storage);

$response = $kernel->handle(new Request(), HttpKernelInterface::MAIN_REQUEST, true);

$this->assertCount(1, $this->storage);
$this->assertSame(500, $this->storage[0]->getAttributes()->get(TraceAttributes::HTTP_RESPONSE_STATUS_CODE));

$this->assertSame(StatusCode::STATUS_ERROR, $this->storage[0]->getStatus()->getCode());

$this->assertArrayHasKey(
TraceResponsePropagator::TRACERESPONSE,
$response->headers->all(),
'traceresponse header is present if TraceResponsePropagator is present'
);
}

public function test_http_kernel_handle_attributes(): void
{
$kernel = $this->getHttpKernel(new EventDispatcher());
Expand Down

0 comments on commit 4172d62

Please sign in to comment.