diff --git a/src/Command/CommandMetrics.php b/src/Command/CommandMetrics.php index aabf4d0..49d498e 100644 --- a/src/Command/CommandMetrics.php +++ b/src/Command/CommandMetrics.php @@ -16,8 +16,8 @@ public static function write(CommandFinished $event): void $labels = CommandLabels::extractFromTask($event); - Prometheus::update('command_runs_total', 1, $labels); - Prometheus::update('command_run_seconds_total', Helper::duration(), $labels); + app()->terminating(fn () => Prometheus::update('command_runs_total', 1, $labels)); + app()->terminating(fn () => Prometheus::update('command_run_seconds_total', Helper::duration(), $labels)); } protected static function needToIgnoreCommand(?string $command): bool diff --git a/src/Guzzle/GuzzleMiddleware.php b/src/Guzzle/GuzzleMiddleware.php index c78e3b7..a724d04 100644 --- a/src/Guzzle/GuzzleMiddleware.php +++ b/src/Guzzle/GuzzleMiddleware.php @@ -47,8 +47,8 @@ public static function handleResponse(string $type, $start, string $host): void $labels = [$host]; - Prometheus::update('http_client_seconds_total', $end - $start, $labels); + app()->terminating(fn () => Prometheus::update('http_client_seconds_total', $end - $start, $labels)); - Prometheus::update('http_client_requests_total', 1, $labels); + app()->terminating(fn () => Prometheus::update('http_client_requests_total', 1, $labels)); } } diff --git a/src/HttpMiddleware/HttpMetricsMiddleware.php b/src/HttpMiddleware/HttpMetricsMiddleware.php index 834921f..92271f6 100644 --- a/src/HttpMiddleware/HttpMetricsMiddleware.php +++ b/src/HttpMiddleware/HttpMetricsMiddleware.php @@ -28,7 +28,7 @@ public function handle($request, Closure $next) $endTime = microtime(true); $duration = $endTime - $startTime; - $this->latencyProfiler->writeMetrics(Prometheus::bag(), $response->getStatusCode(), $duration); + app()->terminating(fn () => $this->latencyProfiler->writeMetrics(Prometheus::bag(), $response->getStatusCode(), $duration)); return $response; } diff --git a/src/Job/JobMiddleware.php b/src/Job/JobMiddleware.php index 72e4675..a970888 100644 --- a/src/Job/JobMiddleware.php +++ b/src/Job/JobMiddleware.php @@ -14,7 +14,7 @@ public function handle($job, $next) $next($job); $duration = microtime(true) - $start; - Prometheus::update('queue_job_runs_total', 1, $labels); - Prometheus::update('queue_job_run_seconds_total', $duration, $labels); + app()->terminating(fn () => Prometheus::update('queue_job_runs_total', 1, $labels)); + app()->terminating(fn () => Prometheus::update('queue_job_run_seconds_total', $duration, $labels)); } } diff --git a/src/Kafka/KafkaMetricsMiddleware.php b/src/Kafka/KafkaMetricsMiddleware.php index bea845d..01ac4f1 100644 --- a/src/Kafka/KafkaMetricsMiddleware.php +++ b/src/Kafka/KafkaMetricsMiddleware.php @@ -30,7 +30,7 @@ private function writeMetrics(Message $message, float $startKafka, KafkaResponse $duration = microtime(true) - $startKafka; $labels = KafkaLabels::extractFromMessage($message, $status->value); - Prometheus::update('kafka_runs_total', 1, $labels); - Prometheus::update('kafka_run_seconds_total', $duration, $labels); + app()->terminating(fn () => Prometheus::update('kafka_runs_total', 1, $labels)); + app()->terminating(fn () => Prometheus::update('kafka_run_seconds_total', $duration, $labels)); } } diff --git a/src/MetricsServiceProvider.php b/src/MetricsServiceProvider.php index 8d13546..9578d38 100644 --- a/src/MetricsServiceProvider.php +++ b/src/MetricsServiceProvider.php @@ -103,15 +103,15 @@ private function registerEventListeners(): void }); Event::listen(MessageLogged::class, function (MessageLogged $event) { - Prometheus::update('log_messages_count', 1, [$event->level]); + app()->terminating(fn () => Prometheus::update('log_messages_count', 1, [$event->level])); }); Event::listen(JobFailed::class, function (JobFailed $event) { - Prometheus::update('queue_job_failed_total', 1, JobLabels::extractFromJob($event->job)); + app()->terminating(fn () => Prometheus::update('queue_job_failed_total', 1, JobLabels::extractFromJob($event->job))); }); Event::listen(JobQueued::class, function (JobQueued $event) { - Prometheus::update('queue_job_dispatched_total', 1, JobLabels::extractFromJob($event->job)); + app()->terminating(fn () => Prometheus::update('queue_job_dispatched_total', 1, JobLabels::extractFromJob($event->job))); }); Bus::pipeThrough([ @@ -119,8 +119,8 @@ private function registerEventListeners(): void ]); Event::listen(ScheduledTaskFinished::class, function (ScheduledTaskFinished $event) { - Prometheus::update('task_runs_total', 1, TaskLabels::extractFromTask($event->task)); - Prometheus::update('task_run_seconds_total', $event->runtime, TaskLabels::extractFromTask($event->task)); + app()->terminating(fn () => Prometheus::update('task_runs_total', 1, TaskLabels::extractFromTask($event->task))); + app()->terminating(fn () => Prometheus::update('task_run_seconds_total', $event->runtime, TaskLabels::extractFromTask($event->task))); }); Event::listen(CommandFinished::class, function (CommandFinished $event) { diff --git a/src/Workers/WorkerUsage.php b/src/Workers/WorkerUsage.php index 431b6ab..e9e4fde 100644 --- a/src/Workers/WorkerUsage.php +++ b/src/Workers/WorkerUsage.php @@ -20,8 +20,8 @@ public function register(MetricsBag $metricsBag): void public function update(MetricsBag $metricsBag): void { if ($this->hasSwoole()) { - Prometheus::update('workers_total', $this->getTotal()); - Prometheus::update('workers_idle', $this->getIdle()); + app()->terminating(fn () => Prometheus::update('workers_total', $this->getTotal())); + app()->terminating(fn () => Prometheus::update('workers_idle', $this->getIdle())); } } diff --git a/tests/EventListenersTest.php b/tests/EventListenersTest.php index dec2035..a566b5a 100644 --- a/tests/EventListenersTest.php +++ b/tests/EventListenersTest.php @@ -43,6 +43,8 @@ ->withArgs(['log_messages_count', 1, ['info']]); logger()->info("hello"); + + app()->terminate(); }); test('test listen message job queued', function () { @@ -70,6 +72,8 @@ }; } Event::dispatch(new JobQueued(...$arr)); + + app()->terminate(); }); test('test listen message job processed', function () { @@ -93,6 +97,8 @@ }); Bus::dispatch($job); + + app()->terminate(); }); test('test listen message command finished', function () { @@ -120,6 +126,8 @@ }); Event::dispatch(new CommandFinished($command, $input, $output, $exitCode)); + + app()->terminate(); }); test('test listen message command finished skip', function () { diff --git a/tests/HttpMetricsMiddlewareTest.php b/tests/HttpMetricsMiddlewareTest.php index 98500b7..a09d9d9 100644 --- a/tests/HttpMetricsMiddlewareTest.php +++ b/tests/HttpMetricsMiddlewareTest.php @@ -29,5 +29,7 @@ return $expectedResponse; }); + $middleware->terminate($expectedRequest, $expectedResponse); + assertSame($expectedResponse, $response); });