summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/ClientSideMonitoring/ApiCallAttemptMonitoringMiddleware.php
blob: 91810bb9cfe28d58b3f83a1d3adf3f2b748040e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php

namespace Aws\ClientSideMonitoring;

use Aws\CommandInterface;
use Aws\Credentials\CredentialsInterface;
use Aws\Exception\AwsException;
use Aws\ResponseContainerInterface;
use Aws\ResultInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * @internal
 */
class ApiCallAttemptMonitoringMiddleware extends AbstractMonitoringMiddleware
{

    /**
     * Standard middleware wrapper function with CSM options passed in.
     *
     * @param callable $credentialProvider
     * @param mixed  $options
     * @param string $region
     * @param string $service
     * @return callable
     */
    public static function wrap(
        callable $credentialProvider,
        $options,
        $region,
        $service
    ) {
        return function (callable $handler) use (
            $credentialProvider,
            $options,
            $region,
            $service
        ) {
            return new static(
                $handler,
                $credentialProvider,
                $options,
                $region,
                $service
            );
        };
    }

    /**
     * {@inheritdoc}
     */
    public static function getRequestData(RequestInterface $request)
    {
        return [
            'Fqdn' => $request->getUri()->getHost(),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function getResponseData($klass)
    {
        if ($klass instanceof ResultInterface) {
            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'),
            ];
        }
        if ($klass instanceof AwsException) {
            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'
                ),
            ];
        }
        if ($klass instanceof \Exception) {
            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'),
            ];
        }

        throw new \InvalidArgumentException('Parameter must be an instance of ResultInterface, AwsException or Exception.');
    }

    private static function getResultAttemptLatency(ResultInterface $result)
    {
        if (isset($result['@metadata']['transferStats']['http'])) {
            $attempt = end($result['@metadata']['transferStats']['http']);
            if (isset($attempt['total_time'])) {
                return (int) floor($attempt['total_time'] * 1000);
            }
        }
        return null;
    }

    private static function getResultDestinationIp(ResultInterface $result)
    {
        if (isset($result['@metadata']['transferStats']['http'])) {
            $attempt = end($result['@metadata']['transferStats']['http']);
            if (isset($attempt['primary_ip'])) {
                return $attempt['primary_ip'];
            }
        }
        return null;
    }

    private static function getResultDnsLatency(ResultInterface $result)
    {
        if (isset($result['@metadata']['transferStats']['http'])) {
            $attempt = end($result['@metadata']['transferStats']['http']);
            if (isset($attempt['namelookup_time'])) {
                return (int) floor($attempt['namelookup_time'] * 1000);
            }
        }
        return null;
    }

    private static function getResultHttpStatusCode(ResultInterface $result)
    {
        return $result['@metadata']['statusCode'];
    }

    private static function getAwsExceptionAttemptLatency(AwsException $e) {
        $attempt = $e->getTransferInfo();
        if (isset($attempt['total_time'])) {
            return (int) floor($attempt['total_time'] * 1000);
        }
        return null;
    }

    private static function getAwsExceptionErrorCode(AwsException $e) {
        return $e->getAwsErrorCode();
    }

    private static function getAwsExceptionMessage(AwsException $e) {
        return $e->getAwsErrorMessage();
    }

    private static function getAwsExceptionDestinationIp(AwsException $e) {
        $attempt = $e->getTransferInfo();
        if (isset($attempt['primary_ip'])) {
            return $attempt['primary_ip'];
        }
        return null;
    }

    private static function getAwsExceptionDnsLatency(AwsException $e) {
        $attempt = $e->getTransferInfo();
        if (isset($attempt['namelookup_time'])) {
            return (int) floor($attempt['namelookup_time'] * 1000);
        }
        return null;
    }

    private static function getAwsExceptionHttpStatusCode(AwsException $e) {
        $response = $e->getResponse();
        if ($response !== null) {
            return $response->getStatusCode();
        }
        return null;
    }

    private static function getExceptionHttpStatusCode(\Exception $e) {
        if ($e instanceof ResponseContainerInterface) {
            $response = $e->getResponse();
            if ($response instanceof ResponseInterface) {
                return $response->getStatusCode();
            }
        }
        return null;
    }

    private static function getExceptionCode(\Exception $e) {
        if (!($e instanceof AwsException)) {
            return get_class($e);
        }
        return null;
    }

    private static function getExceptionMessage(\Exception $e) {
        if (!($e instanceof AwsException)) {
            return $e->getMessage();
        }
        return null;
    }

    /**
     * {@inheritdoc}
     */
    protected function populateRequestEventData(
        CommandInterface $cmd,
        RequestInterface $request,
        array $event
    ) {
        $event = parent::populateRequestEventData($cmd, $request, $event);
        $event['Type'] = 'ApiCallAttempt';
        return $event;
    }

    /**
     * {@inheritdoc}
     */
    protected function populateResultEventData(
        $result,
        array $event
    ) {
        $event = parent::populateResultEventData($result, $event);

        $provider = $this->credentialProvider;
        /** @var CredentialsInterface $credentials */
        $credentials = $provider()->wait();
        $event['AccessKey'] = $credentials->getAccessKeyId();
        $sessionToken = $credentials->getSecurityToken();
        if ($sessionToken !== null) {
            $event['SessionToken'] = $sessionToken;
        }
        if (empty($event['AttemptLatency'])) {
            $event['AttemptLatency'] = (int) (floor(microtime(true) * 1000) - $event['Timestamp']);
        }
        return $event;
    }
}