Skip to content

Commit dc4603b

Browse files
committed
feat(tracer): implement proper SQL connection retry logic
Signed-off-by: Alexandre Rulleau <[email protected]>
1 parent 88ce401 commit dc4603b

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

tests/Integrations/Laravel/Octane/Latest/CommonScenariosTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public function testScenarioGetReturnString()
9292
}
9393
}
9494

95+
$this->assertNotNull(
96+
$webRequestTrace,
97+
'Expected to find a laravel.request trace with App\\Http\\Controllers in resource, but none was found. '
98+
. 'Available traces: ' . json_encode(array_map(function($t) {
99+
return ['name' => $t[0]['name'] ?? 'N/A', 'resource' => $t[0]['resource'] ?? 'N/A'];
100+
}, $traces))
101+
);
102+
95103
$this->assertFlameGraph([$webRequestTrace], [
96104
SpanAssertion::build(
97105
'laravel.request',
@@ -134,7 +142,12 @@ public function testScenarioGetWithView()
134142

135143
foreach ($traces as $trace) {
136144
foreach ($trace as $span) {
137-
if ($span && isset($span["name"]) && $span["name"] === "laravel.request") {
145+
if ($span
146+
&& isset($span["name"])
147+
&& $span["name"] === "laravel.request"
148+
&& isset($span["resource"])
149+
&& str_contains($span["resource"], 'App\\Http\\Controllers')
150+
) {
138151
return true;
139152
}
140153
}
@@ -160,6 +173,14 @@ public function testScenarioGetWithView()
160173
}
161174
}
162175

176+
$this->assertNotNull(
177+
$webRequestTrace,
178+
'Expected to find a laravel.request trace with App\\Http\\Controllers in resource, but none was found. '
179+
. 'Available traces: ' . json_encode(array_map(function($t) {
180+
return ['name' => $t[0]['name'] ?? 'N/A', 'resource' => $t[0]['resource'] ?? 'N/A'];
181+
}, $traces))
182+
);
183+
163184
$this->assertFlameGraph([$webRequestTrace], [
164185
SpanAssertion::build(
165186
'laravel.request',
@@ -219,7 +240,12 @@ public function testScenarioGetWithException()
219240

220241
foreach ($traces as $trace) {
221242
foreach ($trace as $span) {
222-
if ($span && isset($span["name"]) && $span["name"] === "laravel.request") {
243+
if ($span
244+
&& isset($span["name"])
245+
&& $span["name"] === "laravel.request"
246+
&& isset($span["resource"])
247+
&& str_contains($span["resource"], 'App\\Http\\Controllers')
248+
) {
223249
return true;
224250
}
225251
}
@@ -245,6 +271,14 @@ public function testScenarioGetWithException()
245271
}
246272
}
247273

274+
$this->assertNotNull(
275+
$webRequestTrace,
276+
'Expected to find a laravel.request trace with App\\Http\\Controllers in resource, but none was found. '
277+
. 'Available traces: ' . json_encode(array_map(function($t) {
278+
return ['name' => $t[0]['name'] ?? 'N/A', 'resource' => $t[0]['resource'] ?? 'N/A'];
279+
}, $traces))
280+
);
281+
248282
$this->assertFlameGraph([$webRequestTrace], [
249283
SpanAssertion::build(
250284
'laravel.request',
@@ -312,6 +346,14 @@ public function testScenarioGetToMissingRoute()
312346
}
313347
}
314348

349+
$this->assertNotNull(
350+
$webRequestTrace,
351+
'Expected to find a laravel.request trace, but none was found. '
352+
. 'Available traces: ' . json_encode(array_map(function($t) {
353+
return ['name' => $t[0]['name'] ?? 'N/A', 'resource' => $t[0]['resource'] ?? 'N/A'];
354+
}, $traces))
355+
);
356+
315357
$this->assertFlameGraph([$webRequestTrace], [
316358
SpanAssertion::build(
317359
'laravel.request',

tests/Integrations/SQLSRV/SQLSRVTest.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,41 @@ public function testNoFakeServices()
502502

503503
private function createConnection()
504504
{
505-
$conn = sqlsrv_connect(
506-
self::$host . ', ' . self::$port,
507-
[
508-
'PWD' => self::$password,
509-
'Database' => self::$db,
510-
'UID' => self::$user,
511-
'TrustServerCertificate' => true
512-
]
513-
);
505+
// Retry connection to handle SQL Server container startup time
506+
$maxAttempts = 30;
507+
$attempt = 0;
508+
$conn = false;
509+
$lastError = null;
510+
511+
while ($attempt < $maxAttempts && $conn === false) {
512+
$attempt++;
513+
$conn = sqlsrv_connect(
514+
self::$host . ', ' . self::$port,
515+
[
516+
'PWD' => self::$password,
517+
'Database' => self::$db,
518+
'UID' => self::$user,
519+
'TrustServerCertificate' => true,
520+
'LoginTimeout' => 2 // Shorter timeout per attempt
521+
]
522+
);
523+
524+
if ($conn === false) {
525+
$errors = sqlsrv_errors();
526+
$lastError = $errors ? json_encode($errors) : 'Unknown error';
527+
528+
// Only retry on connection errors (timeout, network, server not ready)
529+
if ($attempt < $maxAttempts) {
530+
usleep(500000); // 500ms between retries
531+
}
532+
}
533+
}
534+
535+
if ($conn === false) {
536+
throw new \RuntimeException(
537+
"Failed to connect to SQL Server after {$maxAttempts} attempts. Last error: {$lastError}"
538+
);
539+
}
514540

515541
return $conn;
516542
}

0 commit comments

Comments
 (0)