|
| 1 | +<?php namespace Clockwork\DataSource; |
| 2 | + |
| 3 | +use Clockwork\Helpers\Serializer; |
| 4 | +use Clockwork\Helpers\StackTrace; |
| 5 | +use Clockwork\Request\Request; |
| 6 | + |
| 7 | +use GuzzleHttp\Client; |
| 8 | +use GuzzleHttp\HandlerStack; |
| 9 | +use GuzzleHttp\TransferStats; |
| 10 | +use GuzzleHttp\Exception\GuzzleException; |
| 11 | +use GuzzleHttp\Exception\RequestException; |
| 12 | +use GuzzleHttp\Promise\PromiseInterface; |
| 13 | +use Psr\Http\Message\RequestInterface; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | + |
| 16 | +// Data source for Guzzle HTTP client, provides executed HTTP requests |
| 17 | +class GuzzleDataSource extends DataSource |
| 18 | +{ |
| 19 | + // Sent HTTP requests |
| 20 | + protected $requests = []; |
| 21 | + |
| 22 | + // Whether to collect request and response content (json or form data) and raw content |
| 23 | + protected $collectContent = true; |
| 24 | + protected $collectRawContent = true; |
| 25 | + |
| 26 | + // Create a new data source instance |
| 27 | + public function __construct($collectContent = true, $collectRawContent = false) |
| 28 | + { |
| 29 | + $this->collectContent = $collectContent; |
| 30 | + $this->collectRawContent = $collectRawContent; |
| 31 | + } |
| 32 | + |
| 33 | + // Returns a new Guzzle instance, pre-configured with Clockwork support |
| 34 | + public function instance(array $config = []) |
| 35 | + { |
| 36 | + return new Client($this->configure($config)); |
| 37 | + } |
| 38 | + |
| 39 | + // Updates Guzzle configuration array with Clockwork support |
| 40 | + public function configure(array $config = []) |
| 41 | + { |
| 42 | + $handler = isset($config['handler']) ? $config['handler'] : HandlerStack::create(); |
| 43 | + |
| 44 | + $handler->push($this); |
| 45 | + |
| 46 | + $config['handler'] = $handler; |
| 47 | + |
| 48 | + return $config; |
| 49 | + } |
| 50 | + |
| 51 | + // Add sent notifications to the request |
| 52 | + public function resolve(Request $request) |
| 53 | + { |
| 54 | + $request->httpRequests = array_merge($request->httpRequests, $this->requests); |
| 55 | + |
| 56 | + return $request; |
| 57 | + } |
| 58 | + |
| 59 | + // Reset the data source to an empty state, clearing any collected data |
| 60 | + public function reset() |
| 61 | + { |
| 62 | + $this->requests = []; |
| 63 | + $this->executingRequests = []; |
| 64 | + } |
| 65 | + |
| 66 | + // Guzzle middleware implemenation, that does the requests logging itself |
| 67 | + public function __invoke(callable $handler): callable |
| 68 | + { |
| 69 | + return function(RequestInterface $request, array $options) use ($handler): PromiseInterface { |
| 70 | + $time = microtime(true); |
| 71 | + $stats = null; |
| 72 | + |
| 73 | + $originalOnStats = isset($options['on_stats']) ? $options['on_stats'] : null; |
| 74 | + $options['on_stats'] = function (TransferStats $transferStats) use (&$stats, $originalOnStats) { |
| 75 | + $stats = $transferStats->getHandlerStats(); |
| 76 | + if ($originalOnStats) $originalOnStats($transferStats); |
| 77 | + }; |
| 78 | + |
| 79 | + return $handler($request, $options) |
| 80 | + ->then(function(ResponseInterface $response) use ($request, $time, $stats) { |
| 81 | + $this->collectRequest($request, $response, $time, $stats); |
| 82 | + |
| 83 | + return $response; |
| 84 | + }, function(GuzzleException $exception) use ($request, $time, $stats) { |
| 85 | + $response = $exception instanceof RequestException ? $exception->getResponse() : null; |
| 86 | + $this->collectRequest($request, $response, $time, $stats); |
| 87 | + |
| 88 | + throw $exception; |
| 89 | + }); |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + // Collect a request-response pair |
| 94 | + protected function collectRequest($request, $response = null, $startTime = null, $stats = null) |
| 95 | + { |
| 96 | + $trace = StackTrace::get(); |
| 97 | + |
| 98 | + $request = (object) [ |
| 99 | + 'request' => (object) [ |
| 100 | + 'method' => $request->getMethod(), |
| 101 | + 'url' => $this->removeAuthFromUrl((string) $request->getUri()), |
| 102 | + 'headers' => $request->getHeaders(), |
| 103 | + 'content' => $this->collectContent ? $this->resolveRequestContent($request) : null, |
| 104 | + 'body' => $this->collectRawContent ? (string) $request->getBody() : null |
| 105 | + ], |
| 106 | + 'response' => (object) [ |
| 107 | + 'status' => (int) $response->getStatusCode(), |
| 108 | + 'headers' => $response->getHeaders(), |
| 109 | + 'content' => $this->collectContent ? json_decode((string) $response->getBody(), true) : null, |
| 110 | + 'body' => $this->collectRawContent ? (string) $response->getBody() : null |
| 111 | + ], |
| 112 | + 'stats' => $stats ? (object) [ |
| 113 | + 'timing' => isset($stats['total_time_us']) ? (object) [ |
| 114 | + 'lookup' => $stats['namelookup_time_us'] / 1000, |
| 115 | + 'connect' => ($stats['pretransfer_time_us'] - $stats['namelookup_time_us']) / 1000, |
| 116 | + 'waiting' => ($stats['starttransfer_time_us'] - $stats['pretransfer_time_us']) / 1000, |
| 117 | + 'transfer' => ($stats['total_time_us'] - $stats['starttransfer_time_us']) / 1000 |
| 118 | + ] : null, |
| 119 | + 'size' => (object) [ |
| 120 | + 'upload' => isset($stats['size_upload']) ? $stats['size_upload'] : null, |
| 121 | + 'download' => isset($stats['size_download']) ? $stats['size_download'] : null |
| 122 | + ], |
| 123 | + 'speed' => (object) [ |
| 124 | + 'upload' => isset($stats['speed_upload']) ? $stats['speed_upload'] : null, |
| 125 | + 'download' => isset($stats['speed_download']) ? $stats['speed_download'] : null |
| 126 | + ], |
| 127 | + 'hosts' => (object) [ |
| 128 | + 'local' => isset($stats['local_ip']) ? [ 'ip' => $stats['local_ip'], 'port' => $stats['local_port'] ] : null, |
| 129 | + 'remote' => isset($stats['primary_ip']) ? [ 'ip' => $stats['primary_ip'], 'port' => $stats['primary_port'] ] : null |
| 130 | + ], |
| 131 | + 'version' => isset($stats['http_version']) ? $stats['http_version'] : null |
| 132 | + ] : null, |
| 133 | + 'error' => null, |
| 134 | + 'time' => $startTime, |
| 135 | + 'duration' => (microtime(true) - $startTime) * 1000, |
| 136 | + 'trace' => (new Serializer)->trace($trace) |
| 137 | + ]; |
| 138 | + |
| 139 | + if ($this->passesFilters([ $request ])) { |
| 140 | + $this->requests[] = $request; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Resolve request content, with support for form data and json requests |
| 145 | + protected function resolveRequestContent($request) |
| 146 | + { |
| 147 | + $body = (string) $request->getBody(); |
| 148 | + $headers = $request->getHeaders(); |
| 149 | + |
| 150 | + if (isset($headers['Content-Type']) && $headers['Content-Type'][0] == 'application/x-www-form-urlencoded') { |
| 151 | + parse_str($body, $parameters); |
| 152 | + return $parameters; |
| 153 | + } elseif (isset($headers['Content-Type']) && strpos($headers['Content-Type'][0], 'json') !== false) { |
| 154 | + return json_decode($body, true); |
| 155 | + } |
| 156 | + |
| 157 | + return []; |
| 158 | + } |
| 159 | + |
| 160 | + // Removes username and password from the URL |
| 161 | + protected function removeAuthFromUrl($url) |
| 162 | + { |
| 163 | + return preg_replace('#^(.+?://)(.+?@)(.*)$#', '$1$3', $url); |
| 164 | + } |
| 165 | +} |
0 commit comments