fettle_checks v0.2.2 Fettle.HttpChecker View Source

A Fettle.Checker that is healthy depending on the result of an HTTP request.

Implements the Fettle.Checker behaviour.

Configure in Fettle health-check config as e.g.:

{
  %{
    name: "my-service-check",
    panic_guide_url: "...",
    ...
    checker: Fettle.HttpChecker,
    args: [url: "http://my-service.com/endpoint", method: "POST", req_body: body(), status_code: 200..202, resp_body: ~r/.*xy??y.*/]
  },
}

Options

KeyTypeDescriptionRequired/default
urlString.tURL to callrequired
headers{String.t, String.t}Headers to pass to request[]
methodString.tHTTP method"GET"
req_bodyString.tBody to send with POST, PUT""
httpoisonKeyword.tAdditional options for HTTPoison.request/5[]
status_codenon_neg_integer | Range.t | [non_neg_integer | Range.t]Status code to match200
resp_bodyanyExpected response bodyfalse (don’t care)

Specifying the expected response body

The supported values for resp_body are:

Simple customization can be performed by using the function or module resp_body options.

Customizing via Fettle.HttpCheckerBase

Note that the check/1, compare_response/2 and compare_resp_body/4 functions are all overridable so that this module can be used as a base for more custom implementations; indeed Fettle.HttpChecker itself is just a default implementation of Fettle.HttpCheckerBase.

e.g.

defmodule MyResponseChecker do
  use Fettle.HttpCheckerBase

  def compare_response(resp = %HTTPoison.Response{}, config) do
    # your implementation
  end
end

compare_response/2 is called from check/1, and checks the status code matches, before calling compare_resp_body/4 if the resp_body option is given, so you can override at either the request or body level.

Note that if you are overriding only compare_resp_body/4, you must provide a value for the resp_body option, else it will be skipped by the default implementation of compare_response/2. You can do this robustly by also overriding init/1 to pass a truthy value for the resp_body opt, and calling super:

defmodule JsonBodyChecker do
  use Fettle.HttpCheckerBase
  def init(opts), do: super([{:resp_body, true} | opts])
  def compare_resp_body("application/json", body, true, config) do
    # your implementation
  end
end

The result of the init/1 function is a map with all keys from the opts argument; this is passed through to the lower-level functions as config, so you can add your own parameters for these functions.

Link to this section Summary

Functions

Call an HTTP(S) end-point and assert a response code/response body and return a Fettle.Checker.Response

Compare the response body with to an expected content-type, body or pattern and compute a Fettle.Checker.Response

compare the HTTP response and compute a Fettle.Checker.Result

merges default HTTPoison hackney options with httpoison[:hackney] options specified in config

adds a user-agent header using fettle.system_code config, if not already present

merges default HTTPoison options with httpoison options specified in config

check options and transform into a map, applying defaults as necessary

choses the method used to compare the response body and executes it

Link to this section Functions

Call an HTTP(S) end-point and assert a response code/response body and return a Fettle.Checker.Response

Link to this function compare_resp_body(content_type, body, expected_body, config) View Source
compare_resp_body(
  content_type :: String.t(),
  body :: String.t(),
  expected_body :: any(),
  config :: map()
) :: Fettle.Checker.Result.t()

Compare the response body with to an expected content-type, body or pattern and compute a Fettle.Checker.Response.

Supported types for expected_body are: String or Regex, extensions of this module may implement others.

Link to this function compare_response(response, config) View Source
compare_response(resp :: HTTPoison.Response.t(), config :: map()) ::
  Fettle.Checker.Result.t()

compare the HTTP response and compute a Fettle.Checker.Result

Link to this function default_hackney_opts(hackney_opts) View Source

merges default HTTPoison hackney options with httpoison[:hackney] options specified in config.

Link to this function default_headers(headers) View Source

adds a user-agent header using fettle.system_code config, if not already present

Link to this function default_httpoison_opts(httpoison_opts) View Source

merges default HTTPoison options with httpoison options specified in config.

check options and transform into a map, applying defaults as necessary

Link to this function run_compare_response_body(resp_body_opt, resp, config) View Source
run_compare_response_body(
  resp_body_opt :: any(),
  resp :: HTTPoison.Response.t(),
  config :: map()
) :: Fettle.Checker.Result.t()

choses the method used to compare the response body and executes it