Skip to content

Commit b655730

Browse files
taras-tyshkonshoes
andauthored
Add Healthcheck Endpoint for Service Monitoring (#2248)
#### What problem does this PR solve? This PR introduces a new `GET /healthcheck` endpoint to provide a standard mechanism for monitoring the application's health and availability. This is a common requirement for running services in production, allowing load balancers, container orchestrators (like Kubernetes), and other monitoring tools to verify that the application is running and responsive. #### What is the solution? 1. **New Endpoint:** A `GET /healthcheck` route has been added. 2. **Response:** The endpoint returns a JSON response containing: * `status`: Always `"ok"` to indicate the service is running. * `build`: The git revision hash of the current build. This provides essential diagnostic information for debugging and tracking deployed versions without exposing sensitive system details. 3. **API Documentation:** The new endpoint is fully documented using `OpenApiSpex`, and the specification is available via the Swagger UI. 4. **Testing:** A new test suite has been added to verify the endpoint's functionality. This approach provides a simple, fast, and secure liveness probe while still offering valuable, non-sensitive build information. Co-authored-by: Nate Shoemaker <[email protected]>
1 parent 0215f78 commit b655730

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

.cspell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
"undigits",
147147
"unprovisioned",
148148
"validtoken",
149-
"xdelta"
149+
"xdelta",
150+
"healthcheck"
150151
]
151152
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule NervesHubWeb.HealthcheckController do
2+
use NervesHubWeb, :controller
3+
use OpenApiSpex.ControllerSpecs
4+
5+
alias OpenApiSpex.Schema
6+
7+
defmodule HealthcheckResponse do
8+
require OpenApiSpex
9+
10+
OpenApiSpex.schema(%{
11+
title: "Healthcheck Response",
12+
description: "The response from the healthcheck endpoint.",
13+
type: :object,
14+
properties: %{
15+
status: %Schema{
16+
type: :string,
17+
description: "The operational status of the service.",
18+
example: "ok"
19+
},
20+
build: %Schema{
21+
type: :string,
22+
description: "The git revision of the build.",
23+
example: "e35ffc5"
24+
}
25+
},
26+
required: [:status, :build]
27+
})
28+
end
29+
30+
operation(:index,
31+
description: "Check the health of the service.",
32+
summary: "Healthcheck",
33+
tags: ["Monitoring"],
34+
responses: %{200 => {"OK", "application/json", HealthcheckResponse}}
35+
)
36+
37+
def index(conn, _params) do
38+
json(conn, %{status: "ok", build: System.build_info()[:revision]})
39+
end
40+
end

lib/nerves_hub_web/router.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ defmodule NervesHubWeb.Router do
7979
plug(NervesHubWeb.API.Plugs.Device)
8080
end
8181

82+
scope "/" do
83+
pipe_through(:api)
84+
get("/healthcheck", NervesHubWeb.HealthcheckController, :index)
85+
end
86+
8287
scope "/api" do
8388
pipe_through(:api)
8489
get("/openapi", OpenApiSpex.Plug.RenderSpec, [])
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule NervesHubWeb.HealthcheckControllerTest do
2+
use NervesHubWeb.ConnCase, async: true
3+
4+
describe "GET /healthcheck" do
5+
test "returns status and build information", %{conn: conn} do
6+
conn = get(conn, ~p"/healthcheck")
7+
response = json_response(conn, 200)
8+
assert response == %{"status" => "ok", "build" => System.build_info()[:revision]}
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)