├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── pulse-guzzle-recorder.php └── src ├── LaravelPulseGuzzleRecorderServiceProvider.php └── Recorders └── LaravelPulseGuzzleRecorder.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-pulse-guzzle-recorder` will be documented in this file. 4 | 5 | ## v1.1.1 - 2024-05-01 6 | 7 | ### What's Changed 8 | 9 | * build(deps): bump aglipanci/laravel-pint-action from 2.3.1 to 2.4 by @dependabot in https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/pull/5 10 | 11 | **Full Changelog**: https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/compare/v1.1.0...v1.1.1 12 | 13 | ## v1.1.0 - 2024-04-06 14 | 15 | ### What's Changed 16 | 17 | * build(deps): bump aglipanci/laravel-pint-action from 2.3.0 to 2.3.1 by @dependabot in https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/pull/1 18 | * Add support for Laravel 11 by @jkudish in https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/pull/4 19 | 20 | ### New Contributors 21 | 22 | * @dependabot made their first contribution in https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/pull/1 23 | * @jkudish made their first contribution in https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/pull/4 24 | 25 | **Full Changelog**: https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/compare/v1.0.0...v1.1.0 26 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) MuhammadHuzaifa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Pulse Guzzle Recorder 2 | 3 | ![Laravel Pulse Guzzle](https://pulse.laravel.com/build/assets/dashboard-transparent-4f4f666c.png) 4 | 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/muhammadhuzaifa/laravel-pulse-guzzle-recorder.svg?style=flat-square)](https://packagist.org/packages/muhammadhuzaifa/laravel-pulse-guzzle-recorder) 6 | [![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/huzaifaarain/laravel-pulse-guzzle-recorder/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/actions?query=workflow%3Arun-tests+branch%3Amain) 7 | [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/huzaifaarain/laravel-pulse-guzzle-recorder/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/muhammadhuzaifa/laravel-pulse-guzzle-recorder.svg?style=flat-square)](https://packagist.org/packages/muhammadhuzaifa/laravel-pulse-guzzle-recorder) 9 | 10 | Laravel Pulse Guzzle Recorder provide a custom recorder for intercepting http requests made via guzzlehttp/guzzle php library and log them into the Laravel Pulse Slow Outgoing Requests section. 11 | 12 | `LaravelPulseGuzzleRecorder` under the hood uses `Laravel\Pulse\Recorders\SlowOutgoingRequests::record` functionality, so the configuration passed to the said recorder will also affect this recorder. 13 | 14 | ## Installation 15 | 16 | You can install the package via composer: 17 | 18 | ```bash 19 | composer require muhammadhuzaifa/laravel-pulse-guzzle-recorder 20 | ``` 21 | 22 | Add the recorder `LaravelPulseGuzzleRecorder` in the `config/pulse.php` file 23 | 24 | ```php 25 | return [ 26 | 'recorders' => [ 27 | // after all of the builtin recorders 28 | \MuhammadHuzaifa\LaravelPulseGuzzleRecorder\Recorders\LaravelPulseGuzzleRecorder::class => true, 29 | ] 30 | ] 31 | ``` 32 | 33 | ## Usage 34 | 35 | If you are using Laravel builtin Http facade, then you don't need this package. This package is useful when working with `guzzlehttp/guzzle` using the `Client` class. The `Client` class must be resolved using the service container `app`. 36 | 37 | For example, if you are working with `hubspot/api-client` and would like to intercept the requests then you can create a custom `Client` provide it to the HubSpot `Factory` constructor. 38 | 39 | ```php 40 | $client = app(\GuzzleHttp\Client::class); 41 | $hubspot = \HubSpot\Factory::createWithAccessToken('access-token', $client); 42 | ``` 43 | 44 | ## Testing 45 | 46 | ```bash 47 | composer test 48 | ``` 49 | 50 | ## Changelog 51 | 52 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 53 | 54 | ## Credits 55 | 56 | - [Muhammad Huzaifa](https://github.com/huzaifaarain) 57 | - [All Contributors](../../contributors) 58 | 59 | ## License 60 | 61 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 62 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "muhammadhuzaifa/laravel-pulse-guzzle-recorder", 3 | "description": "Pulse Guzzle Recorder provide a custom recorder for intercepting http requests made via guzzlehttp/guzzle php library and add them into the pulse dashboard if the request is slow. The package uses the guzzle middleware for the data. The recorder intercept and log the request into the Laravel Pulse Slow Outgoing Requests.", 4 | "keywords": [ 5 | "MuhammadHuzaifa", 6 | "laravel", 7 | "laravel-pulse-guzzle-recorder" 8 | ], 9 | "homepage": "https://github.com/huzaifaarain/laravel-pulse-guzzle-recorder", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Muhammad Huzaifa", 14 | "email": "huzaifa.itgroup@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.1", 20 | "guzzlehttp/guzzle": "^7.8", 21 | "illuminate/contracts": "^10.0|^11.0", 22 | "laravel/pulse": "^1.0@beta|^1.0", 23 | "spatie/laravel-package-tools": "^1.14.0" 24 | }, 25 | "require-dev": { 26 | "laravel/pint": "^1.0", 27 | "nunomaduro/collision": "^7.8|^8.0", 28 | "larastan/larastan": "^2.0.1", 29 | "orchestra/testbench": "^8.8", 30 | "pestphp/pest": "^2.20", 31 | "pestphp/pest-plugin-arch": "^2.0", 32 | "pestphp/pest-plugin-laravel": "^2.0", 33 | "phpstan/extension-installer": "^1.1", 34 | "phpstan/phpstan-deprecation-rules": "^1.0", 35 | "phpstan/phpstan-phpunit": "^1.0" 36 | }, 37 | "autoload": { 38 | "psr-4": { 39 | "MuhammadHuzaifa\\LaravelPulseGuzzleRecorder\\": "src/" 40 | } 41 | }, 42 | "autoload-dev": { 43 | "psr-4": { 44 | "MuhammadHuzaifa\\LaravelPulseGuzzleRecorder\\Tests\\": "tests/", 45 | "Workbench\\App\\": "workbench/app/" 46 | } 47 | }, 48 | "scripts": { 49 | "post-autoload-dump": "@composer run prepare", 50 | "clear": "@php vendor/bin/testbench package:purge-laravel-pulse-guzzle-recorder --ansi", 51 | "prepare": "@php vendor/bin/testbench package:discover --ansi", 52 | "build": [ 53 | "@composer run prepare", 54 | "@php vendor/bin/testbench workbench:build --ansi" 55 | ], 56 | "start": [ 57 | "Composer\\Config::disableProcessTimeout", 58 | "@composer run build", 59 | "@php vendor/bin/testbench serve" 60 | ], 61 | "analyse": "vendor/bin/phpstan analyse", 62 | "test": "vendor/bin/pest", 63 | "test-coverage": "vendor/bin/pest --coverage", 64 | "format": "vendor/bin/pint" 65 | }, 66 | "config": { 67 | "sort-packages": true, 68 | "allow-plugins": { 69 | "pestphp/pest-plugin": true, 70 | "phpstan/extension-installer": true 71 | } 72 | }, 73 | "extra": { 74 | "laravel": { 75 | "providers": [ 76 | "MuhammadHuzaifa\\LaravelPulseGuzzleRecorder\\LaravelPulseGuzzleRecorderServiceProvider" 77 | ], 78 | "aliases": {} 79 | } 80 | }, 81 | "minimum-stability": "dev", 82 | "prefer-stable": true 83 | } 84 | -------------------------------------------------------------------------------- /config/pulse-guzzle-recorder.php: -------------------------------------------------------------------------------- 1 | name('laravel-pulse-guzzle-recorder'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Recorders/LaravelPulseGuzzleRecorder.php: -------------------------------------------------------------------------------- 1 | bind(Client::class, function () { 22 | $stack = new HandlerStack(); 23 | $stack->setHandler(Utils::chooseHandler()); 24 | $stack->push($this->middleware()); 25 | 26 | return new Client(['handler' => $stack]); 27 | }); 28 | } 29 | 30 | public function middleware(): callable 31 | { 32 | return function (callable $handler) { 33 | return function ( 34 | RequestInterface $request, 35 | array $options 36 | ) use ($handler) { 37 | $startedAt = CarbonImmutable::now(); 38 | $promise = $handler($request, $options); 39 | 40 | return $promise->then( 41 | function (ResponseInterface $response) use ($request, $startedAt) { 42 | app(SlowOutgoingRequests::class)->record($request, $startedAt->getTimestampMs()); 43 | 44 | return $response; 45 | } 46 | ); 47 | }; 48 | }; 49 | } 50 | } 51 | --------------------------------------------------------------------------------