Giter Site home page Giter Site logo

plug_checkup's Introduction

Build Coverage Status || Hex version Hex downloads

PlugCheckup

PlugCheckup provides a Plug for adding simple health checks to your app. The JSON output is similar to the one provided by the MiniCheck Ruby library. It was started to provide solarisBank an easy way of monitoring Plug based applications.

Usage

  • Add the package to "mix.exs"
defp deps do
  [
    {:plug_checkup, "~> 0.3.0"}
  ]
end
defmodule MyHealthChecks do
  def check_db do
    :ok
  end

  def check_redis do
    :ok
  end
end
  • Forward your health path to PlugCheckup in your Plug Router
checks = [
  %PlugCheckup.Check{name: "DB", module: MyHealthChecks, function: :check_db},
  %PlugCheckup.Check{name: "Redis", module: MyHealthChecks, function: :check_redis}
]

forward(
  "/health",
  to: PlugCheckup,
  init_opts: PlugCheckup.Options.new(json_encoder: Jason, checks: checks)
)

If you're working with Phoenix, you need to change the syntax slightly to accomodate Phoenix.Router.forward/4:

checks = [
  %PlugCheckup.Check{name: "DB", module: MyHealthChecks, function: :check_db},
  %PlugCheckup.Check{name: "Redis", module: MyHealthChecks, function: :check_redis}
]

forward("/health", PlugCheckup, PlugCheckup.Options.new(json_encoder: Jason, checks: checks))

The Checks

A check is a function with arity zero, which should return either :ok or {:error, term}. In case the check raises an exception or times out, that will be mapped to an {:error, reason} result.

Response

PlugCheckup should return either 200 or 500 statuses, Content-Type header "application/json", and the body should respect this JSON schema

If you configure the error_code option when initializing the plug, the specified value will be used when an error occurs instead of the 500 status.

Demo

Check .iex.exs for a demo of plug_checkup in a Plug.Router. The demo can be run as following.

git clone https://github.com/ggpasqualino/plug_checkup
cd plug_checkup
mix deps.get
iex -S mix

Open your browse either on http://localhost:4000/selfhealth or http://localhost:4000/dependencyhealth

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.