From ba00256c48942136b638e79def93074fc08931e4 Mon Sep 17 00:00:00 2001 From: Graham Wharton Date: Thu, 3 Oct 2024 10:02:45 +0100 Subject: [PATCH] Added check to Instrumentation mapResult hook to check for presence of @metadata element in the Aws\Result object, as @metadata is not always set for all instrumented functions. --- src/Aws/src/AwsSdkInstrumentation.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Aws/src/AwsSdkInstrumentation.php b/src/Aws/src/AwsSdkInstrumentation.php index 32cd980de..b1496b152 100644 --- a/src/Aws/src/AwsSdkInstrumentation.php +++ b/src/Aws/src/AwsSdkInstrumentation.php @@ -109,9 +109,17 @@ public function activate(): bool /** @psalm-suppress PossiblyInvalidArgument */ $end_middleware = Middleware::mapResult(function (ResultInterface $result) { - $this->span->setAttributes([ - 'http.status_code' => $result['@metadata']['statusCode'], //@phan-suppress-current-line PhanTypeMismatchDimFetch - ]); + /** + * Some AWS SDK Funtions, such as S3Client->getObjectUrl() do not actually perform on the wire comms + * with AWS Servers, and therefore do not return with a populated AWS\Result object with valid @metadata + * Check for the presence of @metadata before extracting status code as these calls are still + * instrumented. + */ + if (isset($result['@metadata'])) { + $this->span->setAttributes([ + 'http.status_code' => $result['@metadata']['statusCode'], //@phan-suppress-current-line PhanTypeMismatchDimFetch + ]); + } $this->span->end(); $this->scope->detach();