From 39f7967870c1c601aee05efa3c57d49ae7804868 Mon Sep 17 00:00:00 2001 From: Nick Verschoor Date: Thu, 20 Feb 2025 15:25:31 +0100 Subject: [PATCH] feat(PROW-48): Added metrics --- composer.json | 14 +++++- config/kubernetes.php | 40 +++++++++++++++ src/Enums/ContanerMode.php | 11 ++++ src/KubernetesServiceProvider.php | 50 +++++++++++++++---- .../CurrentMasterSupervisorCollector.php | 8 +++ .../CurrentProcessesPerQueueCollector.php | 8 +++ .../Horizon/CurrentWorkloadCollector.php | 8 +++ .../Horizon/FailedJobsPerHourCollector.php | 8 +++ .../Horizon/HorizonStatusCollector.php | 8 +++ .../Horizon/JobsPerMinuteCollector.php | 8 +++ .../Horizon/RecentJobsCollector.php | 8 +++ 11 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 src/Enums/ContanerMode.php create mode 100644 src/Metrics/Collectors/Horizon/CurrentMasterSupervisorCollector.php create mode 100644 src/Metrics/Collectors/Horizon/CurrentProcessesPerQueueCollector.php create mode 100644 src/Metrics/Collectors/Horizon/CurrentWorkloadCollector.php create mode 100644 src/Metrics/Collectors/Horizon/FailedJobsPerHourCollector.php create mode 100644 src/Metrics/Collectors/Horizon/HorizonStatusCollector.php create mode 100644 src/Metrics/Collectors/Horizon/JobsPerMinuteCollector.php create mode 100644 src/Metrics/Collectors/Horizon/RecentJobsCollector.php diff --git a/composer.json b/composer.json index 82f2b85..ceca790 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ ], "require": { "php": "^8.2", - "laravel/octane": "^2.6" + "laravel/octane": "^2.6", + "spatie/laravel-prometheus": "^1.2" }, "extra": { "laravel": { @@ -24,5 +25,14 @@ } }, "minimum-stability": "stable", - "prefer-stable": true + "prefer-stable": true, + "config": { + "allow-plugins": { + "php-http/discovery": true, + "tbachert/spi": true + } + }, + "require-dev": { + "laravel/horizon": "^5.30" + } } diff --git a/config/kubernetes.php b/config/kubernetes.php index bad3ad5..603c055 100644 --- a/config/kubernetes.php +++ b/config/kubernetes.php @@ -37,4 +37,44 @@ | */ 'max_execution_time' => env('MAX_EXECUTION_TIME', 60), + + /* + |-------------------------------------------------------------------------- + | Container mode + |-------------------------------------------------------------------------- + | + | This option defines the container mode that gets used when running + | the Docker container. The name specified in this option should match one + | of the following modes: "http", "horizon", "worker", "scheduler". + | + */ + 'container_mode' => env('CONTAINER_MODE', 'http'), + + /* + |-------------------------------------------------------------------------- + | Project Name + |-------------------------------------------------------------------------- + | + | This option defines the name of the project that gets used when running + | the Docker container. The name specified in this option should match the + | name of the project that you are running. + | + */ + 'project_name' => env('PROJECT_NAME', null), + + /* + |-------------------------------------------------------------------------- + | Metrics + |-------------------------------------------------------------------------- + | + | This option defines the configuration for the metrics endpoint that + | gets used when running the Docker container. The configuration specified + | in this option should match the configuration of the metrics endpoint. + | + */ + 'metrics' => [ + 'enabled' => env('KUBERNETES_METRICS_ENABLED', true), + 'path' => env('KUBERNETES_METRICS_PATH', 'prometheus'), + 'middleware' => [], + ], ]; \ No newline at end of file diff --git a/src/Enums/ContanerMode.php b/src/Enums/ContanerMode.php new file mode 100644 index 0000000..ffd6fd1 --- /dev/null +++ b/src/Enums/ContanerMode.php @@ -0,0 +1,11 @@ +mergeConfigFrom(__DIR__.'/../config/kubernetes.php', 'kubernetes'); + + $this->registerMetricsCollectors(); } - public function boot() + public function boot(): void { $this->registerPublishing(); - $this->setOctaneConfig(); $this->registerCommands(); + + $this->setOctaneConfig(); + $this->setPrometheusConfig(); } protected function registerCommands(): void @@ -29,6 +41,15 @@ protected function registerCommands(): void } } + protected function registerPublishing(): void + { + if ($this->app->runningInConsole()) { + $this->publishes([ + __DIR__.'/../config/kubernetes.php' => config_path('kubernetes.php'), + ], 'kubernetes-config'); + } + } + protected function setOctaneConfig(): void { config()->set('octane.server', 'frankenphp'); @@ -36,12 +57,23 @@ protected function setOctaneConfig(): void config()->set('octane.max_execution_time', config('kubernetes.max_execution_time')); } - protected function registerPublishing() + protected function setPrometheusConfig(): void { - if ($this->app->runningInConsole()) { - $this->publishes([ - __DIR__.'/../config/kubernetes.php' => config_path('kubernetes.php'), - ], 'kubernetes-config'); - } + config()->set('prometheus.enabled', config('kubernetes.metrics.enabled')); + config()->set('prometheus.urls.default', config('kubernetes.metrics.path')); + config()->set('prometheus.middleware', config('kubernetes.metrics.middleware')); + } + + protected function registerMetricsCollectors(): void + { + Prometheus::registerCollectorClasses([ + CurrentMasterSupervisorCollector::class, + CurrentProcessesPerQueueCollector::class, + CurrentWorkloadCollector::class, + FailedJobsPerHourCollector::class, + HorizonStatusCollector::class, + JobsPerMinuteCollector::class, + RecentJobsCollector::class, + ]); } } \ No newline at end of file diff --git a/src/Metrics/Collectors/Horizon/CurrentMasterSupervisorCollector.php b/src/Metrics/Collectors/Horizon/CurrentMasterSupervisorCollector.php new file mode 100644 index 0000000..6f4ac62 --- /dev/null +++ b/src/Metrics/Collectors/Horizon/CurrentMasterSupervisorCollector.php @@ -0,0 +1,8 @@ +