Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make nginx readiness endpoint dependant on a working socket to fpm #763

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion deploy/config/local/nginx/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -303,8 +308,18 @@ server {
# LOCATIONS - heath
##

location ~ ^/(liveness|readiness)$ {
location = /liveness {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this also benefit from skipping the cache?

    if ($request_uri ~* "/liveness|/readiness") {
        set $skip_cache 1;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout, I'll make the change.

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;
}

}
21 changes: 20 additions & 1 deletion deploy/config/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -325,12 +330,26 @@ server {
# LOCATIONS - health
##

location ~ ^/(liveness|readiness)$ {
location = /liveness {
if ($ip_access_health = 0) {
return 404;
}

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;
}

}
52 changes: 29 additions & 23 deletions public/metrics/service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -49,36 +48,42 @@ 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',
'callback' => [$this, 'getStatusCode'],
'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"]
]
]);
}
}

/**
Expand Down Expand Up @@ -140,6 +145,7 @@ public function serveMetrics(): void
{
header('Content-Type', 'text/plain');
echo $this->getServiceMetrics();
unset($this->guzzle_client);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tested this, but it had no visual effect.
Do we still think it's needed, due to good-housekeeping, or should we consider removing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it can do any harm.

exit();
}
}
Expand Down
3 changes: 3 additions & 0 deletions public/metrics/socket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'OK';
Loading