PluginProbe ʕ •ᴥ•ʔ
Transferito: WP Migration / trunk
Transferito: WP Migration vtrunk
trunk 11.4.0 12.0.0 13.1.0 14.0.0 14.0.11 14.0.7 14.1.0 14.1.1 14.1.2 14.1.3 14.1.4
transferito / vendor / aws / aws-sdk-php / src / ClientSideMonitoring / ApiCallMonitoringMiddleware.php
transferito / vendor / aws / aws-sdk-php / src / ClientSideMonitoring Last commit date
Exception 10 months ago AbstractMonitoringMiddleware.php 10 months ago ApiCallAttemptMonitoringMiddleware.php 10 months ago ApiCallMonitoringMiddleware.php 10 months ago Configuration.php 10 months ago ConfigurationInterface.php 10 months ago ConfigurationProvider.php 10 months ago MonitoringMiddlewareInterface.php 10 months ago
ApiCallMonitoringMiddleware.php
177 lines
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 * @internal
13 */
14 class ApiCallMonitoringMiddleware extends AbstractMonitoringMiddleware
15 {
16
17 /**
18 * Api Call Attempt event keys for each Api Call event key
19 *
20 * @var array
21 */
22 private static $eventKeys = [
23 'FinalAwsException' => 'AwsException',
24 'FinalAwsExceptionMessage' => 'AwsExceptionMessage',
25 'FinalSdkException' => 'SdkException',
26 'FinalSdkExceptionMessage' => 'SdkExceptionMessage',
27 'FinalHttpStatusCode' => 'HttpStatusCode',
28 ];
29
30 /**
31 * Standard middleware wrapper function with CSM options passed in.
32 *
33 * @param callable $credentialProvider
34 * @param mixed $options
35 * @param string $region
36 * @param string $service
37 * @return callable
38 */
39 public static function wrap(
40 callable $credentialProvider,
41 $options,
42 $region,
43 $service
44 ) {
45 return function (callable $handler) use (
46 $credentialProvider,
47 $options,
48 $region,
49 $service
50 ) {
51 return new static(
52 $handler,
53 $credentialProvider,
54 $options,
55 $region,
56 $service
57 );
58 };
59 }
60
61 /**
62 * {@inheritdoc}
63 */
64 public static function getRequestData(RequestInterface $request)
65 {
66 return [];
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public static function getResponseData($klass)
73 {
74 if ($klass instanceof ResultInterface) {
75 $data = [
76 'AttemptCount' => self::getResultAttemptCount($klass),
77 'MaxRetriesExceeded' => 0,
78 ];
79 } elseif ($klass instanceof \Exception) {
80 $data = [
81 'AttemptCount' => self::getExceptionAttemptCount($klass),
82 'MaxRetriesExceeded' => self::getMaxRetriesExceeded($klass),
83 ];
84 } else {
85 throw new \InvalidArgumentException('Parameter must be an instance of ResultInterface or Exception.');
86 }
87
88 return $data + self::getFinalAttemptData($klass);
89 }
90
91 private static function getResultAttemptCount(ResultInterface $result) {
92 if (isset($result['@metadata']['transferStats']['http'])) {
93 return count($result['@metadata']['transferStats']['http']);
94 }
95 return 1;
96 }
97
98 private static function getExceptionAttemptCount(\Exception $e) {
99 $attemptCount = 0;
100 if ($e instanceof MonitoringEventsInterface) {
101 foreach ($e->getMonitoringEvents() as $event) {
102 if (isset($event['Type']) &&
103 $event['Type'] === 'ApiCallAttempt') {
104 $attemptCount++;
105 }
106 }
107
108 }
109 return $attemptCount;
110 }
111
112 private static function getFinalAttemptData($klass)
113 {
114 $data = [];
115 if ($klass instanceof MonitoringEventsInterface) {
116 $finalAttempt = self::getFinalAttempt($klass->getMonitoringEvents());
117
118 if (!empty($finalAttempt)) {
119 foreach (self::$eventKeys as $callKey => $attemptKey) {
120 if (isset($finalAttempt[$attemptKey])) {
121 $data[$callKey] = $finalAttempt[$attemptKey];
122 }
123 }
124 }
125 }
126
127 return $data;
128 }
129
130 private static function getFinalAttempt(array $events)
131 {
132 for (end($events); key($events) !== null; prev($events)) {
133 $current = current($events);
134 if (isset($current['Type'])
135 && $current['Type'] === 'ApiCallAttempt'
136 ) {
137 return $current;
138 }
139 }
140
141 return null;
142 }
143
144 private static function getMaxRetriesExceeded($klass)
145 {
146 if ($klass instanceof AwsException && $klass->isMaxRetriesExceeded()) {
147 return 1;
148 }
149 return 0;
150 }
151
152 /**
153 * {@inheritdoc}
154 */
155 protected function populateRequestEventData(
156 CommandInterface $cmd,
157 RequestInterface $request,
158 array $event
159 ) {
160 $event = parent::populateRequestEventData($cmd, $request, $event);
161 $event['Type'] = 'ApiCall';
162 return $event;
163 }
164
165 /**
166 * {@inheritdoc}
167 */
168 protected function populateResultEventData(
169 $result,
170 array $event
171 ) {
172 $event = parent::populateResultEventData($result, $event);
173 $event['Latency'] = (int) (floor(microtime(true) * 1000) - $event['Timestamp']);
174 return $event;
175 }
176 }
177