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

Separate liveness and readiness checks #5048

Merged
merged 5 commits into from
Feb 10, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file.
- Details modal search inputs are now case-insensitive.
- Improved report performance in cases where site has a lot of unique pathnames
- Plausible script now uses `fetch` with keepalive flag as default over `XMLHttpRequest`. This will ensure more reliable tracking. Reminder to use `compat` script variant if tracking Internet Explorer is required.
- The old `/api/health` healtcheck is soft-deprecated in favour of separate `/api/system/live` and `/api/system/ready` checks
apata marked this conversation as resolved.
Show resolved Hide resolved

### Fixed

Expand Down
48 changes: 0 additions & 48 deletions lib/plausible_web/controllers/api/external_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,54 +47,6 @@ defmodule PlausibleWeb.Api.ExternalController do
send_resp(conn, 200, "")
end

def health(conn, _params) do
postgres_health =
case Ecto.Adapters.SQL.query(Plausible.Repo, "SELECT 1", []) do
{:ok, _} -> "ok"
e -> "error: #{inspect(e)}"
end

clickhouse_health =
case Ecto.Adapters.SQL.query(Plausible.ClickhouseRepo, "SELECT 1", []) do
{:ok, _} -> "ok"
e -> "error: #{inspect(e)}"
end

cache_health =
if postgres_health == "ok" and Plausible.Site.Cache.ready?() and
Plausible.Shield.IPRuleCache.ready?() do
"ok"
end

status =
case {postgres_health, clickhouse_health, cache_health} do
{"ok", "ok", "ok"} -> 200
_ -> 500
end

put_status(conn, status)
|> json(%{
postgres: postgres_health,
clickhouse: clickhouse_health,
sites_cache: cache_health
})
end

def info(conn, _params) do
build =
:plausible
|> Application.get_env(:runtime_metadata)
|> Keyword.take([:version, :commit, :created, :tags])
|> Map.new()

geo_database = Plausible.Geo.database_type() || "(not configured)"

json(conn, %{
geo_database: geo_database,
build: build
})
end

defp find_first_invalid_changeset(dropped) do
Enum.find_value(dropped, nil, fn dropped_event ->
case dropped_event.drop_reason do
Expand Down
75 changes: 75 additions & 0 deletions lib/plausible_web/controllers/api/system_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule PlausibleWeb.Api.SystemController do
ukutaht marked this conversation as resolved.
Show resolved Hide resolved
use PlausibleWeb, :controller
require Logger

def info(conn, _params) do
build =
:plausible
|> Application.get_env(:runtime_metadata)
|> Keyword.take([:version, :commit, :created, :tags])
|> Map.new()

geo_database = Plausible.Geo.database_type() || "(not configured)"

json(conn, %{
geo_database: geo_database,
build: build
})
end

def liveness(conn, _params) do
json(conn, %{ok: true})
end

@task_timeout 15_000
def readiness(conn, _params) do
ukutaht marked this conversation as resolved.
Show resolved Hide resolved
postgres_health_task =
Task.async(fn ->
Ecto.Adapters.SQL.query(Plausible.Repo, "SELECT 1", [])
end)

clickhouse_health_task =
Task.async(fn ->
Ecto.Adapters.SQL.query(Plausible.ClickhouseRepo, "SELECT 1", [])
end)

postgres_health =
case Task.await(postgres_health_task, @task_timeout) do
{:ok, _} ->
"ok"

e ->
Logger.error("Postgres health check failure: #{inspect(e)}")
"error"
end

clickhouse_health =
case Task.await(clickhouse_health_task, @task_timeout) do
{:ok, _} ->
"ok"

e ->
Logger.error("Clickhouse health check failure: #{inspect(e)}")
"error"
end

cache_health =
if postgres_health == "ok" and Plausible.Site.Cache.ready?() and
Plausible.Shield.IPRuleCache.ready?() do
"ok"
end

status =
case {postgres_health, clickhouse_health, cache_health} do
{"ok", "ok", "ok"} -> 200
_ -> 500
end

put_status(conn, status)
|> json(%{
postgres: postgres_health,
clickhouse: clickhouse_health,
sites_cache: cache_health
})
end
end
10 changes: 8 additions & 2 deletions lib/plausible_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,14 @@ defmodule PlausibleWeb.Router do

post "/event", Api.ExternalController, :event
get "/error", Api.ExternalController, :error
get "/health", Api.ExternalController, :health
get "/system", Api.ExternalController, :info
# Remove this once all external checks are migration to new /system/health/* checks
ukutaht marked this conversation as resolved.
Show resolved Hide resolved
get "/health", Api.SystemController, :readiness
end

scope "/system" do
ukutaht marked this conversation as resolved.
Show resolved Hide resolved
get "/", Api.SystemController, :info
get "/health/live", Api.SystemController, :liveness
get "/health/ready", Api.SystemController, :readiness
end

scope [] do
Expand Down
Loading