Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
qcam committed Mar 20, 2021
0 parents commit bdedb7f
Show file tree
Hide file tree
Showing 16 changed files with 793 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Test suite

on:
push:
branches:
- master
pull_request:

jobs:
test:
runs-on: ubuntu-latest
env:
MIX_ENV: test
strategy:
matrix:
include:
- pair:
elixir: '1.7'
otp: '20.3'
- pair:
elixir: '1.11'
otp: '23.2'
check_format: true

name: elixir-${{ matrix.pair.elixir }}-otp-${{ matrix.pair.otp }}

steps:
- uses: actions/checkout@v2

- uses: erlef/setup-elixir@v1
with:
otp-version: ${{matrix.pair.otp}}
elixir-version: ${{matrix.pair.elixir}}

- name: Install Dependencies
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get --only test
- name: Check format
if: matrix.check_format
run: mix format --check-formatted

- name: Run Tests
run: mix test
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
goodo-*.tar

# Temporary files for e.g. tests
/tmp
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2021 Fishbrain

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Goodoo

Goodoo is a simple, robust, and highly customizable health check solution written in Elixir.

## Installation

Add `:goodoo` to your Mix project.

```elixir
def deps() do
[{:goodoo, "~> 0.1"}]
end
```

## Overview

Full documentation can be found on [Hex][hex-doc].

Goodoo works by periodically checking the availablity of the sub-systems based
on your configuration, and provides a few APIs to retrieves the report.

To start using Goodoo, create a module:

```elixir
defmodule MyHealthCheck do
use Goodoo
end
```

After that, add the module with the desired checkers to the supervisor tree.
Please see the "Checkers" section for all currently supported checkers.

```elixir
checkers = %{
"primary" => {Goodoo.Checker.EctoSQL, repo: MyPrimaryRepo},
"replica" => {Goodoo.Checker.EctoSQL, repo: MyReplicaRepo},
"persistent_cache" => {Goodoo.Checker.Redix, connection: MyCache}
}

children = [
MyPrimaryRepo,
MyReplicaRepo,
MyCache,
...,
{MyHealthCheck, checkers},
MyEndpoint
]

Supervisor.start_link(children, strategy: :one_for_one, name: MyApp)
```

Allez, hop! You are "goodoo" to go. To retrieve the health check report,
`list_health_states/1` and `get_health_state/2` can be used.

Usually you might want to expose an HTTP endpoint for some uptime checkers e.g.
AWS ALB, Pingdom, etc. It can be easily done with Plug or Phoenix controller.

### Plug integration

```elixir
defmodule MyRouter do
use Plug.Router

plug :match
plug :dispatch

get "/health" do
healthy? =
Enum.all?(
Goodoo.list_health_states(MyHealthCheck),
fn {_checker_name, {state, _last_checked_at}} ->
state == :healthy
end
)

if healthy? do
send_resp(conn, 200, "Everything is 200 OK")
else
send_resp(conn, 503, "Something is on fire!")
end
end

get "/health/:checker_name" do
case Goodoo.get_health_state(MyHealthCheck, checker_name) do
nil ->
send_resp(conn, 404, "Not found")

{state, _last_checked_at} ->
if state == :healthy do
send_resp(conn, 200, "Service is doing fine")
else
send_resp(conn, 503, "Service is on fire")
end
end
end
end
```

### Phoenix integration

```elixir
defmodule HealthController do
use MyWeb, :controller

def index(conn, _params) do
healthy? =
Enum.all?(
Goodoo.list_health_states(MyHealthCheck),
fn {_checker_name, {state, _last_checked_at}} ->
state == :healthy
end
)

conn = put_status(conn, 200)

if healthy? do
text(conn, "Everything is 200 OK")
else
text(conn, "Something is on fire!")
end
end
end
```

### "Goodoo" to know

Goodoo is the local name of [Murray Cod][murray-cod] in Australia. Here is a picture of it.

![goodoo](./goodoo.jpg)

## Development

Make sure you have [Elixir installed][elixir-installation-guide].

1. Fork the project.
2. Run `mix deps.get` to fetch dependencies.
3. Run `mix test`.

## Contributing

If you have any ideas or suggestions, feel free to submit [an
issue][goodoo-issue] or [a pull request][goodoo-pr].

## License

MIT


