Skip to content

Commit

Permalink
Laravel: ClientRequestWatcher reverted to request hashing, as the Req…
Browse files Browse the repository at this point in the history
…uest is immutable so may be a different object depending on middleware execution.
  • Loading branch information
ChrisLightfootWild committed Nov 16, 2023
1 parent a7610ba commit 1ef6e35
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, SpanInterface>
*/
protected array $spans = [];

public function __construct(
private CachedInstrumentation $instrumentation,
) {
$this->spans = new SplObjectStorage();
}

/** @psalm-suppress UndefinedInterfaceMethod */
Expand Down Expand Up @@ -57,26 +59,29 @@ 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;
}

$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;
}
Expand All @@ -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;
Expand Down

0 comments on commit 1ef6e35

Please sign in to comment.