|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Treblle\Laravel\Jobs; |
| 6 | + |
| 7 | +use Throwable; |
| 8 | +use Illuminate\Bus\Queueable; |
| 9 | +use Illuminate\Support\Facades\Log; |
| 10 | +use Illuminate\Support\Facades\Http; |
| 11 | +use Illuminate\Queue\SerializesModels; |
| 12 | +use Illuminate\Queue\InteractsWithQueue; |
| 13 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 14 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 15 | +use Treblle\Laravel\DataTransferObject\TrebllePayloadData; |
| 16 | + |
| 17 | +/** |
| 18 | + * Queue job for sending Treblle monitoring data asynchronously. |
| 19 | + * |
| 20 | + * @package Treblle\Laravel\Jobs |
| 21 | + */ |
| 22 | +final class SendTreblleData implements ShouldQueue |
| 23 | +{ |
| 24 | + use Dispatchable; |
| 25 | + use InteractsWithQueue; |
| 26 | + use Queueable; |
| 27 | + use SerializesModels; |
| 28 | + |
| 29 | + /** |
| 30 | + * The number of times the job may be attempted. |
| 31 | + * |
| 32 | + * @var int |
| 33 | + */ |
| 34 | + public int $tries = 3; |
| 35 | + |
| 36 | + /** |
| 37 | + * The number of seconds to wait before retrying the job. |
| 38 | + * |
| 39 | + * @var int |
| 40 | + */ |
| 41 | + public int $backoff = 5; |
| 42 | + |
| 43 | + /** |
| 44 | + * The number of seconds the job can run before timing out. |
| 45 | + * |
| 46 | + * @var int |
| 47 | + */ |
| 48 | + public int $timeout = 10; |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a new job instance. |
| 52 | + * |
| 53 | + * @param TrebllePayloadData $payloadData The pre-extracted Treblle payload data |
| 54 | + */ |
| 55 | + public function __construct( |
| 56 | + private readonly TrebllePayloadData $payloadData |
| 57 | + ) { |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Execute the job. |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function handle(): void |
| 65 | + { |
| 66 | + try { |
| 67 | + // JSON encode and compress in one pass for better memory efficiency |
| 68 | + $jsonPayload = json_encode($this->payloadData->toArray()); |
| 69 | + $compressedPayload = gzencode($jsonPayload, 6); |
| 70 | + |
| 71 | + $url = $this->getBaseUrl(); |
| 72 | + |
| 73 | + // Log the payload being sent (only in debug mode) |
| 74 | + if ($this->payloadData->debug) { |
| 75 | + Log::info('Treblle: Sending payload', [ |
| 76 | + 'url' => $url, |
| 77 | + 'api_key' => $this->payloadData->apiKey, |
| 78 | + 'sdk_token' => mb_substr($this->payloadData->sdkToken, 0, 10) . '...', |
| 79 | + 'payload_size' => mb_strlen($jsonPayload), |
| 80 | + 'compressed_size' => mb_strlen($compressedPayload), |
| 81 | + 'payload' => json_decode($jsonPayload, true), // Log as array for better readability |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + // Use Laravel's HTTP client for better integration and testing |
| 86 | + $response = Http::timeout(3) |
| 87 | + ->connectTimeout(3) |
| 88 | + ->withoutVerifying() |
| 89 | + ->withHeaders([ |
| 90 | + 'Content-Type' => 'application/json', |
| 91 | + 'Content-Encoding' => 'gzip', |
| 92 | + 'x-api-key' => $this->payloadData->sdkToken, |
| 93 | + 'Accept-Encoding' => 'gzip', |
| 94 | + ]) |
| 95 | + ->withBody($compressedPayload, 'application/json') |
| 96 | + ->post($url); |
| 97 | + |
| 98 | + // Log the response (only in debug mode) |
| 99 | + if ($this->payloadData->debug) { |
| 100 | + Log::info('Treblle: Response received', [ |
| 101 | + 'status_code' => $response->status(), |
| 102 | + 'headers' => $response->headers(), |
| 103 | + 'body' => $response->body(), |
| 104 | + ]); |
| 105 | + } |
| 106 | + } catch (Throwable $throwable) { |
| 107 | + // Always log errors (important for troubleshooting) |
| 108 | + Log::error('Treblle: Failed to send data', [ |
| 109 | + 'error' => $throwable->getMessage(), |
| 110 | + 'trace' => $this->payloadData->debug ? $throwable->getTraceAsString() : null, |
| 111 | + ]); |
| 112 | + |
| 113 | + if ($this->payloadData->debug) { |
| 114 | + throw $throwable; |
| 115 | + } |
| 116 | + |
| 117 | + // Let Laravel's queue system handle retry logic |
| 118 | + $this->fail($throwable); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Get the base URL for Treblle API. |
| 124 | + * |
| 125 | + * If a custom URL is provided, it will be used. Otherwise, a random |
| 126 | + * endpoint from the available Treblle servers is selected for load |
| 127 | + * balancing. |
| 128 | + * |
| 129 | + * @return string The Treblle API endpoint URL |
| 130 | + */ |
| 131 | + private function getBaseUrl(): string |
| 132 | + { |
| 133 | + $urls = [ |
| 134 | + 'https://rocknrolla.treblle.com', |
| 135 | + 'https://punisher.treblle.com', |
| 136 | + 'https://sicario.treblle.com', |
| 137 | + ]; |
| 138 | + |
| 139 | + return $this->payloadData->url ?? $urls[array_rand($urls)]; |
| 140 | + } |
| 141 | +} |
0 commit comments