Skip to content

Commit

Permalink
Csm client config (#1834)
Browse files Browse the repository at this point in the history
* Refactor CSM addition to ClientResolver

* Add client config option for csm

* Add unit tests

* Add changelog
  • Loading branch information
howardlopez committed Jul 1, 2019
1 parent 3641926 commit ba3361b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .changes/nextrelease/csm_client_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "feature",
"category": "ClientSideMonitoring",
"description": "Added client configuration options for client-side monitoring."
}
]
36 changes: 10 additions & 26 deletions src/AwsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public static function getArguments()
* credentials or return null. See Aws\Credentials\CredentialProvider for
* a list of built-in credentials providers. If no credentials are
* provided, the SDK will attempt to load them from the environment.
* - csm:
* (Aws\ClientSideMonitoring\ConfigurationInterface|array|callable) Specifies
* the credentials used to sign requests. Provide an
* Aws\ClientSideMonitoring\ConfigurationInterface object, a callable
* configuration provider used to create client-side monitoring configuration,
* `false` to disable csm, or an associative array with the following keys:
* enabled: (bool) Set to true to enable client-side monitoring, defaults
* to false; host: (string) the host location to send monitoring events to,
* defaults to 127.0.0.1; port: (int) The port used for the host connection,
* defaults to 31000; client_id: (string) An identifier for this project
* - debug: (bool|array) Set to true to display debug information when
* sending requests. Alternatively, you can provide an associative array
* with the following keys: logfn: (callable) Function that is invoked
Expand Down Expand Up @@ -184,7 +194,6 @@ public function __construct(array $args)
$this->defaultRequestOptions = $config['http'];
$this->addSignatureMiddleware();
$this->addInvocationId();
$this->addClientSideMonitoring($args);
$this->addEndpointParameterMiddleware($args);
$this->addEndpointDiscoveryMiddleware($config, $args);
$this->loadAliases();
Expand Down Expand Up @@ -350,31 +359,6 @@ private function addInvocationId()
$this->handlerList->prependSign(Middleware::invocationId(), 'invocation-id');
}

private function addClientSideMonitoring($args)
{
$options = ConfigurationProvider::defaultProvider($args);

$this->handlerList->appendBuild(
ApiCallMonitoringMiddleware::wrap(
$this->credentialProvider,
$options,
$this->region,
$this->getApi()->getServiceId()
),
'ApiCallMonitoringMiddleware'
);

$callAttemptMiddleware = ApiCallAttemptMonitoringMiddleware::wrap(
$this->credentialProvider,
$options,
$this->region,
$this->getApi()->getServiceId()
);
$this->handlerList->appendAttempt (
$callAttemptMiddleware,
'ApiCallAttemptMonitoringMiddleware'
);
}
private function loadAliases($file = null)
{
if (!isset($this->aliases)) {
Expand Down
43 changes: 43 additions & 0 deletions src/ClientResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use Aws\Api\Validator;
use Aws\Api\ApiProvider;
use Aws\Api\Service;
use Aws\ClientSideMonitoring\ApiCallAttemptMonitoringMiddleware;
use Aws\ClientSideMonitoring\ApiCallMonitoringMiddleware;
use Aws\ClientSideMonitoring\Configuration;
use Aws\Credentials\Credentials;
use Aws\Credentials\CredentialsInterface;
use Aws\Endpoint\PartitionEndpointProvider;
Expand Down Expand Up @@ -172,6 +175,13 @@ class ClientResolver
'doc' => 'Set to true to display debug information when sending requests. Alternatively, you can provide an associative array with the following keys: logfn: (callable) Function that is invoked with log messages; stream_size: (int) When the size of a stream is greater than this number, the stream data will not be logged (set to "0" to not log any stream data); scrub_auth: (bool) Set to false to disable the scrubbing of auth data from the logged messages; http: (bool) Set to false to disable the "debug" feature of lower level HTTP adapters (e.g., verbose curl output).',
'fn' => [__CLASS__, '_apply_debug'],
],
'csm' => [
'type' => 'value',
'valid' => [\Aws\ClientSideMonitoring\ConfigurationInterface::class, 'callable', 'array', 'bool'],
'doc' => 'CSM options for the client. Provides a callable wrapping a promise, a boolean "false", an instance of ConfigurationInterface, or an associative array of "enabled", "host", "port", and "client_id".',
'fn' => [__CLASS__, '_apply_csm'],
'default' => [\Aws\ClientSideMonitoring\ConfigurationProvider::class, 'defaultProvider']
],
'http' => [
'type' => 'value',
'valid' => ['array'],
Expand Down Expand Up @@ -433,6 +443,39 @@ public static function _apply_credentials($value, array &$args)
}
}

