Skip to content

Commit

Permalink
feat: added main functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
huzaifaarain committed Dec 13, 2023
1 parent 09ec7c6 commit f4ba92b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "huzaifaarain/laravel-pulse-guzzle-recorder",
"description": "Pulse Guzzle Recorder provide a custom recorder for intercepting http requests made via guzzlehttp/guzzle php library. The package uses the on_stats request option for extracting the request/response data. The recorder intercept and log the request into the Laravel Pulse Slow Outgoing Requests.",
"name": "muhammadhuzaifa/laravel-pulse-guzzle-recorder",
"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.",
"keywords": [
"MuhammadHuzaifa",
"laravel",
Expand All @@ -17,6 +17,7 @@
],
"require": {
"php": "^8.1",
"guzzlehttp/guzzle": "^7.8",
"illuminate/contracts": "^10.0",
"laravel/pulse": "^1.0@beta",
"spatie/laravel-package-tools": "^1.14.0"
Expand All @@ -35,8 +36,7 @@
},
"autoload": {
"psr-4": {
"MuhammadHuzaifa\\LaravelPulseGuzzleRecorder\\": "src/",
"MuhammadHuzaifa\\LaravelPulseGuzzleRecorder\\Database\\Factories\\": "database/factories/"
"MuhammadHuzaifa\\LaravelPulseGuzzleRecorder\\": "src/"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -75,9 +75,7 @@
"providers": [
"MuhammadHuzaifa\\LaravelPulseGuzzleRecorder\\LaravelPulseGuzzleRecorderServiceProvider"
],
"aliases": {
"LaravelPulseGuzzleRecorder": "MuhammadHuzaifa\\LaravelPulseGuzzleRecorder\\Facades\\LaravelPulseGuzzleRecorder"
}
"aliases": {}
}
},
"minimum-stability": "dev",
Expand Down
16 changes: 0 additions & 16 deletions src/Facades/LaravelPulseGuzzleRecorder.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/LaravelPulseGuzzleRecorder.php

This file was deleted.

49 changes: 49 additions & 0 deletions src/Recorders/LaravelPulseGuzzleRecorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace MuhammadHuzaifa\LaravelPulseGuzzleRecorder\Recorders;

use Carbon\CarbonImmutable;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Utils;
use Laravel\Pulse\Recorders\SlowOutgoingRequests;
use Illuminate\Contracts\Foundation\Application;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class LaravelPulseGuzzleRecorder
{
/**
* Register the recorder.
*/
public function register(Application $app): void
{
$app->bind(Client::class, function () {
$stack = new HandlerStack();
$stack->setHandler(Utils::chooseHandler());
$stack->push($this->middleware());
return new Client(['handler' => $stack]);
});
}



public function middleware(): callable
{
return function (callable $handler) {
return function (
RequestInterface $request,
array $options
) use ($handler) {
$startedAt = CarbonImmutable::now();
$promise = $handler($request, $options);
return $promise->then(
function (ResponseInterface $response) use ($request, $startedAt) {
app(SlowOutgoingRequests::class)->record($request, $startedAt->getTimestampMs());
return $response;
}
);
};
};
}
}

0 comments on commit f4ba92b

Please sign in to comment.