diff --git a/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php b/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php index b2ee587e..782537c6 100644 --- a/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php @@ -8,23 +8,25 @@ use Illuminate\Http\Client\Events\ConnectionFailed; use Illuminate\Http\Client\Events\RequestSending; use Illuminate\Http\Client\Events\ResponseReceived; +use Illuminate\Http\Client\Request; +use Illuminate\Http\Client\Response; use OpenTelemetry\API\Instrumentation\CachedInstrumentation; use OpenTelemetry\API\Trace\SpanInterface; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\StatusCode; use OpenTelemetry\SemConv\TraceAttributes; -use SplObjectStorage; use Symfony\Component\HttpFoundation\Response as HttpResponse; class ClientRequestWatcher extends Watcher { - /** @var SplObjectStorage<\Illuminate\Http\Client\Request, SpanInterface> */ - protected SplObjectStorage $spans; + /** + * @var array + */ + protected array $spans = []; public function __construct( private CachedInstrumentation $instrumentation, ) { - $this->spans = new SplObjectStorage(); } /** @psalm-suppress UndefinedInterfaceMethod */ @@ -57,13 +59,14 @@ public function recordRequest(RequestSending $request): void TraceAttributes::SERVER_PORT => $parsedUrl['port'] ?? '', ]) ->startSpan(); - - $this->spans[$request->request] = $span; + $this->spans[$this->createRequestComparisonHash($request->request)] = $span; } public function recordConnectionFailed(ConnectionFailed $request): void { - $span = $this->spans[$request->request] ?? null; + $requestHash = $this->createRequestComparisonHash($request->request); + + $span = $this->spans[$requestHash] ?? null; if (null === $span) { return; } @@ -71,12 +74,14 @@ public function recordConnectionFailed(ConnectionFailed $request): void $span->setStatus(StatusCode::STATUS_ERROR, 'Connection failed'); $span->end(); - unset($this->spans[$request->request]); + unset($this->spans[$requestHash]); } public function recordResponse(ResponseReceived $request): void { - $span = $this->spans[$request->request] ?? null; + $requestHash = $this->createRequestComparisonHash($request->request); + + $span = $this->spans[$requestHash] ?? null; if (null === $span) { return; } @@ -89,9 +94,15 @@ public function recordResponse(ResponseReceived $request): void $this->maybeRecordError($span, $request->response); $span->end(); - unset($this->spans[$request->request]); + unset($this->spans[$requestHash]); } - private function maybeRecordError(SpanInterface $span, \Illuminate\Http\Client\Response $response): void + + private function createRequestComparisonHash(Request $request): string + { + return sha1($request->method() . '|' . $request->url() . '|' . $request->body()); + } + + private function maybeRecordError(SpanInterface $span, Response $response): void { if ($response->successful()) { return;