Skip to content

Commit

Permalink
fix: account id endpoint metric
Browse files Browse the repository at this point in the history
- Captures the AccountIdEndpointMode metric just if the parameter is defined in the ruleset.
- Captures the account id endpoint metric whenever an account id endpoint was resolved by the endpoint middleware.
  • Loading branch information
yenfryherrerafeliz committed Jan 3, 2025
1 parent 3ebc383 commit 999e5fe
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 70 deletions.
17 changes: 17 additions & 0 deletions src/EndpointV2/EndpointV2Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,27 @@ public function __invoke(CommandInterface $command)
$operation = $this->api->getOperation($command->getName());
$commandArgs = $command->toArray();
$providerArgs = $this->resolveArgs($commandArgs, $operation);

// Resolved AccountId Metric
if (!empty($providerArgs[self::ACCOUNT_ID_PARAM])) {
$command->getMetricsBuilder()->append(MetricsBuilder::RESOLVED_ACCOUNT_ID);
}
// AccountIdMode Metric
if(!empty($providerArgs[self::ACCOUNT_ID_ENDPOINT_MODE_PARAM])) {
$command->getMetricsBuilder()->identifyMetricByValueAndAppend(
'account_id_endpoint_mode',
$providerArgs[self::ACCOUNT_ID_ENDPOINT_MODE_PARAM]
);
}

$endpoint = $this->endpointProvider->resolveEndpoint($providerArgs);

// AccountId Endpoint Metric
$command->getMetricsBuilder()->identifyMetricByValueAndAppend(
'account_id_endpoint',
$endpoint->getUrl()
);

