From 6ee70ac2d119f2df6941ec8161693544077fdf5e Mon Sep 17 00:00:00 2001 From: Michele Mancioppi Date: Mon, 31 Oct 2022 09:41:26 +0100 Subject: [PATCH] Implement logging and add a test for metadata endpoint failure --- src/Aws/src/Ecs/Detector.php | 20 ++++++++++++---- src/Aws/tests/Unit/Ecs/DetectorTest.php | 32 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Aws/src/Ecs/Detector.php b/src/Aws/src/Ecs/Detector.php index 86d21017..95948ed8 100644 --- a/src/Aws/src/Ecs/Detector.php +++ b/src/Aws/src/Ecs/Detector.php @@ -19,6 +19,7 @@ namespace OpenTelemetry\Aws\Ecs; +use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; use OpenTelemetry\SDK\Common\Attribute\Attributes; use OpenTelemetry\SDK\Resource\ResourceDetectorInterface; use OpenTelemetry\SDK\Resource\ResourceInfo; @@ -36,6 +37,8 @@ */ class Detector implements ResourceDetectorInterface { + use LogsMessagesTrait; + private const ECS_METADATA_KEY_V4 = 'ECS_CONTAINER_METADATA_URI_V4'; private const ECS_METADATA_KEY_V3 = 'ECS_CONTAINER_METADATA_URI'; @@ -86,9 +89,8 @@ public function __construct( public function getResource(): ResourceInfo { $metadataEndpointV4 = getenv(self::ECS_METADATA_KEY_V4); - // Check if running on ECS by looking for below environment variables + if (!$metadataEndpointV4 && !getenv(self::ECS_METADATA_KEY_V3)) { - // TODO: add 'Process is not running on ECS' when logs are added return ResourceInfoFactory::emptyResource(); } @@ -126,7 +128,7 @@ private function getContainerId(): ?string } } } catch (Throwable $e) { - // TODO: add 'Failed to read container ID' when logging is added + self::logDebug('Failed to read container ID', ['exception' => $e]); } return null; @@ -143,7 +145,11 @@ private function getMetadataEndpointV4Resource(): ResourceInfo ->createRequest('GET', $metadataEndpointV4); $containerResponse = $this->client->sendRequest($containerRequest); if ($containerResponse->getStatusCode() > 299) { - // TODO: Log error + self::logError(sprintf('Cannot retrieve container metadata from %s endpoint', $metadataEndpointV4), [ + 'status_code' => $containerResponse->getStatusCode(), + 'response_body' => $containerResponse->getBody()->getContents(), + ]); + return ResourceInfoFactory::emptyResource(); } @@ -151,7 +157,11 @@ private function getMetadataEndpointV4Resource(): ResourceInfo ->createRequest('GET', $metadataEndpointV4 . '/task'); $taskResponse = $this->client->sendRequest($taskRequest); if ($taskResponse->getStatusCode() > 299) { - // TODO: Log error + self::logError(sprintf('Cannot retrieve task metadata from %s endpoint', $metadataEndpointV4 . '/task'), [ + 'status_code' => $taskResponse->getStatusCode(), + 'response_body' => $taskResponse->getBody()->getContents(), + ]); + return ResourceInfoFactory::emptyResource(); } diff --git a/src/Aws/tests/Unit/Ecs/DetectorTest.php b/src/Aws/tests/Unit/Ecs/DetectorTest.php index 87bb3fc1..772130bb 100644 --- a/src/Aws/tests/Unit/Ecs/DetectorTest.php +++ b/src/Aws/tests/Unit/Ecs/DetectorTest.php @@ -256,6 +256,38 @@ public function TestReturnEmptyResourceInvalidContainerIdAndHostname() putenv(self::ECS_ENV_VAR_V3_KEY); } + /** + * @test + */ + public function TestV4EndpointFails() + { + putenv(self::ECS_ENV_VAR_V4_KEY . '=' . self::ECS_ENV_VAR_V4_VAL); + + $mockData = $this->createMock(DataProvider::class); + $mockData->method('getCgroupData')->willReturn(self::INVALID_CGROUP_LENGTH); + $mockData->method('getHostName')->willReturn(null); + + $mockGuzzle = new MockHandler([ + new Response(500, ['Content-Type' => 'application/json'], '{"message":"cuz I have a baad daaay"}'), + ]); + $handlerStack = HandlerStack::create($mockGuzzle); + $client = new Client(['handler' => $handlerStack]); + $requestFactory = new HttpFactory(); + + $detector = new Detector($mockData, $client, $requestFactory); + + $this->assertEquals(ResourceInfo::create( + Attributes::create( + [ + ResourceAttributes::CLOUD_PROVIDER => ResourceAttributeValues::CLOUD_PROVIDER_AWS, + ResourceAttributes::CLOUD_PLATFORM => ResourceAttributeValues::CLOUD_PLATFORM_AWS_ECS, + ] + ) + ), $detector->getResource()); + + putenv(self::ECS_ENV_VAR_V4_KEY); + } + /** * @test */