Giter Site home page Giter Site logo

peixian / sentry-elixir Goto Github PK

View Code? Open in Web Editor NEW

This project forked from getsentry/sentry-elixir

0.0 1.0 0.0 1.03 MB

The official Elixir SDK for Sentry (sentry.io)

Home Page: https://sentry.io

License: MIT License

Shell 0.21% Elixir 99.79%

sentry-elixir's Introduction

sentry

Build Status Hex Package Hex Docs

The official Sentry client for Elixir which provides a simple API to capture exceptions, automatically handle Plug exceptions, and provides a backend for the Elixir Logger. This documentation represents unreleased features, for documentation on the current release, see here.

Installation

To use Sentry with your projects, edit your mix.exs file and add it as a dependency. Sentry does not install a JSON library nor HTTP client by itself. Sentry will default to trying to use Jason for JSON operations and Hackney for HTTP requests, but can be configured to use other ones. To use the default ones, do:

defp deps do
  [
    # ...
    {:sentry, "8.0.0"},
    {:jason, "~> 1.1"},
    {:hackney, "~> 1.8"},
    # if you are using plug_cowboy
    {:plug_cowboy, "~> 2.3"},
  ]
end

Capture Crashed Process Exceptions

This library comes with an extension to capture all error messages that the Plug handler might not. This is based on Logger.Backend. You can add it as a backend when your application starts:

# lib/my_app/application.ex

+   def start(_type, _args) do
+     Logger.add_backend(Sentry.LoggerBackend)

The backend can also be configured to capture Logger metadata, which is detailed here.

Capture Arbitrary Exceptions

Sometimes you want to capture specific exceptions. To do so, use Sentry.capture_exception/2.

try do
  ThisWillError.really()
rescue
  my_exception ->
    Sentry.capture_exception(my_exception, [stacktrace: __STACKTRACE__, extra: %{extra: information}])
end

Capture Non-Exception Events

Sometimes you want to capture messages that are not Exceptions.

    Sentry.capture_message("custom_event_name", extra: %{extra: information})

For optional settings check the docs.

Configuration

Sentry has a range of configuration options, but most applications will have a configuration that looks like the following:

# config/config.exs
config :sentry,
  dsn: "https://[email protected]/1",
  environment_name: Mix.env(),
  included_environments: [:prod],
  enable_source_code_context: true,
  root_source_code_paths: [File.cwd!()]

Context and Breadcrumbs

Sentry has multiple options for including contextual information. They are organized into "Tags", "User", and "Extra", and Sentry's documentation on them is here. Breadcrumbs are a similar concept and Sentry's documentation covers them here.

In Elixir this can be complicated due to processes being isolated from one another. Tags context can be set globally through configuration, and all contexts can be set within a process, and on individual events. If an event is sent within a process that has some context configured it will include that context in the event. Examples of each are below, and for more information see the documentation of Sentry.Context.

# Global Tags context via configuration:

config :sentry,
  tags: %{my_app_version: "14.30.10"}
  # ...

# Process-based Context
Sentry.Context.set_extra_context(%{day_of_week: "Friday"})
Sentry.Context.set_user_context(%{id: 24, username: "user_username", has_subscription: true})
Sentry.Context.set_tags_context(%{locale: "en-us"})
Sentry.Context.add_breadcrumb(%{category: "web.request"})

# Event-based Context
Sentry.capture_exception(exception, [tags: %{locale: "en-us", }, user: %{id: 34},
  extra: %{day_of_week: "Friday"}, breadcrumbs: [%{timestamp: 1461185753845, category: "web.request"}]]

Fingerprinting

By default, Sentry aggregates reported events according to the attributes of the event, but users may need to override this functionality via fingerprinting.

To achieve that in Sentry Elixir, one can use the before_send_event configuration callback. If there are certain types of errors you would like to have grouped differently, they can be matched on in the callback, and have the fingerprint attribute changed before the event is sent. An example configuration and implementation could look like:

# lib/sentry.ex
defmodule MyApp.Sentry
  def before_send(%{exception: [%{type: DBConnection.ConnectionError}]} = event) do
    %{event | fingerprint: ["ecto", "db_connection", "timeout"]}
  end

  def before_send(event) do
    event
  end
end

# config.exs
config :sentry,
  before_send_event: {MyApp.Sentry, :before_send},
  # ...

Reporting Exceptions with Source Code

Sentry's server supports showing the source code that caused an error, but depending on deployment, the source code for an application is not guaranteed to be available while it is running. To work around this, the Sentry library reads and stores the source code at compile time. This has some unfortunate implications. If a file is changed, and Sentry is not recompiled, it will still report old source code.

The best way to ensure source code is up to date is to recompile Sentry itself via mix deps.compile sentry --force. It's possible to create a Mix Task alias in mix.exs to do this. The example below allows one to run mix sentry_recompile && mix compile which will compile any uncompiled or changed parts of the application, and then force recompilation of Sentry so it has the newest source. The second mix compile is required due to Mix only invoking the same task once in an alias.

# mix.exs
defp aliases do
  [sentry_recompile: ["compile", "deps.compile sentry --force"]]
end

For more documentation, see Sentry.Sources.

Testing Your Configuration

To ensure you've set up your configuration correctly we recommend running the included mix task. It can be tested on different Mix environments and will tell you if it is not currently configured to send events in that environment:

$ MIX_ENV=dev mix sentry.send_test_event
Client configuration:
server: https://sentry.io/
public_key: public
secret_key: secret
included_environments: [:prod]
current environment_name: :dev

:dev is not in [:prod] so no test event will be sent

$ MIX_ENV=prod mix sentry.send_test_event
Client configuration:
server: https://sentry.io/
public_key: public
secret_key: secret
included_environments: [:prod]
current environment_name: :prod

Sending test event!

Testing with Sentry

In some cases, users may want to test that certain actions in their application cause a report to be sent to Sentry. Sentry itself does this by using Bypass. It is important to note that when modifying the environment configuration the test case should not be run asynchronously. Not returning the environment configuration to its original state could also affect other tests depending on how the Sentry configuration interacts with them.

Example:

test "add/2 does not raise but sends an event to Sentry when given bad input" do
  bypass = Bypass.open()

  Bypass.expect(bypass, fn conn ->
    {:ok, _body, conn} = Plug.Conn.read_body(conn)
    Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
  end)

  Application.put_env(:sentry, :dsn, "http://public:secret@localhost:#{bypass.port}/1")
  Application.put_env(:sentry, :send_result, :sync)
  MyModule.add(1, "a")
end

When testing, you will also want to set the send_result type to :sync, so the request is done synchronously.

License

This project is Licensed under the MIT License.

sentry-elixir's People

Contributors

mitchellhenke avatar vishnevskiy avatar whatyouhide avatar hauleth avatar dcramer avatar josevalim avatar sl0thentr0py avatar johndoneth avatar howleysv avatar halostatue avatar ruslandoga avatar rafaeljesus avatar flupke avatar jbernardo95 avatar aselder avatar mitsuhiko avatar shosti avatar olafura avatar dustinfarris avatar tt avatar stevedomin avatar heymackey avatar gjaldon avatar mathiasose avatar jan-auer avatar mbramson avatar mazyod avatar michallepicki avatar lewapkon avatar peixian 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.