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 / ApiCallMonitoringMiddleware.php

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

127 lines 4.4 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\Exception\AwsException;
7 use Dudlewebs\WPMCS\s3\Aws\MonitoringEventsInterface;
8 use Dudlewebs\WPMCS\s3\Aws\ResultInterface;
9 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
10 /**
11 * @internal
12 */
13 class ApiCallMonitoringMiddleware extends AbstractMonitoringMiddleware
14 {
15 /**
16 * Api Call Attempt event keys for each Api Call event key
17 *
18 * @var array
19 */
20 private static $eventKeys = ['FinalAwsException' => 'AwsException', 'FinalAwsExceptionMessage' => 'AwsExceptionMessage', 'FinalSdkException' => 'SdkException', 'FinalSdkExceptionMessage' => 'SdkExceptionMessage', 'FinalHttpStatusCode' => 'HttpStatusCode'];
21 /**
22 * Standard middleware wrapper function with CSM options passed in.
23 *
24 * @param callable $credentialProvider
25 * @param mixed $options
26 * @param string $region
27 * @param string $service
28 * @return callable
29 */
30 public static function wrap(callable $credentialProvider, $options, $region, $service)
31 {
32 return function (callable $handler) use($credentialProvider, $options, $region, $service) {
33 return new static($handler, $credentialProvider, $options, $region, $service);
34 };
35 }
36 /**
37 * {@inheritdoc}
38 */
39 public static function getRequestData(RequestInterface $request)
40 {
41 return [];
42 }
43 /**
44 * {@inheritdoc}
45 */
46 public static function getResponseData($klass)
47 {
48 if ($klass instanceof ResultInterface) {
49 $data = ['AttemptCount' => self::getResultAttemptCount($klass), 'MaxRetriesExceeded' => 0];
50 } elseif ($klass instanceof \Exception) {
51 $data = ['AttemptCount' => self::getExceptionAttemptCount($klass), 'MaxRetriesExceeded' => self::getMaxRetriesExceeded($klass)];
52 } else {
53 throw new \InvalidArgumentException('Parameter must be an instance of ResultInterface or Exception.');
54 }
55 return $data + self::getFinalAttemptData($klass);
56 }
57 private static function getResultAttemptCount(ResultInterface $result)
58 {
59 if (isset($result['@metadata']['transferStats']['http'])) {
60 return \count($result['@metadata']['transferStats']['http']);
61 }
62 return 1;
63 }
64 private static function getExceptionAttemptCount(\Exception $e)
65 {
66 $attemptCount = 0;
67 if ($e instanceof MonitoringEventsInterface) {
68 foreach ($e->getMonitoringEvents() as $event) {
69 if (isset($event['Type']) && $event['Type'] === 'ApiCallAttempt') {
70 $attemptCount++;
71 }
72 }
73 }
74 return $attemptCount;
75 }
76 private static function getFinalAttemptData($klass)
77 {
78 $data = [];
79 if ($klass instanceof MonitoringEventsInterface) {
80 $finalAttempt = self::getFinalAttempt($klass->getMonitoringEvents());
81 if (!empty($finalAttempt)) {
82 foreach (self::$eventKeys as $callKey => $attemptKey) {
83 if (isset($finalAttempt[$attemptKey])) {
84 $data[$callKey] = $finalAttempt[$attemptKey];
85 }
86 }
87 }
88 }
89 return $data;
90 }
91 private static function getFinalAttempt(array $events)
92 {
93 for (\end($events); \key($events) !== null; \prev($events)) {
94 $current = \current($events);
95 if (isset($current['Type']) && $current['Type'] === 'ApiCallAttempt') {
96 return $current;
97 }
98 }
99 return null;
100 }
101 private static function getMaxRetriesExceeded($klass)
102 {
103 if ($klass instanceof AwsException && $klass->isMaxRetriesExceeded()) {
104 return 1;
105 }
106 return 0;
107 }
108 /**
109 * {@inheritdoc}
110 */
111 protected function populateRequestEventData(CommandInterface $cmd, RequestInterface $request, array $event)
112 {
113 $event = parent::populateRequestEventData($cmd, $request, $event);
114 $event['Type'] = 'ApiCall';
115 return $event;
116 }
117 /**
118 * {@inheritdoc}
119 */
120 protected function populateResultEventData($result, array $event)
121 {
122 $event = parent::populateResultEventData($result, $event);
123 $event['Latency'] = (int) (\floor(\microtime(\true) * 1000) - $event['Timestamp']);
124 return $event;
125 }
126 }
127