Giter VIP home page Giter VIP logo

boom's Introduction

BoomNotifier

hex Elixir CI


This package allows your Phoenix application to send notifications whenever an exception is raised. By default it includes an email and a webhook notifier, you can also implement custom ones, or use some of the independently realeased notifiers listed below.

It was inspired by the ExceptionNotification gem that provides a similar functionality for Rack/Rails applications.

You can read the full documentation at https://hexdocs.pm/boom_notifier.

Installation

The package can be installed by adding boom_notifier to your list of dependencies in mix.exs:

def deps do
  [
    {:boom_notifier, "~> 0.8.0"}
  ]
end

Getting started

This is an example for setting up an email notifier, you can see the full list of available notifiers here.

Email notifier

BoomNotifier has built in support for both Bamboo and Swoosh.

defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
      notifier: BoomNotifier.MailNotifier.Bamboo,
      # or to use Swoosh
      # notifier: BoomNotifier.MailNotifier.Swoosh,
      options: [
        mailer: YourApp.Mailer,
        from: "[email protected]",
        to: "[email protected]",
        subject: "BOOM error caught"
      ]

  # ...

For the email to be sent, you need to define a valid mailer in the options keyword list. You can customize the from, to and subject attributes.

subject will be truncated at 80 chars, if you want more add the option max_subject_length.

Notifiers

BoomNotifier uses notifiers to deliver notifications when errors occur in your applications. By default, 2 notifiers are available:

You can also choose from these independetly-released notifiers:

On top of this, you can easily implement your own custom notifier.

Multiple notifiers

BoomNotifier allows you to setup multiple notifiers, like in the example below:

defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    notifiers: [
      [
        notifier: BoomNotifier.WebhookNotifier,
        options: [
          url: "http://example.com",
        ]
      ],
      [
        notifier: CustomNotifier,
        options: # ...
      ]
    ]

Setup when handle_errors/2 is already being used

If you are using or want to use your own implementation of handle_errors/2 for the Plug.ErrorHandler module, be sure to include the usage of BoomNotifier after that.

In addition, you will have to add the notify_error/2 callback that BoomNotifier provides within your implementation of handle_errors/2.

defmodule YourApp.Router do
  use Phoenix.Router

  use Plug.ErrorHandler

  def handle_errors(conn, error) do
    # ...
    notify_error(conn, error)
    # ...
  end

  use BoomNotifier,
    ...

Notification Trigger

By default, BoomNotifier will send a notification every time an exception is raised.

However, there are different strategies to decide when to send the notifications using the :notification_trigger option with one of the following values: :always and :exponential.

Always

This option is the default one. It will trigger a notification for every exception.

defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    notification_trigger: :always,
    notifiers: [
      # ...
    ]

Exponential

It uses a formula of log2(errors_count) to determine whether to send a notification, based on the accumulated error count for each specific exception. This makes the notifier only send a notification when the count is: 1, 2, 4, 8, 16, 32, 64, 128, ..., (2**n).

You can also set an optional max value.

defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    notification_trigger: :exponential,
    notifiers: [
      # ...
    ]
defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    notification_trigger: [exponential: [limit: 100]]
    notifiers: [
      # ...
    ]

Custom data or Metadata

By default, BoomNotifier will not include any custom data from your requests.

However, there are different strategies to decide which information do you want to include in the notifications using the :custom_data option with one of the following values: :assigns, :logger or both.

The included information will show up in your notification, in a new section titled "Metadata".

Assigns

This option will include the data that is in the connection assigns field.

You can also specify the fields you want to retrieve from conn.assigns.

defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    custom_data: :assigns,
    notifiers: [
      # ...
    ]
defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    custom_data: [assigns: [fields: [:current_user, :session_data]]],
    notifiers: [
      # ...
    ]

Example of adding custom data to the connection:

assign(conn, :name, "John")

Logger

This option will include the data that is in the Logger metadata field.

You can also specify the fields you want to retrieve from Logger.metadata().

defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    custom_data: :logger,
    notifiers: [
      # ...
    ]
defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    custom_data: [logger: [fields: [:request_id, :current_user]]],
    notifiers: [
      # ...
    ]

Example of adding custom data to the logger:

Logger.metadata(name: "John")

Using both

You can do any combination of the above settings to include data from both sources. The names of the fields are independent for each source, they will appear under the source namespace.

defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    custom_data: [
      [assigns: [fields: [:current_user]]],
      [logger: [fields: [:request_id, :current_user]]]
    ],
    notifiers: [
      # ...
    ]
   # ...
end

Ignore exceptions

By default, all exceptions are captured by Boom. The :ignore_exceptions setting is provided to ignore exceptions of a certain kind. Said exceptions will not generate any kind of notification from Boom.

defmodule YourApp.Router do
  use Phoenix.Router

  use BoomNotifier,
    ignore_exceptions: [
      HTTPoison.Error, MyApp.CustomException
    ],
    notifiers: [
      # ...
    ]
   # ...
end

License

BoomNotifier is released under the terms of the MIT License.

Credits

The authors of this project are Ignacio and Jorge. It is sponsored and maintained by Wyeworks.

boom's People

Contributors

iaguirre88 avatar jmbejar avatar grzuy avatar matsu911 avatar leticiaerrandonea avatar mcass19 avatar noelia-lencina avatar smartinez87 avatar romiparada avatar rafaeliga avatar rktjmp avatar 2guti2 avatar krainboltgreene avatar joulei avatar gastonabella avatar eldano avatar brunvez 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.