diff --git a/deploy/config/local/nginx/server.conf b/deploy/config/local/nginx/server.conf index be1c5c58f..8d0076ceb 100644 --- a/deploy/config/local/nginx/server.conf +++ b/deploy/config/local/nginx/server.conf @@ -116,6 +116,11 @@ server { set $skip_cache 1; } + # ...it's to the readiness endpoint + if ($request_uri = "/readiness") { + set $skip_cache 1; + } + # ...it's from a logged in user, the cookie 'wordpress_no_cache' exists. if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1; @@ -303,8 +308,18 @@ server { # LOCATIONS - heath ## - location ~ ^/(liveness|readiness)$ { + location = /liveness { return 200; } + location = /readiness { + # Make sure we can connect to php-fpm via the socket. + set $script_name /metrics/socket.php; + + fastcgi_param SCRIPT_FILENAME $document_root$script_name; + include fastcgi_params; + + fastcgi_pass fpm; + } + } diff --git a/deploy/config/server.conf b/deploy/config/server.conf index 67a6742d8..0a50c7140 100644 --- a/deploy/config/server.conf +++ b/deploy/config/server.conf @@ -135,6 +135,11 @@ server { set $skip_cache 1; } + # ...it's to the liveness or readiness endpoint + if ($request_uri ~* "^(/liveness|/readiness)$" { + set $skip_cache 1; + } + # ...it's from a logged in user, the cookie 'wordpress_no_cache' exists. if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1; @@ -325,7 +330,7 @@ server { # LOCATIONS - health ## - location ~ ^/(liveness|readiness)$ { + location = /liveness { if ($ip_access_health = 0) { return 404; } @@ -333,4 +338,18 @@ server { return 200; } + location = /readiness { + if ($ip_access_health = 0) { + return 404; + } + + # Make sure we can connect to php-fpm via the socket. + set $script_name /metrics/socket.php; + + fastcgi_param SCRIPT_FILENAME $document_root$script_name; + include fastcgi_params; + + fastcgi_pass fpm; + } + } diff --git a/public/metrics/service.php b/public/metrics/service.php index 8b81d26ef..9cd98f512 100644 --- a/public/metrics/service.php +++ b/public/metrics/service.php @@ -34,7 +34,6 @@ class Metrics */ public function __construct() { - // Get the ip group of the incoming request. $ip_group = $_SERVER['HTTP_X_MOJ_IP_GROUP'] ?? 0; @@ -49,29 +48,8 @@ public function __construct() $this->home_url = env('WP_HOME'); - // Define an array of metrics that we want to generate. + // Define an array of metrics that we want to generate for all environments. $this->metrics_properties = [ - 'http_status_code_control' => [ - 'help' => 'The http status code when accessing an open site.', - 'type' => 'gauge', - 'callback' => [$this, 'getStatusCode'], - 'args' => [$this::OPEN_URL] - ], - 'http_status_code_invalid_header' => [ - 'help' => 'The http status code of when sending X-Moj-Ip-Group header.', - 'type' => 'gauge', - 'callback' => [$this, 'getStatusCode'], - 'args' => [ - $this->home_url, - ['headers' => ['X-Moj-Ip-Group' => 0]] - ] - ], - 'http_status_code_health' => [ - 'help' => 'The http status code of /health.', - 'type' => 'gauge', - 'callback' => [$this, 'getStatusCode'], - 'args' => ["{$this->home_url}/health"] - ], 'http_status_code_wp_home' => [ 'help' => 'The http status code when accessing this service via it\'s full url as defined in WP_HOME.', 'type' => 'gauge', @@ -79,6 +57,33 @@ public function __construct() 'args' => [$this->home_url] ] ]; + + if ('production' !== env('WP_ENV')) { + // Add other metrics for non-production. + $this->metrics_properties = array_merge($this->metrics_properties, [ + 'http_status_code_control' => [ + 'help' => 'The http status code when accessing an open site.', + 'type' => 'gauge', + 'callback' => [$this, 'getStatusCode'], + 'args' => [$this::OPEN_URL] + ], + 'http_status_code_invalid_header' => [ + 'help' => 'The http status code of when sending X-Moj-Ip-Group header.', + 'type' => 'gauge', + 'callback' => [$this, 'getStatusCode'], + 'args' => [ + $this->home_url, + ['headers' => ['X-Moj-Ip-Group' => 0]] + ] + ], + 'http_status_code_health' => [ + 'help' => 'The http status code of /health.', + 'type' => 'gauge', + 'callback' => [$this, 'getStatusCode'], + 'args' => ["{$this->home_url}/health"] + ] + ]); + } } /** @@ -140,6 +145,7 @@ public function serveMetrics(): void { header('Content-Type', 'text/plain'); echo $this->getServiceMetrics(); + unset($this->guzzle_client); exit(); } } diff --git a/public/metrics/socket.php b/public/metrics/socket.php new file mode 100644 index 000000000..1abc618f2 --- /dev/null +++ b/public/metrics/socket.php @@ -0,0 +1,3 @@ +