Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Record handled exceptions #280

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Instrumentation/Symfony/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;
},
);
}
}
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
Loading