| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\ClientSideMonitoring; |
| 4 |
|
| 5 |
use Aws\CommandInterface; |
| 6 |
use Aws\Exception\AwsException; |
| 7 |
use Aws\MonitoringEventsInterface; |
| 8 |
use Aws\ResultInterface; |
| 9 |
use Psr\Http\Message\RequestInterface; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ApiCallMonitoringMiddleware extends AbstractMonitoringMiddleware |
| 15 |
{ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private static $eventKeys = [ |
| 22 |
'FinalAwsException' => 'AwsException', |
| 23 |
'FinalAwsExceptionMessage' => 'AwsExceptionMessage', |
| 24 |
'FinalSdkException' => 'SdkException', |
| 25 |
'FinalSdkExceptionMessage' => 'SdkExceptionMessage', |
| 26 |
'FinalHttpStatusCode' => 'HttpStatusCode', |
| 27 |
]; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static function wrap( |
| 39 |
callable $credentialProvider, |
| 40 |
$options, |
| 41 |
$region, |
| 42 |
$service |
| 43 |
) { |
| 44 |
return function (callable $handler) use ( |
| 45 |
$credentialProvider, |
| 46 |
$options, |
| 47 |
$region, |
| 48 |
$service |
| 49 |
) { |
| 50 |
return new static( |
| 51 |
$handler, |
| 52 |
$credentialProvider, |
| 53 |
$options, |
| 54 |
$region, |
| 55 |
$service |
| 56 |
); |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function getRequestData(RequestInterface $request) |
| 64 |
{ |
| 65 |
return []; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static function getResponseData($klass) |
| 72 |
{ |
| 73 |
if ($klass instanceof ResultInterface) { |
| 74 |
$data = [ |
| 75 |
'AttemptCount' => self::getResultAttemptCount($klass), |
| 76 |
'MaxRetriesExceeded' => 0, |
| 77 |
]; |
| 78 |
} elseif ($klass instanceof \Exception) { |
| 79 |
$data = [ |
| 80 |
'AttemptCount' => self::getExceptionAttemptCount($klass), |
| 81 |
'MaxRetriesExceeded' => self::getMaxRetriesExceeded($klass), |
| 82 |
]; |
| 83 |
} else { |
| 84 |
throw new \InvalidArgumentException('Parameter must be an instance of ResultInterface or Exception.'); |
| 85 |
} |
| 86 |
|
| 87 |
return $data + self::getFinalAttemptData($klass); |
| 88 |
} |
| 89 |
|
| 90 |
private static function getResultAttemptCount(ResultInterface $result) { |
| 91 |
if (isset($result['@metadata']['transferStats']['http'])) { |
| 92 |
return count($result['@metadata']['transferStats']['http']); |
| 93 |
} |
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
private static function getExceptionAttemptCount(\Exception $e) { |
| 98 |
$attemptCount = 0; |
| 99 |
if ($e instanceof MonitoringEventsInterface) { |
| 100 |
foreach ($e->getMonitoringEvents() as $event) { |
| 101 |
if (isset($event['Type']) && |
| 102 |
$event['Type'] === 'ApiCallAttempt') { |
| 103 |
$attemptCount++; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
return $attemptCount; |
| 109 |
} |
| 110 |
|
| 111 |
private static function getFinalAttemptData($klass) |
| 112 |
{ |
| 113 |
$data = []; |
| 114 |
if ($klass instanceof MonitoringEventsInterface) { |
| 115 |
$finalAttempt = self::getFinalAttempt($klass->getMonitoringEvents()); |
| 116 |
|
| 117 |
if (!empty($finalAttempt)) { |
| 118 |
foreach (self::$eventKeys as $callKey => $attemptKey) { |
| 119 |
if (isset($finalAttempt[$attemptKey])) { |
| 120 |
$data[$callKey] = $finalAttempt[$attemptKey]; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $data; |
| 127 |
} |
| 128 |
|
| 129 |
private static function getFinalAttempt(array $events) |
| 130 |
{ |
| 131 |
for (end($events); key($events) !== null; prev($events)) { |
| 132 |
$current = current($events); |
| 133 |
if (isset($current['Type']) |
| 134 |
&& $current['Type'] === 'ApiCallAttempt' |
| 135 |
) { |
| 136 |
return $current; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return null; |
| 141 |
} |
| 142 |
|
| 143 |
private static function getMaxRetriesExceeded($klass) |
| 144 |
{ |
| 145 |
if ($klass instanceof AwsException && $klass->isMaxRetriesExceeded()) { |
| 146 |
return 1; |
| 147 |
} |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
protected function populateRequestEventData( |
| 155 |
CommandInterface $cmd, |
| 156 |
RequestInterface $request, |
| 157 |
array $event |
| 158 |
) { |
| 159 |
$event = parent::populateRequestEventData($cmd, $request, $event); |
| 160 |
$event['Type'] = 'ApiCall'; |
| 161 |
return $event; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
protected function populateResultEventData( |
| 168 |
$result, |
| 169 |
array $event |
| 170 |
) { |
| 171 |
$event = parent::populateResultEventData($result, $event); |
| 172 |
$event['Latency'] = (int) (floor(microtime(true) * 1000) - $event['Timestamp']); |
| 173 |
return $event; |
| 174 |
} |
| 175 |
} |