GithubHelp home page GithubHelp logo

take-five / pinglix Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pvdvreede/pinglix

0.0 1.0 0.0 26 KB

Plug compatible health check middleware based on pinglish.

License: MIT License

Elixir 100.00%

pinglix's Introduction

Pinglix

Build Status Hex pm

Plug compatible health check system in Elixir based on https://github.com/jbarnette/pinglish.

Setup

Pinglix is an Elixir Hex package, so you can just add the following to your mix.exs file under deps.

{:pinglix, "~> 1.1"}

Usage

At its simplest form, create a module that usees Pinglix which will generate a Plug compatiable middleware, put this module in your app to get the /_ping endpoint which follows numbers 1, 4, 6, 8, 9 of the Pinglish specification.

defmodule MyApp.Ping do
  use Pinglix
end

defmodule MyApp do
  plug MyApp.Ping
end

In this form, the check always returns ok as all it is doing is confirming that your Plug compatible webserver is running. This might provide some benefit, but it is far more useful to be able to check backend/downstream services that your service relies on. For this use case you can use the following macro based setup to add custom checks for Pinglix to run whenever the /_ping endpoint is hit:

defmodule MyApp.Ping do
  use Pinglix

  # return :ok if all is well, or :fail with a message
  # as the string
  defcheck :webservice do
    {:ok, %HTTPoison{status_code: code}} = HTTPoison.get("https://downstream.webservice/_ping")
    case code do
      200 -> :ok
      _   -> {:fail, "Received status #{code} from my webservice."}
    end
  end

  # any exceptions will set the check to fail and set the fail message to be the
  # exception message
  defcheck :db do
    DB.ping!
    :ok
  end

  # you can add some info to the json response even if the check passes
  # just return a tuple with :ok and a string instead of just the :ok atom
  defcheck :s3_info do
    count = S3.check_objects("bucket1")
    if count > 100 do
      {:fail, "S3 objects are at #{count}."}
    else
      {:ok, "S3 objects are at #{count}."}
    end
  end
end

defmodule MyApp do
  # you can provide a timeout option in milli seconds if you dont want
  # the default 29 seconds
  plug MyApp.Ping, timeout: 59_000 # this will timeout after 59 seconds

  # other plugs and endpoints.
end

There are some example ping Plugs at test/test_helper.exs.

Using Pinglix will create a new plug with your checks builtin for you to include where you need. These checks will be run simultaneously. The request will return after 29 seconds as per the Pinglish specification, so all checks have 29 seconds each, rather than 29 seconds cumulatively.

License

The MIT License (MIT)

Copyright (c) 2015 Paul Van de Vreede

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.

pinglix's People

Contributors

pvdvreede avatar samhamilton avatar take-five avatar technicalcapt avatar

Watchers

 avatar

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.