PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / Aws / ClientSideMonitoring / ApiCallAttemptMonitoringMiddleware.php

ApiCallAttemptMonitoringMiddleware.php in Media Cloud Sync 1.2.12, at includes/sdk/s3/Aws/ClientSideMonitoring/ApiCallAttemptMonitoringMiddleware.php

182 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\ClientSideMonitoring;
4
5 use Dudlewebs\WPMCS\s3\Aws\CommandInterface;
6 use Dudlewebs\WPMCS\s3\Aws\Credentials\CredentialsInterface;
7 use Dudlewebs\WPMCS\s3\Aws\Exception\AwsException;
8 use Dudlewebs\WPMCS\s3\Aws\ResponseContainerInterface;
9 use Dudlewebs\WPMCS\s3\Aws\ResultInterface;
10 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
11 use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface;
12 /**
13 * @internal
14 */
15 class ApiCallAttemptMonitoringMiddleware extends AbstractMonitoringMiddleware
16 {
17 /**
18 * Standard middleware wrapper function with CSM options passed in.
19 *
20 * @param callable $credentialProvider
21 * @param mixed $options
22 * @param string $region
23 * @param string $service
24 * @return callable
25 */
26 public static function wrap(callable $credentialProvider, $options, $region, $service)
27 {
28 return function (callable $handler) use($credentialProvider, $options, $region, $service) {
29 return new static($handler, $credentialProvider, $options, $region, $service);
30 };
31 }
32 /**
33 * {@inheritdoc}
34 */
35 public static function getRequestData(RequestInterface $request)
36 {
37 return ['Fqdn' => $request->getUri()->getHost()];
38 }
39 /**
40 * {@inheritdoc}
41 */
42 public static function getResponseData($klass)
43 {
44 if ($klass instanceof ResultInterface) {
45 return ['AttemptLatency' => self::getResultAttemptLatency($klass), 'DestinationIp' => self::getResultDestinationIp($klass), 'DnsLatency' => self::getResultDnsLatency($klass), 'HttpStatusCode' => self::getResultHttpStatusCode($klass), 'XAmzId2' => self::getResultHeader($klass, 'x-amz-id-2'), 'XAmzRequestId' => self::getResultHeader($klass, 'x-amz-request-id'), 'XAmznRequestId' => self::getResultHeader($klass, 'x-amzn-RequestId')];
46 }
47 if ($klass instanceof AwsException) {
48 return ['AttemptLatency' => self::getAwsExceptionAttemptLatency($klass), 'AwsException' => \substr(self::getAwsExceptionErrorCode($klass), 0, 128), 'AwsExceptionMessage' => \substr(self::getAwsExceptionMessage($klass), 0, 512), 'DestinationIp' => self::getAwsExceptionDestinationIp($klass), 'DnsLatency' => self::getAwsExceptionDnsLatency($klass), 'HttpStatusCode' => self::getAwsExceptionHttpStatusCode($klass), 'XAmzId2' => self::getAwsExceptionHeader($klass, 'x-amz-id-2'), 'XAmzRequestId' => self::getAwsExceptionHeader($klass, 'x-amz-request-id'), 'XAmznRequestId' => self::getAwsExceptionHeader($klass, 'x-amzn-RequestId')];
49 }
50 if ($klass instanceof \Exception) {
51 return ['HttpStatusCode' => self::getExceptionHttpStatusCode($klass), 'SdkException' => \substr(self::getExceptionCode($klass), 0, 128), 'SdkExceptionMessage' => \substr(self::getExceptionMessage($klass), 0, 512), 'XAmzId2' => self::getExceptionHeader($klass, 'x-amz-id-2'), 'XAmzRequestId' => self::getExceptionHeader($klass, 'x-amz-request-id'), 'XAmznRequestId' => self::getExceptionHeader($klass, 'x-amzn-RequestId')];
52 }
53 throw new \InvalidArgumentException('Parameter must be an instance of ResultInterface, AwsException or Exception.');
54 }
55 private static function getResultAttemptLatency(ResultInterface $result)
56 {
57 if (isset($result['@metadata']['transferStats']['http'])) {
58 $attempt = \end($result['@metadata']['transferStats']['http']);
59 if (isset($attempt['total_time'])) {
60 return (int) \floor($attempt['total_time'] * 1000);
61 }
62 }
63 return null;
64 }
65 private static function getResultDestinationIp(ResultInterface $result)
66 {
67 if (isset($result['@metadata']['transferStats']['http'])) {
68 $attempt = \end($result['@metadata']['transferStats']['http']);
69 if (isset($attempt['primary_ip'])) {
70 return $attempt['primary_ip'];
71 }
72 }
73 return null;
74 }
75 private static function getResultDnsLatency(ResultInterface $result)
76 {
77 if (isset($result['@metadata']['transferStats']['http'])) {
78 $attempt = \end($result['@metadata']['transferStats']['http']);
79 if (isset($attempt['namelookup_time'])) {
80 return (int) \floor($attempt['namelookup_time'] * 1000);
81 }
82 }
83 return null;
84 }
85 private static function getResultHttpStatusCode(ResultInterface $result)
86 {
87 return $result['@metadata']['statusCode'];
88 }
89 private static function getAwsExceptionAttemptLatency(AwsException $e)
90 {
91 $attempt = $e->getTransferInfo();
92 if (isset($attempt['total_time'])) {
93 return (int) \floor($attempt['total_time'] * 1000);
94 }
95 return null;
96 }
97 private static function getAwsExceptionErrorCode(AwsException $e)
98 {
99 return $e->getAwsErrorCode();
100 }
101 private static function getAwsExceptionMessage(AwsException $e)
102 {
103 return $e->getAwsErrorMessage();
104 }
105 private static function getAwsExceptionDestinationIp(AwsException $e)
106 {
107 $attempt = $e->getTransferInfo();
108 if (isset($attempt['primary_ip'])) {
109 return $attempt['primary_ip'];
110 }
111 return null;
112 }
113 private static function getAwsExceptionDnsLatency(AwsException $e)
114 {
115 $attempt = $e->getTransferInfo();
116 if (isset($attempt['namelookup_time'])) {
117 return (int) \floor($attempt['namelookup_time'] * 1000);
118 }
119 return null;
120 }
121 private static function getAwsExceptionHttpStatusCode(AwsException $e)
122 {
123 $response = $e->getResponse();
124 if ($response !== null) {
125 return $response->getStatusCode();
126 }
127 return null;
128 }
129 private static function getExceptionHttpStatusCode(\Exception $e)
130 {
131 if ($e instanceof ResponseContainerInterface) {
132 $response = $e->getResponse();
133 if ($response instanceof ResponseInterface) {
134 return $response->getStatusCode();
135 }
136 }
137 return null;
138 }
139 private static function getExceptionCode(\Exception $e)
140 {
141 if (!$e instanceof AwsException) {
142 return \get_class($e);
143 }
144 return null;
145 }
146 private static function getExceptionMessage(\Exception $e)
147 {
148 if (!$e instanceof AwsException) {
149 return $e->getMessage();
150 }
151 return null;
152 }
153 /**
154 * {@inheritdoc}
155 */
156 protected function populateRequestEventData(CommandInterface $cmd, RequestInterface $request, array $event)
157 {
158 $event = parent::populateRequestEventData($cmd, $request, $event);
159 $event['Type'] = 'ApiCallAttempt';
160 return $event;
161 }
162 /**
163 * {@inheritdoc}
164 */
165 protected function populateResultEventData($result, array $event)
166 {
167 $event = parent::populateResultEventData($result, $event);
168 $provider = $this->credentialProvider;
169 /** @var CredentialsInterface $credentials */
170 $credentials = $provider()->wait();
171 $event['AccessKey'] = $credentials->getAccessKeyId();
172 $sessionToken = $credentials->getSecurityToken();
173 if ($sessionToken !== null) {
174 $event['SessionToken'] = $sessionToken;
175 }
176 if (empty($event['AttemptLatency'])) {
177 $event['AttemptLatency'] = (int) (\floor(\microtime(\true) * 1000) - $event['Timestamp']);
178 }
179 return $event;
180 }
181 }
182