-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RTC-454] Healthcheck endpoint (#143)
* Initial healthcheck endpoint implementation * Add distribution info * Add test
- Loading branch information
Showing
9 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
defmodule JellyfishWeb.ApiSpec.HealthReport do | ||
@moduledoc false | ||
|
||
require OpenApiSpex | ||
alias OpenApiSpex.Schema | ||
|
||
defmodule Status do | ||
@moduledoc false | ||
|
||
require OpenApiSpex | ||
|
||
OpenApiSpex.schema(%{ | ||
title: "HealthReportStatus", | ||
description: "Informs about the status of Jellyfish or a specific service", | ||
type: :string, | ||
enum: ["UP", "DOWN"], | ||
example: "UP" | ||
}) | ||
end | ||
|
||
defmodule Distribution do | ||
@moduledoc false | ||
|
||
require OpenApiSpex | ||
|
||
OpenApiSpex.schema(%{ | ||
title: "HealthReportDistribution", | ||
description: "Informs about the status of Jellyfish distribution", | ||
type: :object, | ||
properties: %{ | ||
enabled: %Schema{ | ||
type: :boolean, | ||
description: "Whether distribution is enabled on this Jellyfish" | ||
}, | ||
nodeStatus: Status, | ||
nodesInCluster: %Schema{ | ||
type: :integer, | ||
description: | ||
"Amount of nodes (including this Jellyfish's node) in the distribution cluster" | ||
} | ||
}, | ||
required: [:nodeStatus, :nodesInCluster] | ||
}) | ||
end | ||
|
||
OpenApiSpex.schema(%{ | ||
title: "HealthReport", | ||
description: "Describes overall Jellyfish health", | ||
type: :object, | ||
properties: %{ | ||
status: Status, | ||
uptime: %Schema{type: :integer, description: "Uptime of Jellyfish (in seconds)"}, | ||
distribution: Distribution | ||
}, | ||
required: [:status, :uptime, :distribution] | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule JellyfishWeb.HealthcheckController do | ||
use JellyfishWeb, :controller | ||
use OpenApiSpex.ControllerSpecs | ||
|
||
alias JellyfishWeb.ApiSpec | ||
|
||
action_fallback JellyfishWeb.FallbackController | ||
|
||
operation :show, | ||
operation_id: "healthcheck", | ||
summary: "Describes the health of Jellyfish", | ||
responses: [ | ||
ok: ApiSpec.data("Healthy", ApiSpec.HealthcheckResponse), | ||
internal_server_error: ApiSpec.data("Unhealthy", ApiSpec.HealthcheckResponse) | ||
] | ||
|
||
def show(conn, _params) do | ||
report = get_health_report() | ||
|
||
conn | ||
|> put_resp_content_type("application/json") | ||
|> render("show.json", report: report) | ||
end | ||
|
||
defp get_health_report() do | ||
%{ | ||
status: :up, | ||
uptime: get_uptime(), | ||
distribution: get_distribution_report() | ||
} | ||
end | ||
|
||
defp get_uptime() do | ||
System.monotonic_time(:second) - Application.fetch_env!(:jellyfish, :start_time) | ||
end | ||
|
||
defp get_distribution_report() do | ||
alive? = Node.alive?() | ||
visible_nodes = Node.list() |> length() | ||
|
||
%{ | ||
enabled: Application.fetch_env!(:jellyfish, :dist_config)[:enabled], | ||
node_status: if(alive?, do: :up, else: :down), | ||
nodes_in_cluster: visible_nodes + if(alive?, do: 1, else: 0) | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule JellyfishWeb.HealthcheckJSON do | ||
@moduledoc false | ||
|
||
def show(%{report: report}) do | ||
%{data: data(report)} | ||
end | ||
|
||
def data(%{status: status, uptime: uptime, distribution: distribution}) do | ||
%{ | ||
status: status_str(status), | ||
uptime: uptime, | ||
distribution: %{ | ||
enabled: distribution.enabled, | ||
nodeStatus: status_str(distribution.node_status), | ||
nodesInCluster: distribution.nodes_in_cluster | ||
} | ||
} | ||
end | ||
|
||
defp status_str(:up), do: "UP" | ||
defp status_str(:down), do: "DOWN" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/jellyfish_web/controllers/healthcheck_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule JellyfishWeb.HealthcheckControllerTest do | ||
use JellyfishWeb.ConnCase, async: true | ||
|
||
import OpenApiSpex.TestAssertions | ||
|
||
@schema JellyfishWeb.ApiSpec.spec() | ||
|
||
setup %{conn: conn} do | ||
server_api_token = Application.fetch_env!(:jellyfish, :server_api_token) | ||
conn = put_req_header(conn, "authorization", "Bearer " <> server_api_token) | ||
|
||
[conn: conn] | ||
end | ||
|
||
test "healthcheck without distribution", %{conn: conn} do | ||
conn = get(conn, ~p"/health") | ||
response = json_response(conn, :ok) | ||
assert_response_schema(response, "HealthcheckResponse", @schema) | ||
|
||
assert %{ | ||
"status" => "UP", | ||
"uptime" => _uptime, | ||
"distribution" => %{ | ||
"enabled" => false, | ||
"nodeStatus" => "DOWN", | ||
"nodesInCluster" => 0 | ||
} | ||
} = response["data"] | ||
end | ||
end |