[elixir-installation-guide]: https://elixir-lang.org/install.html
[hex-doc]: https://hexdocs.pm/goodoo
[goodoo-issue]: https://github.com/fishbrain/goodoo/issues
[goodoo-pr]: https://github.com/fishbrain/goodoo/pulls
[murray-cod]: https://en.wikipedia.org/wiki/Murray_cod
Binary file added goodoo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
149 changes: 149 additions & 0 deletions lib/goodoo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
defmodule Goodoo do
@moduledoc """
Goodoo is a simple, robust, and highly customizable health check solution written in Elixir.
Goodoo works by periodically checking the availablity of the sub-systems based
on your configuration, and provides a few APIs to retrieves the report.
To start using Goodoo, create a module:
defmodule MyHealthCheck do
use Goodoo
end
After that, add the module with the desired checkers to the supervisor tree.
Please see the "Checkers" section for all currently supported checkers.
checkers = %{
"primary" => {Goodoo.Checker.EctoSQL, repo: MyPrimaryRepo},
"replica" => {Goodoo.Checker.EctoSQL, repo: MyReplicaRepo},
"persistent_cache" => {Goodoo.Checker.Redix, connection: MyCache}
}
children = [
MyPrimaryRepo,
MyReplicaRepo,
MyCache,
...,
{MyHealthCheck, checkers},
MyEndpoint
]
Supervisor.start_link(children, strategy: :one_for_one, name: MyApp)
Allez, hop! You are "goodoo" to go. To retrieve the health check report,
`list_health_states/1` and `get_health_state/2` can be used.
Usually you might want to expose an HTTP endpoint for some uptime checkers e.g.
AWS ALB, Pingdom, etc. It can be easily done with Plug.
defmodule MyRouter do
use Plug.Router
plug :match
plug :dispatch
get "/health" do
healthy? =
Enum.all?(
Goodoo.list_health_states(MyHealthCheck),
fn {_checker_name, {state, _last_checked_at}} ->
state == :healthy
end
)
if healthy? do
send_resp(conn, 200, "Everything is 200 OK")
else
send_resp(conn, 503, "Something is on fire!")
end
end
get "/health/:checker_name" do
case Goodoo.get_health_state(MyHealthCheck, checker_name) do
nil ->
send_resp(conn, 404, "Not found")
{state, _last_checked_at} ->
if state == :healthy do
send_resp(conn, 200, "Service is doing fine")
else
send_resp(conn, 503, "Service is on fire")
end
end
end
end
### Checkers
Goodoo implemented a few common checkers:
* `Goodoo.Checker.EctoSQL` - checkers for works with `Ecto.Repo`.
* `Goodoo.Checker.Redix` - Checker that works with `Redix`.
For more information, please visit the documentation for them accordingly.
Goodoo supports customer checkers, please visit `Goodoo.Checker` for more information.
### Checker scheduling/interval
Goodoo schedules checkers based on the last health state. The default intervals are:
* `:healthy` - next check will be in 30 seconds.
* `:degraded` - next check will be in 10 seconds.
* `:unhealthy` - next check will be in 3 seconds.
You can configure your own strategy with the following example. Please note that missing
intervals will fall back to the defaults.
# `:healthy` and `:degraded` will fall back to defaults.
repo_intervals = %{
unhealthy: 1_000
}
cache_intervals = %{
unhealthy: 1_000,
degraded: 5_000,
healthy: 15_000
}
checkers = %{
"repo" => {Goodoo.Checker.EctoSQL, repo: MyRepo, intervals: repo_intervals},
"cache" => {Goodoo.Checker.EctoSQL, connection: MyCache, intervals: cache_intervals}
}
"""

defmacro __using__(_) do
quote location: :keep do
import Goodoo

def child_spec(checkers) do
Goodoo.Supervisor.child_spec({__MODULE__, checkers})
end
end
end

@doc """
Retrieves all checker statuses of a healthcheck module.
"""
@spec list_health_states(module()) :: %{
Goodoo.Checker.name() => {Goodoo.Checker.health_state(), DateTime.t()}
}
def list_health_states(module) do
module
|> Goodoo.Storage.get_storage_name()
|> Goodoo.Storage.list()
end

@doc """
Retrieve the checker status by its name of a healthcheck module
"""
@spec get_health_state(module(), Goodoo.Checker.name()) ::
{Goodoo.Checker.health_state(), DateTime.t()} | nil
def get_health_state(module, name) do
module
|> Goodoo.Storage.get_storage_name()
|> Goodoo.Storage.get(name)
end
end
Loading

0 comments on commit bdedb7f

Please sign in to comment.