Skip to content

Commit

Permalink
Implement logging and add a test for metadata endpoint failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Mancioppi committed Oct 31, 2022
1 parent 8260f9a commit 6ee70ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/Aws/src/Ecs/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand All @@ -143,15 +145,23 @@ 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();
}

$taskRequest = $this->requestFactory
->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();
}

Expand Down
32 changes: 32 additions & 0 deletions src/Aws/tests/Unit/Ecs/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 6ee70ac

Please sign in to comment.