if (!empty($authSchemes = $endpoint->getProperty('authSchemes'))) {
$this->applyAuthScheme(
$authSchemes,
Expand Down
43 changes: 42 additions & 1 deletion src/MetricsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Aws\Credentials\CredentialsInterface;
use Aws\Credentials\CredentialSources;
use GuzzleHttp\Psr7\Uri;

/**
* A placeholder for gathering metrics in a request.
Expand Down Expand Up @@ -136,7 +137,9 @@ public function identifyMetricByValueAndAppend(
'signature' => 'appendSignatureMetric',
'request_compression' => 'appendRequestCompressionMetric',
'request_checksum' => 'appendRequestChecksumMetric',
'credentials' => 'appendCredentialsMetric'
'credentials' => 'appendCredentialsMetric',
'account_id_endpoint_mode' => 'appendAccountIdEndpointMode',
'account_id_endpoint' => 'appendAccountIdEndpoint',
];

$fn = $appendMetricFns[$featureGroup];
Expand Down Expand Up @@ -244,6 +247,44 @@ private function appendCredentialsMetric(
}
}

/**
* Appends the account_id_endpoint_mode metrics based on
* the value resolved.
*
* @param string $accountIdEndpointMode
*
* @return void
*/
private function appendAccountIdEndpointMode(
string $accountIdEndpointMode
): void
{
if (empty($accountIdEndpointMode)) {
return;
}

if ($accountIdEndpointMode === 'preferred') {
$this->append(MetricsBuilder::ACCOUNT_ID_MODE_PREFERRED);
} elseif ($accountIdEndpointMode === 'disabled') {
$this->append(MetricsBuilder::ACCOUNT_ID_MODE_DISABLED);
} elseif ($accountIdEndpointMode === 'required') {
$this->append(MetricsBuilder::ACCOUNT_ID_MODE_REQUIRED);
}
}

/**
* @param string $endpoint
*
* @return void
*/
private function appendAccountIdEndpoint(string $endpoint): void
{
static $pattern = "/(https|http):\\/\\/\\d{12}\\.ddb/";
if (preg_match($pattern, $endpoint)) {
$this->append(MetricsBuilder::ACCOUNT_ID_ENDPOINT);
}
}

/**
* Validates if a metric can be appended by ensuring the total size,
* including the new metric and separator, does not exceed the limit.
Expand Down
21 changes: 0 additions & 21 deletions src/UserAgentMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class UserAgentMiddleware

static $metricsFnList = [
'appendEndpointMetric',
'appendAccountIdModeMetric',
'appendRetryConfigMetric',
];

Expand Down Expand Up @@ -281,26 +280,6 @@ private function appendEndpointMetric(): void
}
}

/**
* Appends the account id endpoint mode metric into the metrics builder,
* based on the account id endpoint mode provide as client argument.
*/
private function appendAccountIdModeMetric(): void
{
$accountIdMode = $this->args['account_id_endpoint_mode'] ?? null;
if ($accountIdMode === null) {
return;
}

if ($accountIdMode === 'preferred') {
$this->metricsBuilder->append(MetricsBuilder::ACCOUNT_ID_MODE_PREFERRED);
} elseif ($accountIdMode === 'disabled') {
$this->metricsBuilder->append(MetricsBuilder::ACCOUNT_ID_MODE_DISABLED);
} elseif ($accountIdMode === 'required') {
$this->metricsBuilder->append(MetricsBuilder::ACCOUNT_ID_MODE_REQUIRED);
}
}

/**
* Appends the retry mode metric into the metrics builder,
* based on the resolved retry config mode.
Expand Down
173 changes: 125 additions & 48 deletions tests/UserAgentMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Aws\Crypto\MaterialsProviderV2;
use Aws\DynamoDb\DynamoDbClient;
use Aws\EndpointV2\EndpointDefinitionProvider;
use Aws\EndpointV2\EndpointProviderV2;
use Aws\MetricsBuilder;
use Aws\Result;
use Aws\S3\Crypto\S3EncryptionClient;
Expand Down Expand Up @@ -211,27 +212,6 @@ public function userAgentCasesDataProvider(): \Generator

return [$args, 'm/' . MetricsBuilder::ENDPOINT_OVERRIDE];
},
'metricsWithAccountIdModePreferred' => function (): array {
$args = [
'account_id_endpoint_mode' => 'preferred'
];

return [$args, 'm/' . MetricsBuilder::ACCOUNT_ID_MODE_PREFERRED];
},
'metricsWithAccountIdModeRequired' => function (): array {
$args = [
'account_id_endpoint_mode' => 'required'
];

return [$args, 'm/' . MetricsBuilder::ACCOUNT_ID_MODE_REQUIRED];
},
'metricsWithAccountIdModeDisabled' => function (): array {
$args = [
'account_id_endpoint_mode' => 'disabled'
];

return [$args, 'm/' . MetricsBuilder::ACCOUNT_ID_MODE_DISABLED];
},
'metricsWithRetryConfigArrayStandardMode' => function (): array {
$args = [
'retries' => [
Expand Down Expand Up @@ -658,46 +638,143 @@ public function testUserAgentCaptureGzipRequestCompressionMetric()
* @return void
*/
public function testUserAgentCaptureResolvedAccountIdMetric()
{
$dynamoDbClient = $this->getTestDynamoDBClient(
[
'credentials' => new Credentials(
'foo',
'foo',
'foo',
null,
'123456789012'
),
'http_handler' => function (
RequestInterface $request
) {
$metrics = $this->getMetricsAsArray($request);

$this->assertTrue(
in_array(MetricsBuilder::RESOLVED_ACCOUNT_ID, $metrics)
);

return new Response(
200,
[],
'{}'
);
}
]
);
$dynamoDbClient->listTables();
}

/**
* Tests user agent captures the accountIdEndpointMode metric.
*
* @return void
*/
public function testUserAgentCaptureResolvedAccountIdEndpointMode() {
$accountIdModesMetrics = [
'preferred' => MetricsBuilder::ACCOUNT_ID_MODE_PREFERRED,
'required' => MetricsBuilder::ACCOUNT_ID_MODE_REQUIRED,
'disabled' => MetricsBuilder::ACCOUNT_ID_MODE_DISABLED,
];
foreach ($accountIdModesMetrics as $config => $metric) {
$dynamoDbClient = $this->getTestDynamoDBClient(
[
'account_id_endpoint_mode' => $config,
'credentials' => new Credentials(
'foo',
'foo',
'foo',
null,
'123456789012'
),
'http_handler' => function (
RequestInterface $request
) use ($metric) {
$metrics = $this->getMetricsAsArray($request);

$this->assertTrue(
in_array($metric, $metrics)
);

return new Response(
200,
[],
'{}'
);
}
]
);
$dynamoDbClient->listTables();
}
}

/**
* Tests user agent captures a resolved account id metric.
*
* @return void
*/
public function testUserAgentCaptureAccountIdEndpointMetric()
{
$dynamoDbClient = $this->getTestDynamoDBClient(
[
'credentials' => new Credentials(
'foo',
'foo',
'foo',
null,
'123456789012'
),
'http_handler' => function (
RequestInterface $request
) {
$metrics = $this->getMetricsAsArray($request);

$this->assertTrue(
in_array(MetricsBuilder::ACCOUNT_ID_ENDPOINT, $metrics)
);

return new Response(
200,
[],
'{}'
);
}
]
);
$dynamoDbClient->listTables();
}

/**
* Returns a test dynamodb client,
* where rules for resolving account endpoints
* are present.
*
* @param array $args
*
* @return DynamoDbClient
*/
private function getTestDynamoDBClient(
array $args
): DynamoDbClient
{
try {
$ruleSet = $this->getDynamoDBTestRuleSet();
} catch (\Exception $e) {
$this->fail($e->getMessage());
}

$dynamoDbClient = new DynamoDbClient([
return new DynamoDbClient([
'api_provider' => ApiProvider::filesystem(
__DIR__ . '/fixtures/aws_client_test'
),
'endpoint_provider' => new \Aws\EndpointV2\EndpointProviderV2(
'endpoint_provider' => new EndpointProviderV2(
$ruleSet,
EndpointDefinitionProvider::getPartitions()
),
'region' => 'us-east-2',
'credentials' => new Credentials(
'foo',
'foo',
'foo',
null,
'123456789012'
),
'http_handler' => function (
RequestInterface $request
) {
$metrics = $this->getMetricsAsArray($request);

$this->assertTrue(
in_array(MetricsBuilder::RESOLVED_ACCOUNT_ID, $metrics)
);

return new Response(
200,
[],
'{}'
);
}
]);
$dynamoDbClient->listTables();
] + $args);
}

/**
Expand Down

0 comments on commit 999e5fe

Please sign in to comment.