Skip to content

Commit 43c1642

Browse files
committed
test(laravel): cover concurrent identical requests in ClientRequestWatcher
Sends two identical requests through Http::pool() and asserts each gets its own span correctly matched to its own response, reproducing the span collision fixed in the previous commit.
1 parent 9450d93 commit 43c1642

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/Instrumentation/Laravel/tests/Integration/Http/ClientTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66

77
use GuzzleHttp\Exception\ConnectException;
88
use GuzzleHttp\Promise\RejectedPromise;
9+
use GuzzleHttp\Psr7\Request as Psr7Request;
10+
use GuzzleHttp\Psr7\Response as Psr7Response;
11+
use Illuminate\Http\Client\Events\ConnectionFailed;
12+
use Illuminate\Http\Client\Events\ResponseReceived;
13+
use Illuminate\Http\Client\Pool;
914
use Illuminate\Http\Client\Request;
15+
use Illuminate\Http\Client\Response;
16+
use Illuminate\Support\Facades\Event;
1017
use Illuminate\Support\Facades\Http;
1118
use OpenTelemetry\API\Trace\StatusCode;
1219
use OpenTelemetry\SDK\Trace\StatusData;
20+
use OpenTelemetry\SemConv\Attributes\HttpAttributes;
1321
use OpenTelemetry\SemConv\Attributes\UrlAttributes;
1422
use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration\TestCase;
1523

@@ -60,4 +68,51 @@ public function test_it_records_connection_failures(): void
6068
self::assertEquals('http://fail', $span->getAttributes()->get(UrlAttributes::URL_FULL));
6169
self::assertEquals(StatusData::create(StatusCode::STATUS_ERROR, 'Connection failed'), $span->getStatus());
6270
}
71+
72+
public function test_it_matches_spans_to_responses_for_concurrent_identical_requests(): void
73+
{
74+
Http::fake([
75+
'same.opentelemetry.io' => Http::sequence()
76+
->push(status: 200)
77+
->push(status: 500),
78+
]);
79+
80+
$responses = Http::pool(fn (Pool $pool) => [
81+
$pool->get('same.opentelemetry.io'),
82+
$pool->get('same.opentelemetry.io'),
83+
]);
84+
85+
self::assertEquals(200, $responses[0]->status());
86+
self::assertEquals(500, $responses[1]->status());
87+
88+
self::assertCount(2, $this->storage);
89+
90+
$firstSpan = $this->storage[0];
91+
$secondSpan = $this->storage[1];
92+
93+
self::assertEquals(200, $firstSpan->getAttributes()->get(HttpAttributes::HTTP_RESPONSE_STATUS_CODE));
94+
self::assertEquals(StatusCode::STATUS_UNSET, $firstSpan->getStatus()->getCode());
95+
96+
self::assertEquals(500, $secondSpan->getAttributes()->get(HttpAttributes::HTTP_RESPONSE_STATUS_CODE));
97+
self::assertEquals(StatusCode::STATUS_ERROR, $secondSpan->getStatus()->getCode());
98+
}
99+
100+
public function test_it_ignores_response_with_no_matching_request(): void
101+
{
102+
$request = new Request(new Psr7Request('GET', 'http://untracked.opentelemetry.io'));
103+
$response = new Response(new Psr7Response(200));
104+
105+
Event::dispatch(new ResponseReceived($request, $response));
106+
107+
self::assertCount(0, $this->storage);
108+
}
109+
110+
public function test_it_ignores_connection_failure_with_no_matching_request(): void
111+
{
112+
$request = new Request(new Psr7Request('GET', 'http://untracked.opentelemetry.io'));
113+
114+
Event::dispatch(new ConnectionFailed($request));
115+
116+
self::assertCount(0, $this->storage);
117+
}
63118
}

0 commit comments

Comments
 (0)