diff --git a/.changes/nextrelease/csm_client_config.json b/.changes/nextrelease/csm_client_config.json new file mode 100644 index 0000000000..13b777e6c6 --- /dev/null +++ b/.changes/nextrelease/csm_client_config.json @@ -0,0 +1,7 @@ +[ + { + "type": "feature", + "category": "ClientSideMonitoring", + "description": "Added client configuration options for client-side monitoring." + } +] diff --git a/src/AwsClient.php b/src/AwsClient.php index 017252942a..61e69247a1 100644 --- a/src/AwsClient.php +++ b/src/AwsClient.php @@ -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 @@ -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(); @@ -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)) { diff --git a/src/ClientResolver.php b/src/ClientResolver.php index 5ca3df7ac1..e6bb38e49a 100644 --- a/src/ClientResolver.php +++ b/src/ClientResolver.php @@ -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; @@ -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'], @@ -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( diff --git a/tests/ClientResolverTest.php b/tests/ClientResolverTest.php index 32fae7f4ed..e9740201c0 100644 --- a/tests/ClientResolverTest.php +++ b/tests/ClientResolverTest.php @@ -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; @@ -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 () {