public static function _apply_csm($value, array &$args, HandlerList $list)
{
if ($value === false) {
$value = new Configuration(
false,
\Aws\ClientSideMonitoring\ConfigurationProvider::DEFAULT_HOST,
\Aws\ClientSideMonitoring\ConfigurationProvider::DEFAULT_PORT,
\Aws\ClientSideMonitoring\ConfigurationProvider::DEFAULT_CLIENT_ID
);
$args['csm'] = $value;
}

$list->appendBuild(
ApiCallMonitoringMiddleware::wrap(
$args['credentials'],
$value,
$args['region'],
$args['api']->getServiceId()
),
'ApiCallMonitoringMiddleware'
);

$list->appendAttempt(
ApiCallAttemptMonitoringMiddleware::wrap(
$args['credentials'],
$value,
$args['region'],
$args['api']->getServiceId()
),
'ApiCallAttemptMonitoringMiddleware'
);
}

public static function _apply_api_provider(callable $value, array &$args)
{
$api = new Service(
Expand Down
47 changes: 47 additions & 0 deletions tests/ClientResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use Aws\Api\Service;
use Aws\ClientResolver;
use Aws\ClientSideMonitoring\Configuration;
use Aws\ClientSideMonitoring\ConfigurationProvider;
use Aws\CommandInterface;
use Aws\Credentials\CredentialProvider;
use Aws\Credentials\Credentials;
Expand Down Expand Up @@ -388,6 +390,51 @@ public function testCanUseCredentialsCache()
$this->assertSame($c, $cached);
}

public function testCanUseCsmConfigObject()
{
$config = new Configuration(true, 'foohost', 1111, 'barid');
$resolver = new ClientResolver(ClientResolver::getDefaultArguments());
$conf = $resolver->resolve([
'service' => 'sqs',
'region' => 'x',
'csm' => $config,
'version' => 'latest'
], new HandlerList());
$this->assertEquals($config->toArray(), $conf['csm']->toArray());
}

public function testCanUseCsmArray()
{
$config = new Configuration(true, 'foohost', 1111, 'barid');
$configArray = $config->toArray();
$resolver = new ClientResolver(ClientResolver::getDefaultArguments());
$conf = $resolver->resolve([
'service' => 'sqs',
'region' => 'x',
'csm' => $configArray,
'version' => 'latest'
], new HandlerList());
$this->assertEquals($configArray, $conf['csm']);
}

public function testCanUseCsmFalse()
{
$config = new Configuration(
false,
ConfigurationProvider::DEFAULT_HOST,
ConfigurationProvider::DEFAULT_PORT,
ConfigurationProvider::DEFAULT_CLIENT_ID
);
$resolver = new ClientResolver(ClientResolver::getDefaultArguments());
$conf = $resolver->resolve([
'service' => 'sqs',
'region' => 'x',
'csm' => false,
'version' => 'latest'
], new HandlerList());
$this->assertEquals($config->toArray(), $conf['csm']->toArray());
}

public function testCanUseCustomEndpointProviderWithExtraData()
{
$p = function () {
Expand Down

0 comments on commit ba3361b

Please sign in to comment.