PlugInstrumenter v0.1.2 PlugInstrumenter View Source

Reports plug timing to a configurable callback function.

Wraps plugs, adding instrumentation. Use it in your plug pipeline like this:

plug PlugInstrumenter, plug: MyPlug

Pass options to the plug like this:

plug PlugInstrumenter, plug: MyPlug, opts: [my_opt: :cool]

Metrics are passed to a configured callback, and a configurable name where the default is based on the module’s name. There are three phases that can be instrumented:

  • :pre - when the call/2 function is executed.
  • :post - when the before_send callbacks are executed.
  • :init - when the init/1 function is executed.

Options

Options can be set in your configuration under the :plug_instrumenter namespace. They will be overridden by options passed to the plug macro.

  • :plug - The plug to instrument
  • :now - a module/function tuple pointing to an mfa that returns the current time. Default is :erlang.monotonic_time(:microsecond).
  • :callback - The instrumentation callback, which should have a 3-arity function. The default callback calls Logger.debug. The arguments passed to the function are as follows:

    • phase - one of:

      • :init - executed after the init/1 has completed
      • :pre - executed after the call/2 method has completed
      • :post - executed after before_send callbacks have completed
    • {start, finish} - the start and finish time, as reported by :now
    • opts the PlugInstrumenter options represented as a map.
  • :name - a string or 2-arity function that returns the metric name as a string. If a function is used, it will be called during the plug’s init phase with the following arguments:

    • module - The name of the plug module
    • opts - The options passed to the plug instrumenter. The instrumented plug’s options are included via the key :plug_opts.

Link to this section Summary

Link to this section Types

Link to this type callback_t() View Source
callback_t() :: {module(), atom()}
Link to this type opts_t() View Source
opts_t() :: %{
  :plug => module(),
  :name => String.t(),
  optional(:callback) => callback_t(),
  :now => {module(), atom(), [any()]},
  :plug_opts => any(),
  optional(atom()) => any()
}
Link to this type phase_t() View Source
phase_t() :: :init | :pre | :post
Link to this type plug_opts_t() View Source
plug_opts_t() :: {opts_t(), any()}