Skip to content

Commit 4804079

Browse files
committed
Add and End2End test for tracing
1 parent 7dee116 commit 4804079

File tree

7 files changed

+138
-0
lines changed

7 files changed

+138
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App\Controller;
6+
7+
use Doctrine\DBAL\Connection;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class TracingController
11+
{
12+
/**
13+
* @var Connection
14+
*/
15+
private $connection;
16+
17+
public function __construct(Connection $connection)
18+
{
19+
$this->connection = $connection;
20+
}
21+
22+
public function pingDatabase(): Response
23+
{
24+
$this->connection->executeQuery('SELECT 1');
25+
26+
return new Response('Success');
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App;
6+
7+
use Symfony\Component\Config\Loader\LoaderInterface;
8+
9+
class KernelWithTracing extends Kernel
10+
{
11+
public function registerContainerConfiguration(LoaderInterface $loader): void
12+
{
13+
parent::registerContainerConfiguration($loader);
14+
15+
$loader->load(__DIR__ . '/tracing.yml');
16+
}
17+
}

tests/End2End/App/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ dispatch:
2929
dispatch_unrecoverable:
3030
path: /dispatch-unrecoverable-message
3131
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\MessengerController::dispatchUnrecoverableMessage' }
32+
33+
tracing_ping_database:
34+
path: /tracing/ping-database
35+
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\TracingController::pingDatabase' }

tests/End2End/App/tracing.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sentry:
2+
tracing:
3+
enabled: true
4+
options:
5+
traces_sample_rate: 1
6+
7+
services:
8+
Sentry\SentryBundle\Tests\End2End\App\Controller\TracingController:
9+
arguments:
10+
- '@doctrine.dbal.default_connection'
11+
tags:
12+
- controller.service_arguments

tests/End2End/End2EndTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Messenger\MessageBusInterface;
2020

2121
if (!class_exists(KernelBrowser::class)) {
22+
/** @phpstan-ignore-next-line */
2223
class_alias(Client::class, KernelBrowser::class);
2324
}
2425

tests/End2End/StubTransportFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function send(Event $event): PromiseInterface
3434
$message = $event->getMessage();
3535
} elseif ($event->getExceptions()) {
3636
$message = $event->getExceptions()[0]->getValue();
37+
} elseif ($event->getTransaction()) {
38+
$message = 'TRACING: ' . $event->getTransaction();
39+
foreach ($event->getSpans() as $i => $span) {
40+
$message .= \PHP_EOL . $i . ') ' . $span->getDescription();
41+
}
3742
} else {
3843
$message = 'NO MESSAGE NOR EXCEPTIONS';
3944
}

tests/End2End/TracingEnd2EndTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End;
6+
7+
use Sentry\SentryBundle\Tests\End2End\App\KernelWithTracing;
8+
use Sentry\State\HubInterface;
9+
use Symfony\Bundle\FrameworkBundle\Client;
10+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
11+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
12+
use Symfony\Component\HttpFoundation\Response;
13+
14+
if (!class_exists(KernelBrowser::class)) {
15+
/** @phpstan-ignore-next-line */
16+
class_alias(Client::class, KernelBrowser::class);
17+
}
18+
19+
class TracingEnd2EndTest extends WebTestCase
20+
{
21+
public const SENT_EVENTS_LOG = '/tmp/sentry_e2e_test_sent_events.log';
22+
23+
protected static function getKernelClass(): string
24+
{
25+
return KernelWithTracing::class;
26+
}
27+
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
file_put_contents(self::SENT_EVENTS_LOG, '');
33+
}
34+
35+
public function testTracingWithDoctrineConnectionPing(): void
36+
{
37+
$client = static::createClient(['debug' => false]);
38+
39+
$client->request('GET', '/tracing/ping-database');
40+
41+
$response = $client->getResponse();
42+
43+
$this->assertInstanceOf(Response::class, $response);
44+
$this->assertSame(200, $response->getStatusCode());
45+
46+
$this->assertLastEventIdIsNotNull($client);
47+
$this->assertTracingEventCount(1);
48+
}
49+
50+
private function assertLastEventIdIsNotNull(KernelBrowser $client): void
51+
{
52+
$container = $client->getContainer();
53+
$this->assertNotNull($container);
54+
55+
$hub = $container->get('test.hub');
56+
$this->assertInstanceOf(HubInterface::class, $hub);
57+
58+
$this->assertNotNull($hub->getLastEventId(), 'Last error not captured');
59+
}
60+
61+
private function assertTracingEventCount(int $expectedCount): void
62+
{
63+
$events = file_get_contents(self::SENT_EVENTS_LOG);
64+
$this->assertNotFalse($events, 'Cannot read sent events log');
65+
$listOfTracingEvents = array_filter(explode(StubTransportFactory::SEPARATOR, trim($events)), static function (string $elem) {
66+
return str_contains('TRACING', $elem);
67+
});
68+
69+
$this->assertCount($expectedCount, $listOfTracingEvents, 'Wrong number of tracing events sent: ' . \PHP_EOL . $events);
70+
}
71+
}

0 commit comments

Comments
 (0)