Last active
May 28, 2025 17:06
-
-
Save spaantje/9718360e04185fc707a98eff19ce05cf to your computer and use it in GitHub Desktop.
GuzzleHandlerStack.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use GuzzleHttp\Client; | |
| use App\Guzzle\GuzzleHandlerStack; | |
| $guzzle = new Client([ | |
| 'base_uri' => 'https://example.com/api/', | |
| 'handler' => GuzzleHandlerStack::create(), | |
| ]); | |
| $response = $guzzle->post('suburi', [ | |
| 'json' => $data | |
| ]); | |
| // etc | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Exceptions; | |
| use Exception; | |
| use Laravel\Telescope\Telescope; | |
| use Laravel\Telescope\IncomingEntry; | |
| use GuzzleHttp\Exception\ClientException; | |
| use GuzzleHttp\Exception\ServerException; | |
| use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | |
| class Handler extends ExceptionHandler | |
| { | |
| // removed the rest for clarity | |
| /** | |
| * Report or log an exception. | |
| * | |
| * This is a great spot to send exceptions to Sentry, Bugsnag, etc. | |
| * | |
| * @param Exception $exception | |
| * @return void | |
| * @throws Exception | |
| */ | |
| public function report(Exception $exception) | |
| { | |
| if ($exception instanceof ClientException) { | |
| $startTime = defined('SOME_TIMER') ? SOME_TIMER : null; | |
| $entry = IncomingEntry::make([ | |
| 'uri' => (string)$exception->getRequest()->getUri(), | |
| 'method' => $exception->getRequest()->getMethod(), | |
| 'headers' => $exception->getResponse()->getHeaders(), | |
| 'payload' => (string)$exception->getRequest()->getBody(), | |
| 'response_status' => $exception->getResponse()->getStatusCode(), | |
| 'response' => json_decode((string)$exception->getResponse()->getBody()), | |
| 'duration' => $startTime ? floor((microtime(true) - $startTime) * 1000) : null, | |
| 'memory' => round(memory_get_peak_usage(true) / 1024 / 1025, 1), | |
| ]); | |
| $entry->tags(['guzzle']); | |
| Telescope::recordRequest($entry); | |
| } | |
| if ($exception instanceof ServerException) { | |
| try { | |
| $startTime = defined('SOME_TIMER') ? SOME_TIMER : null; | |
| $entry = IncomingEntry::make([ | |
| 'uri' => (string)$exception->getRequest()->getUri(), | |
| 'method' => $exception->getRequest()->getMethod(), | |
| 'headers' => $exception->getResponse()->getHeaders(), | |
| 'payload' => (string)$exception->getRequest()->getBody(), | |
| 'response_status' => $exception->getResponse()->getStatusCode(), | |
| 'response' => json_decode((string)$exception->getResponse()->getBody()), | |
| 'duration' => $startTime ? floor((microtime(true) - $startTime) * 1000) : null, | |
| 'memory' => round(memory_get_peak_usage(true) / 1024 / 1025, 1), | |
| ]); | |
| $entry->tags(['guzzle']); | |
| Telescope::recordRequest($entry); | |
| } catch (Exception $e) { | |
| // Do nothing with this exception.. | |
| } | |
| } | |
| parent::report($exception); | |
| } | |
| // removed the rest for clarity | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Guzzle; | |
| use GuzzleHttp\HandlerStack; | |
| use Laravel\Telescope\Telescope; | |
| use Laravel\Telescope\IncomingEntry; | |
| use Psr\Http\Message\RequestInterface; | |
| use Psr\Http\Message\ResponseInterface; | |
| class GuzzleHandlerStack | |
| { | |
| public static function create() | |
| { | |
| $stack = HandlerStack::create(); | |
| $stack->unshift(self::telescope_request_logger()); | |
| return $stack; | |
| } | |
| public static function telescope_request_logger() | |
| { | |
| return function (callable $handler) { | |
| return function (RequestInterface $request, array $options) use ($handler) { | |
| $promise = $handler($request, $options); | |
| return $promise->then( | |
| function (ResponseInterface $response) use ($request, $options) { | |
| $startTime = defined('SOME_TIMER') ? SOME_TIMER : null; | |
| $entry = IncomingEntry::make([ | |
| 'uri' => (string)$request->getUri(), | |
| 'method' => $request->getMethod(), | |
| 'headers' => $response->getHeaders(), | |
| 'payload' => json_decode((string)$request->getBody()), | |
| 'response_status' => $response->getStatusCode(), | |
| 'response' => json_decode((string)$response->getBody()), | |
| 'duration' => $startTime ? floor((microtime(true) - $startTime) * 1000) : null, | |
| 'memory' => round(memory_get_peak_usage(true) / 1024 / 1025, 1), | |
| ]); | |
| $entry->tags(['guzzle']); | |
| Telescope::recordRequest($entry); | |
| return $response; | |
| } | |
| ); | |
| }; | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment