bavix /
laravel-prof
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bavix\Prof\Services; |
||
| 4 | |||
| 5 | use Bavix\Prof\Models\ProfileLogEntry; |
||
| 6 | use Illuminate\Support\Facades\Auth; |
||
| 7 | use Illuminate\Support\Str; |
||
| 8 | |||
| 9 | class ProfileLogService |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $hostname; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $clientIp; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $requestId; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $version; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int|string|null |
||
| 34 | */ |
||
| 35 | protected $userId; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var float[] |
||
| 39 | */ |
||
| 40 | protected $ticks = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * ProfileLogService constructor. |
||
| 44 | */ |
||
| 45 | public function __construct() |
||
| 46 | { |
||
| 47 | $this->requestId = $this->requestId(\config('prof.requestIdLength', 16)); |
||
| 48 | $this->clientIp = \request()->getClientIp(); |
||
| 49 | $this->version = \app()->version(); |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 50 | $this->hostname = \gethostname(); |
||
| 51 | $this->userId = Auth::id(); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $eventName |
||
| 56 | * @param string|null $target |
||
| 57 | */ |
||
| 58 | public function tick(string $eventName, ?string $target = null): void |
||
| 59 | { |
||
| 60 | $currentTime = \microtime(true); |
||
| 61 | if (empty($this->ticks[$eventName])) { |
||
| 62 | if (!$target) { |
||
| 63 | $target = \request()->getRequestUri(); |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->ticks[$eventName] = [$currentTime, $target]; |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | // load data from Tick |
||
| 71 | [$tickTime, $target] = $this->ticks[$eventName]; |
||
| 72 | |||
| 73 | $entry = new ProfileLogEntry(); |
||
| 74 | $entry->fill([ |
||
| 75 | 'hostname' => $this->hostname, |
||
| 76 | 'project' => \config('app.name', 'bavix/laravel-prof'), |
||
| 77 | 'version' => $this->version, |
||
| 78 | 'userId' => $this->userId, |
||
| 79 | 'sessionId' => \session()->getId(), |
||
| 80 | 'requestId' => $this->requestId, |
||
| 81 | 'requestIp' => $this->clientIp, |
||
| 82 | 'eventName' => $eventName, |
||
| 83 | 'target' => $target, |
||
| 84 | 'latency' => $currentTime - $tickTime, |
||
| 85 | 'memoryPeak' => \memory_get_usage(true), |
||
| 86 | 'date' => $tickTime, |
||
| 87 | 'created' => $tickTime, |
||
| 88 | ]); |
||
| 89 | |||
| 90 | // save via queue if enabled |
||
| 91 | $entry->save(); |
||
| 92 | |||
| 93 | unset($this->ticks[$eventName]); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function recordAllTicks(): void |
||
| 100 | { |
||
| 101 | foreach ($this->ticks as $eventName => $tickData) { |
||
| 102 | $this->tick($eventName); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Identification of the request (request signature). |
||
| 108 | * |
||
| 109 | * @param int $length |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function requestId(int $length = 16): string |
||
| 113 | { |
||
| 114 | try { |
||
| 115 | $reqId = \random_bytes($length >> 1); |
||
| 116 | } catch (\Throwable $e) { |
||
| 117 | $reqId = Str::random($length >> 1); |
||
| 118 | } finally { |
||
| 119 | return \bin2hex($reqId); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | } |
||
| 124 |