Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement cloud{provider,platform}, aws.{ecs.*,log.*} in ECS detector #83

Merged
merged 3 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 111 additions & 15 deletions src/Aws/src/Ecs/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SemConv\ResourceAttributes;
use OpenTelemetry\SemConv\ResourceAttributeValues;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Throwable;

/**
Expand All @@ -39,34 +42,48 @@ class Detector implements ResourceDetectorInterface
private const CONTAINER_ID_LENGTH = 64;

private DataProvider $processData;
private ClientInterface $client;
private RequestFactoryInterface $requestFactory;

public function __construct(DataProvider $processData)
{
public function __construct(
DataProvider $processData,
ClientInterface $client,
RequestFactoryInterface $requestFactory
) {
$this->processData = $processData;
$this->client = $client;
$this->requestFactory = $requestFactory;
}

/**
* If running on ECS, runs getContainerId(), getClusterName(), and
* returns resource with valid extracted values
* If not running on ECS, returns empty rsource
* If running on ECS with an ECS agent v1.3, returns a resource with the following attributes set:
*
* -
mmanciop marked this conversation as resolved.
Show resolved Hide resolved
*
* If not running on ECS, returns empty resource.
*/
public function getResource(): ResourceInfo
{
$metadataEndpointV4 = getenv(self::ECS_METADATA_KEY_V4);
// Check if running on ECS by looking for below environment variables
if (!getenv(self::ECS_METADATA_KEY_V4) && !getenv(self::ECS_METADATA_KEY_V3)) {
if (!$metadataEndpointV4 && !getenv(self::ECS_METADATA_KEY_V3)) {
// TODO: add 'Process is not running on ECS' when logs are added
mmanciop marked this conversation as resolved.
Show resolved Hide resolved
return ResourceInfoFactory::emptyResource();
}

$hostName = $this->processData->getHostname();
$containerId = $this->getContainerId();
$basicEcsResource = ResourceInfo::create(Attributes::create([
ResourceAttributes::CLOUD_PROVIDER => ResourceAttributeValues::CLOUD_PROVIDER_AWS,
ResourceAttributes::CLOUD_PLATFORM => ResourceAttributeValues::CLOUD_PLATFORM_AWS_ECS,
]));

return !$hostName && !$containerId
? ResourceInfoFactory::emptyResource()
: ResourceInfo::create(Attributes::create([
ResourceAttributes::CONTAINER_NAME => $hostName,
ResourceAttributes::CONTAINER_ID => $containerId,
]));
$metadataV4Resource = $this->getMetadataEndpointV4Resource();

$hostNameAndContainerIdResource = ResourceInfo::create(Attributes::create([
ResourceAttributes::CONTAINER_NAME => $this->processData->getHostname(),
ResourceAttributes::CONTAINER_ID => $this->getContainerId(),
]));

return ResourceInfoFactory::merge($basicEcsResource, $hostNameAndContainerIdResource, $metadataV4Resource);
}

/**
Expand All @@ -88,9 +105,88 @@ private function getContainerId(): ?string
}
}
} catch (Throwable $e) {
//TODO: add 'Failed to read container ID' when logging is added
// TODO: add 'Failed to read container ID' when logging is added
}

return null;
}

private function getMetadataEndpointV4Resource(): ResourceInfo
{
$metadataEndpointV4 = getenv(self::ECS_METADATA_KEY_V4);
if (!$metadataEndpointV4) {
return ResourceInfoFactory::emptyResource();
}

$containerRequest = $this->requestFactory
->createRequest('GET', $metadataEndpointV4);
$containerResponse = $this->client->sendRequest($containerRequest);
if ($containerResponse->getStatusCode() > 299) {
// TODO: Log error
return ResourceInfoFactory::emptyResource();
}

$taskRequest = $this->requestFactory
->createRequest('GET', $metadataEndpointV4 . '/task');
$taskResponse = $this->client->sendRequest($taskRequest);
if ($taskResponse->getStatusCode() > 299) {
// TODO: Log error
return ResourceInfoFactory::emptyResource();
}

$containerMetadata = json_decode($containerResponse->getBody()->getContents(), true);
$taskMetadata = json_decode($taskResponse->getBody()->getContents(), true);

$launchType = isset($taskMetadata['LaunchType']) ? strtolower($taskMetadata['LaunchType']) : null;
$taskFamily = isset($taskMetadata['Family']) ? $taskMetadata['Family'] : null;
$taskRevision = isset($taskMetadata['Revision']) ? $taskMetadata['Revision'] : null;

$clusterArn = null;
$taskArn = null;
if (isset($taskMetadata['Cluster']) && isset($taskMetadata['TaskARN'])) {
$taskArn = $taskMetadata['TaskARN'];
$lastIndexOfColon = strrpos($taskArn, ':');
if ($lastIndexOfColon) {
$baseArn = substr($taskArn, 0, $lastIndexOfColon);
$cluster = $taskMetadata['Cluster'];
$clusterArn = strpos($cluster, 'arn:') === 0 ? $cluster : $baseArn . ':cluster/' . $cluster;
}
}

$containerArn = isset($containerMetadata['ContainerARN']) ? $containerMetadata['ContainerARN'] : null;

$logResource = ResourceInfoFactory::emptyResource();
if (isset($containerMetadata['LogOptions']) && isset($containerMetadata['LogDriver']) && $containerMetadata['LogDriver'] === 'awslogs') {
$logOptions = $containerMetadata['LogOptions'];
$logsGroupName = $logOptions['awslogs-group'];
$logsStreamName = $logOptions['awslogs-stream'];

$logsGroupArns = [];
$logsStreamArns = [];
if (isset($containerMetadata['ContainerARN']) && preg_match('/arn:aws:ecs:([^:]+):([^:]+):.*/', $containerMetadata['ContainerARN'], $matches)) {
[$arn, $awsRegion, $awsAccount] = $matches;

$logsGroupArns = ['arn:aws:logs:' . $awsRegion . ':' . $awsAccount . ':log-group:' . $logsGroupName];
$logsStreamArns = ['arn:aws:logs:' . $awsRegion . ':' . $awsAccount . ':log-group:' . $logsGroupName . ':log-stream:' . $logsStreamName];
}

$logResource = ResourceInfo::create(Attributes::create([
ResourceAttributes::AWS_LOG_GROUP_NAMES => [$logsGroupName],
ResourceAttributes::AWS_LOG_GROUP_ARNS => $logsGroupArns,
ResourceAttributes::AWS_LOG_STREAM_NAMES => [$logsStreamName],
ResourceAttributes::AWS_LOG_STREAM_ARNS => $logsStreamArns,
]));
}

$ecsResource = ResourceInfo::create(Attributes::create([
ResourceAttributes::AWS_ECS_CONTAINER_ARN => $containerArn,
ResourceAttributes::AWS_ECS_CLUSTER_ARN => $clusterArn,
ResourceAttributes::AWS_ECS_LAUNCHTYPE => $launchType,
ResourceAttributes::AWS_ECS_TASK_ARN => $taskArn,
ResourceAttributes::AWS_ECS_TASK_FAMILY => $taskFamily,
ResourceAttributes::AWS_ECS_TASK_REVISION => $taskRevision,
]));

return ResourceInfoFactory::merge($ecsResource, $logResource);
}
}
Loading