From d57f8ea6862a55a9edb9d24fe032092f10dbe1e0 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Tue, 5 Nov 2024 14:22:38 +0400 Subject: [PATCH 1/5] DDEV-1966 update metrics in termination --- src/Command/CommandMetrics.php | 4 ++-- src/Guzzle/GuzzleMiddleware.php | 4 ++-- src/HttpMiddleware/HttpMetricsMiddleware.php | 10 ++++++++-- src/Job/JobMiddleware.php | 4 ++-- src/Kafka/KafkaMetricsMiddleware.php | 4 ++-- src/MetricsServiceProvider.php | 10 +++++----- src/Workers/WorkerUsage.php | 4 ++-- tests/EventListenersTest.php | 8 ++++++++ tests/HttpMetricsMiddlewareTest.php | 2 ++ 9 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/Command/CommandMetrics.php b/src/Command/CommandMetrics.php index aabf4d0..2cc801c 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..7741e24 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..97b3db4 100644 --- a/src/HttpMiddleware/HttpMetricsMiddleware.php +++ b/src/HttpMiddleware/HttpMetricsMiddleware.php @@ -10,6 +10,8 @@ class HttpMetricsMiddleware { + protected int|float $duration; + public function __construct( private readonly LatencyProfiler $latencyProfiler ) { @@ -27,9 +29,13 @@ public function handle($request, Closure $next) $response = $next($request); $endTime = microtime(true); - $duration = $endTime - $startTime; - $this->latencyProfiler->writeMetrics(Prometheus::bag(), $response->getStatusCode(), $duration); + $this->duration = $endTime - $startTime; return $response; } + + public function terminate(Request $request, Response $response): void + { + $this->latencyProfiler->writeMetrics(Prometheus::bag(), $response->getStatusCode(), $this->duration); + } } diff --git a/src/Job/JobMiddleware.php b/src/Job/JobMiddleware.php index 72e4675..97ea836 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..c47decb 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..f446b94 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..6697af2 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); }); From dcc422a6235aaa77fc61e3e3fc7378a41a573528 Mon Sep 17 00:00:00 2001 From: SiriusV Date: Tue, 5 Nov 2024 10:23:50 +0000 Subject: [PATCH 2/5] Fix styling --- src/Command/CommandMetrics.php | 4 ++-- src/Guzzle/GuzzleMiddleware.php | 4 ++-- src/Job/JobMiddleware.php | 4 ++-- src/Kafka/KafkaMetricsMiddleware.php | 4 ++-- src/MetricsServiceProvider.php | 10 +++++----- src/Workers/WorkerUsage.php | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Command/CommandMetrics.php b/src/Command/CommandMetrics.php index 2cc801c..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); - app()->terminating(fn() => Prometheus::update('command_runs_total', 1, $labels)); - app()->terminating(fn() => 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 7741e24..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]; - app()->terminating(fn() => Prometheus::update('http_client_seconds_total', $end - $start, $labels)); + app()->terminating(fn () => Prometheus::update('http_client_seconds_total', $end - $start, $labels)); - app()->terminating(fn() => Prometheus::update('http_client_requests_total', 1, $labels)); + app()->terminating(fn () => Prometheus::update('http_client_requests_total', 1, $labels)); } } diff --git a/src/Job/JobMiddleware.php b/src/Job/JobMiddleware.php index 97ea836..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; - app()->terminating(fn() => Prometheus::update('queue_job_runs_total', 1, $labels)); - app()->terminating(fn() => 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 c47decb..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); - app()->terminating(fn() => Prometheus::update('kafka_runs_total', 1, $labels)); - app()->terminating(fn() => 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 f446b94..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) { - app()->terminating(fn() => 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) { - app()->terminating(fn() => 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) { - app()->terminating(fn() => 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) { - 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))); + 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 6697af2..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()) { - app()->terminating(fn() => Prometheus::update('workers_total', $this->getTotal())); - app()->terminating(fn() => Prometheus::update('workers_idle', $this->getIdle())); + app()->terminating(fn () => Prometheus::update('workers_total', $this->getTotal())); + app()->terminating(fn () => Prometheus::update('workers_idle', $this->getIdle())); } } From 37bca0e9de6d90bb0bd3bc5fe17de055b5241269 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Fri, 6 Dec 2024 12:32:12 +0400 Subject: [PATCH 3/5] DDEV-1966 update metrics in termination --- src/HttpMiddleware/HttpMetricsMiddleware.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HttpMiddleware/HttpMetricsMiddleware.php b/src/HttpMiddleware/HttpMetricsMiddleware.php index 97b3db4..6303a05 100644 --- a/src/HttpMiddleware/HttpMetricsMiddleware.php +++ b/src/HttpMiddleware/HttpMetricsMiddleware.php @@ -6,7 +6,7 @@ use Ensi\LaravelMetrics\LatencyProfiler; use Ensi\LaravelPrometheus\Prometheus; use Illuminate\Http\Request; -use Illuminate\Http\Response; +use Symfony\Component\HttpFoundation\Response; class HttpMetricsMiddleware { From e168fb16183787c75e9a0b5edbb6f495e41046a8 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Fri, 6 Dec 2024 12:48:42 +0400 Subject: [PATCH 4/5] DDEV-1966 update metrics in termination --- src/HttpMiddleware/HttpMetricsMiddleware.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/HttpMiddleware/HttpMetricsMiddleware.php b/src/HttpMiddleware/HttpMetricsMiddleware.php index 6303a05..3e6c9ce 100644 --- a/src/HttpMiddleware/HttpMetricsMiddleware.php +++ b/src/HttpMiddleware/HttpMetricsMiddleware.php @@ -6,7 +6,7 @@ use Ensi\LaravelMetrics\LatencyProfiler; use Ensi\LaravelPrometheus\Prometheus; use Illuminate\Http\Request; -use Symfony\Component\HttpFoundation\Response; +use Illuminate\Http\Response; class HttpMetricsMiddleware { @@ -29,13 +29,9 @@ public function handle($request, Closure $next) $response = $next($request); $endTime = microtime(true); - $this->duration = $endTime - $startTime; + $duration = $endTime - $startTime; + app()->terminating(fn () => $this->latencyProfiler->writeMetrics(Prometheus::bag(), $response->getStatusCode(), $duration)); return $response; } - - public function terminate(Request $request, Response $response): void - { - $this->latencyProfiler->writeMetrics(Prometheus::bag(), $response->getStatusCode(), $this->duration); - } } From 79f01d361a8dd63e8a82bfdda51f4b42de05a69a Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Fri, 6 Dec 2024 12:51:00 +0400 Subject: [PATCH 5/5] DDEV-1966 update metrics in termination --- src/HttpMiddleware/HttpMetricsMiddleware.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/HttpMiddleware/HttpMetricsMiddleware.php b/src/HttpMiddleware/HttpMetricsMiddleware.php index 3e6c9ce..92271f6 100644 --- a/src/HttpMiddleware/HttpMetricsMiddleware.php +++ b/src/HttpMiddleware/HttpMetricsMiddleware.php @@ -10,8 +10,6 @@ class HttpMetricsMiddleware { - protected int|float $duration; - public function __construct( private readonly LatencyProfiler $latencyProfiler ) {