ex_health v0.1.0 ExHealth View Source

CircleCI

ExHealth is a simple extensible health check utility that monitors your applications.

By itself, ExHealth is a supervised GenServer that periodically performs a set of checks, but you can easily configure your it to serve JSON responses that look like:

{
   last_check:"2018-09-18T06:43:53.773719Z",
   result:{
      check_results:[
         [
            "Database",
            true
         ],
         [
            "PhoenixExampleWeb_Endpoint",
            true
         ]
      ],
      msg:"healthy"
   }
}

Getting Started

Configuration for ExHealth must be present the Application environment. This can be done by updating the :ex_health values in your config/config.exs:

config :ex_health,
  module: MyApplication.HealthChecks,
  interval_ms: 1000

Then you must define a module MyApplication.HealthChecks with some checks:

defmodule MyApplication.HealthChecks do
  process_check(MyApplication.CacheServer)

  test "Redis" do
    MyRedis.ping() # This should return :ok | {:error, "Message"}
  end
end

Integrating with Phoenix

To integrate with Phoenix or any other web framework, you can take advantage of ExHealth.Plug which handles serving a JSON response for you.

See ExHealth.Plug for instructions.

Link to this section Summary

Functions

Defines a healthcheck function

Defines a healthcheck function for a given process

Starts the application with empty state

Called when an application is started

Synchronously fetches the latest status from ExHealth.HealthServer

Stops the Application

Link to this section Functions

Link to this macro health_check(name, list) View Source (macro)

Defines a healthcheck function.

Takes the following arguments:

  1. name - a string for the name of the health check
  2. block - block that returns :ok | true | {:error, "Reason"}

Examples:

defmodule MyApp.HealthChecks do
  health_check("Database") do
    MyDB.ping() # This should return  :ok | true | {:error, "Reason"}
  end
end
Link to this macro process_check(module) View Source (macro)

Defines a healthcheck function for a given process.

Returns true if the process has one of the following statuses:

  • :running
  • :waiting

See Process.info/1 for more information about process status.

Examples:

defmodule MyApp.HealthChecks do
  process_check(MyApp.SomeImportantService)
end

Starts the application with empty state

Called when an application is started.

This function is called when an application is started using Application.start/2 (and functions on top of that, such as Application.ensure_started/2). This function should start the top-level process of the application (which should be the top supervisor of the application’s supervision tree if the application follows the OTP design principles around supervision).

start_type defines how the application is started:

  • :normal - used if the startup is a normal startup or if the application is distributed and is started on the current node because of a failover from another node and the application specification key :start_phases is :undefined.
  • {:takeover, node} - used if the application is distributed and is started on the current node because of a failover on the node node.
  • {:failover, node} - used if the application is distributed and is started on the current node because of a failover on node node, and the application specification key :start_phases is not :undefined.

start_args are the arguments passed to the application in the :mod specification key (e.g., mod: {MyApp, [:my_args]}).

This function should either return {:ok, pid} or {:ok, pid, state} if startup is successful. pid should be the PID of the top supervisor. state can be an arbitrary term, and if omitted will default to []; if the application is later stopped, state is passed to the stop/1 callback (see the documentation for the c:stop/1 callback for more information).

use Application provides no default implementation for the start/2 callback.

Callback implementation for Application.start/2.

Synchronously fetches the latest status from ExHealth.HealthServer

Examples:

iex(1)> ExHealth.status()
%ExHealth.Status{
  checks: [
    %ExHealth.Check{
      mfa: {ExHealth.SelfCheck, :hc__ExHealth_HealthServer, []},
      name: "ExHealth_HealthServer"
    }
  ],
  interval_ms: 15000,
  last_check: nil,
  result: %{check_results: [], msg: :pending}
}

Stops the Application