| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Aws\ClientSideMonitoring;
|
|
|
4 |
|
|
|
5 |
use Aws\CommandInterface;
|
|
|
6 |
use Aws\Credentials\CredentialsInterface;
|
|
|
7 |
use Aws\Exception\AwsException;
|
|
|
8 |
use Aws\ResponseContainerInterface;
|
|
|
9 |
use Aws\ResultInterface;
|
|
|
10 |
use Psr\Http\Message\RequestInterface;
|
|
|
11 |
use Psr\Http\Message\ResponseInterface;
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* @internal
|
|
|
15 |
*/
|
|
|
16 |
class ApiCallAttemptMonitoringMiddleware extends AbstractMonitoringMiddleware
|
|
|
17 |
{
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Standard middleware wrapper function with CSM options passed in.
|
|
|
21 |
*
|
|
|
22 |
* @param callable $credentialProvider
|
|
|
23 |
* @param mixed $options
|
|
|
24 |
* @param string $region
|
|
|
25 |
* @param string $service
|
|
|
26 |
* @return callable
|
|
|
27 |
*/
|
|
|
28 |
public static function wrap(
|
|
|
29 |
callable $credentialProvider,
|
|
|
30 |
$options,
|
|
|
31 |
$region,
|
|
|
32 |
$service
|
|
|
33 |
) {
|
|
|
34 |
return function (callable $handler) use (
|
|
|
35 |
$credentialProvider,
|
|
|
36 |
$options,
|
|
|
37 |
$region,
|
|
|
38 |
$service
|
|
|
39 |
) {
|
|
|
40 |
return new static(
|
|
|
41 |
$handler,
|
|
|
42 |
$credentialProvider,
|
|
|
43 |
$options,
|
|
|
44 |
$region,
|
|
|
45 |
$service
|
|
|
46 |
);
|
|
|
47 |
};
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* {@inheritdoc}
|
|
|
52 |
*/
|
|
|
53 |
public static function getRequestData(RequestInterface $request)
|
|
|
54 |
{
|
|
|
55 |
return [
|
|
|
56 |
'Fqdn' => $request->getUri()->getHost(),
|
|
|
57 |
];
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* {@inheritdoc}
|
|
|
62 |
*/
|
|
|
63 |
public static function getResponseData($klass)
|
|
|
64 |
{
|
|
|
65 |
if ($klass instanceof ResultInterface) {
|
|
|
66 |
return [
|
|
|
67 |
'AttemptLatency' => self::getResultAttemptLatency($klass),
|
|
|
68 |
'DestinationIp' => self::getResultDestinationIp($klass),
|
|
|
69 |
'DnsLatency' => self::getResultDnsLatency($klass),
|
|
|
70 |
'HttpStatusCode' => self::getResultHttpStatusCode($klass),
|
|
|
71 |
'XAmzId2' => self::getResultHeader($klass, 'x-amz-id-2'),
|
|
|
72 |
'XAmzRequestId' => self::getResultHeader($klass, 'x-amz-request-id'),
|
|
|
73 |
'XAmznRequestId' => self::getResultHeader($klass, 'x-amzn-RequestId'),
|
|
|
74 |
];
|
|
|
75 |
}
|
|
|
76 |
if ($klass instanceof AwsException) {
|
|
|
77 |
return [
|
|
|
78 |
'AttemptLatency' => self::getAwsExceptionAttemptLatency($klass),
|
|
|
79 |
'AwsException' => substr(
|
|
|
80 |
self::getAwsExceptionErrorCode($klass),
|
|
|
81 |
0,
|
|
|
82 |
128
|
|
|
83 |
),
|
|
|
84 |
'AwsExceptionMessage' => substr(
|
|
|
85 |
self::getAwsExceptionMessage($klass),
|
|
|
86 |
0,
|
|
|
87 |
512
|
|
|
88 |
),
|
|
|
89 |
'DestinationIp' => self::getAwsExceptionDestinationIp($klass),
|
|
|
90 |
'DnsLatency' => self::getAwsExceptionDnsLatency($klass),
|
|
|
91 |
'HttpStatusCode' => self::getAwsExceptionHttpStatusCode($klass),
|
|
|
92 |
'XAmzId2' => self::getAwsExceptionHeader($klass, 'x-amz-id-2'),
|
|
|
93 |
'XAmzRequestId' => self::getAwsExceptionHeader(
|
|
|
94 |
$klass,
|
|
|
95 |
'x-amz-request-id'
|
|
|
96 |
),
|
|
|
97 |
'XAmznRequestId' => self::getAwsExceptionHeader(
|
|
|
98 |
$klass,
|
|
|
99 |
'x-amzn-RequestId'
|
|
|
100 |
),
|
|
|
101 |
];
|
|
|
102 |
}
|
|
|
103 |
if ($klass instanceof \Exception) {
|
|
|
104 |
return [
|
|
|
105 |
'HttpStatusCode' => self::getExceptionHttpStatusCode($klass),
|
|
|
106 |
'SdkException' => substr(
|
|
|
107 |
self::getExceptionCode($klass),
|
|
|
108 |
0,
|
|
|
109 |
128
|
|
|
110 |
),
|
|
|
111 |
'SdkExceptionMessage' => substr(
|
|
|
112 |
self::getExceptionMessage($klass),
|
|
|
113 |
0,
|
|
|
114 |
512
|
|
|
115 |
),
|
|
|
116 |
'XAmzId2' => self::getExceptionHeader($klass, 'x-amz-id-2'),
|
|
|
117 |
'XAmzRequestId' => self::getExceptionHeader($klass, 'x-amz-request-id'),
|
|
|
118 |
'XAmznRequestId' => self::getExceptionHeader($klass, 'x-amzn-RequestId'),
|
|
|
119 |
];
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
throw new \InvalidArgumentException('Parameter must be an instance of ResultInterface, AwsException or Exception.');
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
private static function getResultAttemptLatency(ResultInterface $result)
|
|
|
126 |
{
|
|
|
127 |
if (isset($result['@metadata']['transferStats']['http'])) {
|
|
|
128 |
$attempt = end($result['@metadata']['transferStats']['http']);
|
|
|
129 |
if (isset($attempt['total_time'])) {
|
|
|
130 |
return (int) floor($attempt['total_time'] * 1000);
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
return null;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
private static function getResultDestinationIp(ResultInterface $result)
|
|
|
137 |
{
|
|
|
138 |
if (isset($result['@metadata']['transferStats']['http'])) {
|
|
|
139 |
$attempt = end($result['@metadata']['transferStats']['http']);
|
|
|
140 |
if (isset($attempt['primary_ip'])) {
|
|
|
141 |
return $attempt['primary_ip'];
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
return null;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
private static function getResultDnsLatency(ResultInterface $result)
|
|
|
148 |
{
|
|
|
149 |
if (isset($result['@metadata']['transferStats']['http'])) {
|
|
|
150 |
$attempt = end($result['@metadata']['transferStats']['http']);
|
|
|
151 |
if (isset($attempt['namelookup_time'])) {
|
|
|
152 |
return (int) floor($attempt['namelookup_time'] * 1000);
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
return null;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
private static function getResultHttpStatusCode(ResultInterface $result)
|
|
|
159 |
{
|
|
|
160 |
return $result['@metadata']['statusCode'];
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
private static function getAwsExceptionAttemptLatency(AwsException $e) {
|
|
|
164 |
$attempt = $e->getTransferInfo();
|
|
|
165 |
if (isset($attempt['total_time'])) {
|
|
|
166 |
return (int) floor($attempt['total_time'] * 1000);
|
|
|
167 |
}
|
|
|
168 |
return null;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
private static function getAwsExceptionErrorCode(AwsException $e) {
|
|
|
172 |
return $e->getAwsErrorCode();
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
private static function getAwsExceptionMessage(AwsException $e) {
|
|
|
176 |
return $e->getAwsErrorMessage();
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
private static function getAwsExceptionDestinationIp(AwsException $e) {
|
|
|
180 |
$attempt = $e->getTransferInfo();
|
|
|
181 |
if (isset($attempt['primary_ip'])) {
|
|
|
182 |
return $attempt['primary_ip'];
|
|
|
183 |
}
|
|
|
184 |
return null;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
private static function getAwsExceptionDnsLatency(AwsException $e) {
|
|
|
188 |
$attempt = $e->getTransferInfo();
|
|
|
189 |
if (isset($attempt['namelookup_time'])) {
|
|
|
190 |
return (int) floor($attempt['namelookup_time'] * 1000);
|
|
|
191 |
}
|
|
|
192 |
return null;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
private static function getAwsExceptionHttpStatusCode(AwsException $e) {
|
|
|
196 |
$response = $e->getResponse();
|
|
|
197 |
if ($response !== null) {
|
|
|
198 |
return $response->getStatusCode();
|
|
|
199 |
}
|
|
|
200 |
return null;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
private static function getExceptionHttpStatusCode(\Exception $e) {
|
|
|
204 |
if ($e instanceof ResponseContainerInterface) {
|
|
|
205 |
$response = $e->getResponse();
|
|
|
206 |
if ($response instanceof ResponseInterface) {
|
|
|
207 |
return $response->getStatusCode();
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
return null;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
private static function getExceptionCode(\Exception $e) {
|
|
|
214 |
if (!($e instanceof AwsException)) {
|
|
|
215 |
return get_class($e);
|
|
|
216 |
}
|
|
|
217 |
return null;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
private static function getExceptionMessage(\Exception $e) {
|
|
|
221 |
if (!($e instanceof AwsException)) {
|
|
|
222 |
return $e->getMessage();
|
|
|
223 |
}
|
|
|
224 |
return null;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* {@inheritdoc}
|
|
|
229 |
*/
|
|
|
230 |
protected function populateRequestEventData(
|
|
|
231 |
CommandInterface $cmd,
|
|
|
232 |
RequestInterface $request,
|
|
|
233 |
array $event
|
|
|
234 |
) {
|
|
|
235 |
$event = parent::populateRequestEventData($cmd, $request, $event);
|
|
|
236 |
$event['Type'] = 'ApiCallAttempt';
|
|
|
237 |
return $event;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* {@inheritdoc}
|
|
|
242 |
*/
|
|
|
243 |
protected function populateResultEventData(
|
|
|
244 |
$result,
|
|
|
245 |
array $event
|
|
|
246 |
) {
|
|
|
247 |
$event = parent::populateResultEventData($result, $event);
|
|
|
248 |
|
|
|
249 |
$provider = $this->credentialProvider;
|
|
|
250 |
/** @var CredentialsInterface $credentials */
|
|
|
251 |
$credentials = $provider()->wait();
|
|
|
252 |
$event['AccessKey'] = $credentials->getAccessKeyId();
|
|
|
253 |
$sessionToken = $credentials->getSecurityToken();
|
|
|
254 |
if ($sessionToken !== null) {
|
|
|
255 |
$event['SessionToken'] = $sessionToken;
|
|
|
256 |
}
|
|
|
257 |
if (empty($event['AttemptLatency'])) {
|
|
|
258 |
$event['AttemptLatency'] = (int) (floor(microtime(true) * 1000) - $event['Timestamp']);
|
|
|
259 |
}
|
|
|
260 |
return $event;
|
|
|
261 |
}
|
|
|
262 |
}
|